All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Pali Rohár" <pali@kernel.org>
To: linux-man@vger.kernel.org,
	Alejandro Colomar <alx.manpages@gmail.com>,
	Michael Kerrisk <mtk.manpages@gmail.com>
Cc: "Marek Behún" <kabel@kernel.org>,
	libc-alpha@sourceware.org,
	"G. Branden Robinson" <g.branden.robinson@gmail.com>
Subject: [PATCH v2] ioctl_tty.2: Add example how to get or set baudrate on the serial port
Date: Fri, 30 Jul 2021 15:02:50 +0200	[thread overview]
Message-ID: <20210730130251.18781-1-pali@kernel.org> (raw)
In-Reply-To: <20210730095333.6118-1-pali@kernel.org>

Signed-off-by: Pali Rohár <pali@kernel.org>

---
Changes in v2:
* Use \e for backslash
* Use exit(EXIT_*) instead of return num
* Sort includes
* Add comment about possible fallback
---
 man2/ioctl_tty.2 | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2
index 967b5c4c78c9..771a9a470bf0 100644
--- a/man2/ioctl_tty.2
+++ b/man2/ioctl_tty.2
@@ -748,6 +748,67 @@ main(void)
     close(fd);
 }
 .EE
+.PP
+Get or set arbitrary baudrate on the serial port.
+.PP
+.EX
+#include <asm/termbits.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int
+main(int argc, char *argv[])
+{
+#if !defined(TCGETS2) || !defined(TCSETS2) || !defined(BOTHER)
+    fprintf(stderr, "TCGETS2, TCSETS2 or BOTHER is unsupported\en");
+    /* Program may fallback to TCGETS/TCSETS with Bnnn constants */
+    exit(EXIT_FAILURE);
+#else
+    struct termios2 tio2;
+    int fd, rc;
+
+    if (argc != 2 && argc != 3) {
+        fprintf(stderr, "Usage: %s device [new_baudrate]\en", argv[0]);
+        exit(EXIT_FAILURE);
+    }
+
+    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
+    if (fd < 0) {
+        perror("open");
+        exit(EXIT_FAILURE);
+    }
+
+    rc = ioctl(fd, TCGETS2, &tio2);
+    if (rc) {
+        perror("TCGETS2");
+        close(fd);
+        exit(EXIT_FAILURE);
+    }
+
+    printf("%u\en", tio2.c_ospeed);
+
+    if (argc == 3) {
+        tio2.c_cflag &= ~CBAUD;
+        tio2.c_cflag |= BOTHER;
+        tio2.c_ospeed = tio2.c_ispeed = atoi(argv[2]);
+
+        rc = ioctl(fd, TCSETS2, &tio2);
+        if (rc) {
+            perror("TCSETS2");
+            close(fd);
+            exit(EXIT_FAILURE);
+        }
+    }
+
+    close(fd);
+    exit(EXIT_SUCCESS);
+#endif
+}
+.EE
 .SH SEE ALSO
 .BR ldattach (1),
 .BR ioctl (2),
-- 
2.20.1


  parent reply	other threads:[~2021-07-30 13:03 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-30  9:53 [PATCH] ioctl_tty.2: Add example how to get or set baudrate on the serial port Pali Rohár
2021-07-30 11:47 ` Alejandro Colomar (man-pages)
2021-07-30 12:05   ` Pali Rohár
2021-07-30 12:21     ` Alejandro Colomar (man-pages)
2021-07-30 13:02 ` Pali Rohár [this message]
2021-08-01 13:51 ` [PATCH v3] " Pali Rohár
2021-08-04 22:08   ` Pali Rohár
2021-08-05  5:52     ` Greg Kroah-Hartman
2021-08-05  8:22       ` Pali Rohár
2021-08-05  8:30         ` Greg Kroah-Hartman
2021-08-05  8:44           ` Pali Rohár
2021-08-05  8:50             ` Greg Kroah-Hartman
2021-08-05  9:51               ` Pali Rohár
2021-08-05 15:28                 ` Alejandro Colomar (man-pages)
2021-08-05 16:14                   ` Greg Kroah-Hartman
2021-08-05 16:45                     ` Licensing example programs in man-pages (was [PATCH v3] ioctl_tty.2: Add example how to get or set baudrate on the serial port) Alejandro Colomar (man-pages)
2021-08-05 17:54                       ` Greg Kroah-Hartman
2021-08-06  7:22                         ` Alejandro Colomar (man-pages)
2021-08-06  8:32                           ` Pali Rohár
2021-08-08  8:35   ` [PATCH v3] ioctl_tty.2: Add example how to get or set baudrate on the serial port Alejandro Colomar (man-pages)
2021-08-08 21:05     ` Pali Rohár
2021-08-08 21:19       ` Alejandro Colomar (man-pages)
2021-08-10 19:49 ` [PATCH v4] " Pali Rohár
2021-08-10 20:11   ` Pali Rohár
2021-08-31 20:34   ` Pali Rohár
2021-09-10 13:37     ` Alejandro Colomar (man-pages)
2021-09-10 13:39       ` Pali Rohár

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210730130251.18781-1-pali@kernel.org \
    --to=pali@kernel.org \
    --cc=alx.manpages@gmail.com \
    --cc=g.branden.robinson@gmail.com \
    --cc=kabel@kernel.org \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=mtk.manpages@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.