Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I wrote this a long time ago, and just copy and paste the bits needed into each project.</p> <pre><code>#include &lt;errno.h&gt; #include &lt;fcntl.h&gt; #include &lt;string.h&gt; #include &lt;termios.h&gt; #include &lt;unistd.h&gt; int set_interface_attribs (int fd, int speed, int parity) { struct termios tty; memset (&amp;tty, 0, sizeof tty); if (tcgetattr (fd, &amp;tty) != 0) { error_message ("error %d from tcgetattr", errno); return -1; } cfsetospeed (&amp;tty, speed); cfsetispeed (&amp;tty, speed); tty.c_cflag = (tty.c_cflag &amp; ~CSIZE) | CS8; // 8-bit chars // disable IGNBRK for mismatched speed tests; otherwise receive break // as \000 chars tty.c_iflag &amp;= ~IGNBRK; // disable break processing tty.c_lflag = 0; // no signaling chars, no echo, // no canonical processing tty.c_oflag = 0; // no remapping, no delays tty.c_cc[VMIN] = 0; // read doesn't block tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout tty.c_iflag &amp;= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, // enable reading tty.c_cflag &amp;= ~(PARENB | PARODD); // shut off parity tty.c_cflag |= parity; tty.c_cflag &amp;= ~CSTOPB; tty.c_cflag &amp;= ~CRTSCTS; if (tcsetattr (fd, TCSANOW, &amp;tty) != 0) { error_message ("error %d from tcsetattr", errno); return -1; } return 0; } void set_blocking (int fd, int should_block) { struct termios tty; memset (&amp;tty, 0, sizeof tty); if (tcgetattr (fd, &amp;tty) != 0) { error_message ("error %d from tggetattr", errno); return; } tty.c_cc[VMIN] = should_block ? 1 : 0; tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout if (tcsetattr (fd, TCSANOW, &amp;tty) != 0) error_message ("error %d setting term attributes", errno); } ... char *portname = "/dev/ttyUSB1" ... int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC); if (fd &lt; 0) { error_message ("error %d opening %s: %s", errno, portname, strerror (errno)); return; } set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity) set_blocking (fd, 0); // set no blocking write (fd, "hello!\n", 7); // send 7 character greeting usleep ((7 + 25) * 100); // sleep enough to transmit the 7 plus // receive 25: approx 100 uS per char transmit char buf [100]; int n = read (fd, buf, sizeof buf); // read up to 100 characters if ready to read </code></pre> <p>The values for speed are <code>B115200</code>, <code>B230400</code>, <code>B9600</code>, <code>B19200</code>, <code>B38400</code>, <code>B57600</code>, <code>B1200</code>, <code>B2400</code>, <code>B4800</code>, etc. The values for parity are <code>0</code> (meaning no parity), <code>PARENB|PARODD</code> (enable parity and use odd), <code>PARENB</code> (enable parity and use even), <code>PARENB|PARODD|CMSPAR</code> (mark parity), and <code>PARENB|CMSPAR</code> (space parity).</p> <p>"Blocking" sets whether a <code>read()</code> on the port waits for the specified number of characters to arrive. Setting <em>no blocking</em> means that a <code>read()</code> returns however many characters are available without waiting for more, up to the buffer limit.</p> <hr> <p>Addendum:</p> <p><code>CMSPAR</code> is needed only for choosing mark and space parity, which is uncommon. For most applications, it can be omitted. My header file <code>/usr/include/bits/termios.h</code> enables definition of <code>CMSPAR</code> only if the preprocessor symbol <code>__USE_MISC</code> is defined. That definition occurs (in <code>features.h</code>) with</p> <pre><code>#if defined _BSD_SOURCE || defined _SVID_SOURCE #define __USE_MISC 1 #endif </code></pre> <p>The introductory comments of <code>&lt;features.h&gt;</code> says:</p> <pre><code>/* These are defined by the user (or the compiler) to specify the desired environment: ... _BSD_SOURCE ISO C, POSIX, and 4.3BSD things. _SVID_SOURCE ISO C, POSIX, and SVID things. ... */ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload