linux - how to open, read, and write from serial port in C -


i little bit confused reading , writing serial port. have usb device in linux uses ftdi usb serial device converter driver. when plug in, creates: /dev/ttyusb1.

i thought itd simple open , read/write in c. know baud rate , parity information, seems there no standard this?

am missing something, or can point me in right direction?

i wrote long time ago, , copy , paste bits needed each project.

#include <errno.h> #include <fcntl.h>  #include <string.h> #include <termios.h> #include <unistd.h>  int set_interface_attribs (int fd, int speed, int parity) {         struct termios tty;         memset (&tty, 0, sizeof tty);         if (tcgetattr (fd, &tty) != 0)         {                 error_message ("error %d tcgetattr", errno);                 return -1;         }          cfsetospeed (&tty, speed);         cfsetispeed (&tty, speed);          tty.c_cflag = (tty.c_cflag & ~csize) | cs8;     // 8-bit chars         // disable ignbrk mismatched speed tests; otherwise receive break         // \000 chars         tty.c_iflag &= ~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 &= ~(ixon | ixoff | ixany); // shut off xon/xoff ctrl          tty.c_cflag |= (clocal | cread);// ignore modem controls,                                         // enable reading         tty.c_cflag &= ~(parenb | parodd);      // shut off parity         tty.c_cflag |= parity;         tty.c_cflag &= ~cstopb;         tty.c_cflag &= ~crtscts;          if (tcsetattr (fd, tcsanow, &tty) != 0)         {                 error_message ("error %d tcsetattr", errno);                 return -1;         }         return 0; }  void set_blocking (int fd, int should_block) {         struct termios tty;         memset (&tty, 0, sizeof tty);         if (tcgetattr (fd, &tty) != 0)         {                 error_message ("error %d 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, &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 < 0) {         error_message ("error %d opening %s: %s", errno, portname, strerror (errno));         return; }  set_interface_attribs (fd, b115200, 0);  // set speed 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 transmit 7 plus                                      // receive 25:  approx 100 per char transmit char buf [100]; int n = read (fd, buf, sizeof buf);  // read 100 characters if ready read 

the values speed b115200, b230400, b9600, b19200, b38400, b57600, b1200, b2400, b4800, etc. values parity 0 (meaning no parity), parenb|parodd (enable parity , use odd), parenb (enable parity , use even), parenb|parodd|cmspar (mark parity), , parenb|cmspar (space parity).

"blocking" sets whether read() on port waits specified number of characters arrive. setting no blocking means read() returns many characters available without waiting more, buffer limit.


addendum:

cmspar needed choosing mark , space parity, uncommon. applications, can omitted. header file /usr/include/bits/termios.h enables definition of cmspar if preprocessor symbol __use_misc defined. definition occurs (in features.h) with

#if defined _bsd_source || defined _svid_source  #define __use_misc     1 #endif 

the introductory comments of <features.h> says:

/* these defined user (or compiler)    specify desired environment:  ...    _bsd_source          iso c, posix, , 4.3bsd things.    _svid_source         iso c, posix, , svid things. ...  */ 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -