Lost bit in reading from serial port - c

I'm trying to read data from custom hardware using serial port. I believe the configuration was right. The problem is, I see some of the byte is missing. In the data, length of the data is specified, so I know if some of the data were missing.
My configuration is as follows :
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0) return -1;
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw input
tty.c_cc[VMIN] = 1; // read does block
tty.c_cc[VTIME] = 0; // 0 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl (flow control)
//disable hw flow control
#ifdef CRTSCTS
tty.c_cflag &= ~CRTSCTS;
#endif
#ifdef CNEW_RTSCTS
tty.c_cflag &= ~CNEW_RTSCTS;
#endif
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_iflag &= ~(ISTRIP | IGNCR | INLCR | ICRNL
#ifdef IUCLC
| IUCLC
#endif
);
tty.c_oflag &= ~(OPOST
#ifdef ONLCR
| ONLCR
#endif
#ifdef OCRNL
| OCRNL
#endif
#ifdef ONOCR
| ONOCR
#endif
#ifdef ONLRET
| ONLRET
#endif
);
if (tcsetattr (fd, TCSANOW, &tty) != 0) return -1;
return 0;
}
this is how I read it :
int fd = open (portname, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
char buf [10];
while(1) {
int n = 0;
while (n = read (fd, buf, sizeof buf) > 0) {
write(STDOUT_FILENO, buf, n); //this printing
.. other job ..
}
}
below is the example of data :
// correct data
#*69880001000800010007003F4530302403322402FE24080524000024012C2400142430303030302452B4FF
// example of lost data (lost data is signed by _ )
#*69880001000800010007003F4530302403322402FE2408092_000_24012C240014243030303030241674FF
#*69880001000800010007003F4530302403312402FF24080524_00024012C240014243030303030244EF9FF
For the configuration, What I'll need is :
baud rate : 19200
data bits : 8
stop bits : 1
parity : none
flow control : none
FYI, my hardware is sending data byte-per-byte. So n in the code above is always 1 (or 0).
When I try using putty, everything is okay. Is there something I missed?
Thank you

Related

Inverting output of /dev/ttyUSB0

I'm trying to inverted the output of /dev/ttyUSB0.
I'm working "C". I'm new to Linux but you won't hurt my feelings if you tell me this a stupid question.
I have 2 sources of RS485; one from a Raspberry Pi that looks like this:
From Raspberry Pi RS485 "shield"/daughter board (/dev/serial0):
+
|--| |--|
| | | |
____| |_____| |_________
-
The other is from an USB to RS485 converter on an Ubuntu machine (/dev/ttyUSB0):
+
_____ _______ __________
| | | |
| | | |
|--| |--|
-
I'd like to invert the 2nd/Ubuntu version so it looks like the Raspberry Pi version.
Here's my code:
int set_interface_attribs(int fd, int baudRateFlag)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0)
{
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)baudRateFlag);
cfsetispeed(&tty, (speed_t)baudRateFlag);
tty.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
// TODO: CRTSCTS isn't defined in termios.h
// but is in "arm-linux-gnueabihf\bits\termios.h"
tty.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
/* setup for non-canonical mode */
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
/* fetch bytes as they become available */
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
if (tcsetattr(fd, TCSANOW, &tty) != 0)
{
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}

How to listen on serial port with C using CRTSCTS

I am trying to listen to a serial port with a C program utilizing CRTSCTS (to prevent an independently powered Arduino from rebooting after the computer it is connected to restarts).
I started with the code from here: Canonical Mode Linux Serial Port
After some adaptations it looks like this:
#define SERIALTERMINAL "/dev/ttyUSB0"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int set_interface_attribs(int fd, int speed)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)speed);
cfsetispeed(&tty, (speed_t)speed);
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8; /* 8-bit characters */
tty.c_cflag &= ~PARENB; /* no parity bit */
tty.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
//tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= ~CRTSCTS; //trying to turn on CRTSCTS
tty.c_cflag &= ~HUPCL; //trying turning off HUPCL - it works!
tty.c_lflag |= ICANON | ISIG; /* canonical input */
tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);
tty.c_iflag &= ~IGNCR; /* preserve carriage return */
tty.c_iflag &= ~INPCK;
tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
tty.c_iflag &= ~(IXON | IXOFF | IXANY); /* no SW flowcontrol */
//tty.c_iflag |= ~IXON; //try ixon
tty.c_oflag &= ~OPOST;
tty.c_cc[VEOL] = 0;
tty.c_cc[VEOL2] = 0;
tty.c_cc[VEOF] = 0x04;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int main()
{
char *portname = SERIALTERMINAL;
int fd;
int wlen;
//fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC); original version
fd = open(portname, O_RDONLY | O_NOCTTY);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
/*baudrate 115200, 8 bits, no parity, 1 stop bit */
set_interface_attribs(fd, B115200);
/* simple canonical input */
do {
unsigned char buf[700];
unsigned char *p;
int rdlen;
rdlen = read(fd, buf, sizeof(buf) - 1);
if (rdlen > 0) {
buf[rdlen] = 0;
printf("%s", buf);
} else if (rdlen < 0) {
printf("Error from read: %d: %s\n", rdlen, strerror(errno));
} else { /* rdlen == 0 */
printf("Nothing read. EOF?\n");
}
/* repeat read */
} while (1);
}
When the program is run it doesn't show any output.
Going back out of CRTSCTS mode by swapping:
tty.c_cflag |= ~CRTSCTS;
with
tty.c_cflag &= ~CRTSCTS;
the program will show output but it is then not in the CRTSCTS mode which I need in order to prevent sending a DTR signal which results in an Arduino reset.
I have tried quite a few different options after reading through the Termios documentation: http://man7.org/linux/man-pages/man3/termios.3.html but to no avail.
So the question is what combination of options will allow reading from the serial port without sending a DTR signal?

Posix Serial Port Raw Read hanging

I'm testing an UART application on the BeagleBone Black and when I try to read from the device it blocks forever. A write works fine. I hooked up a logic analyzer to inspect the line and I can see the data being transmitted and received, but it just always blocks on a read.
int main() {
int res;
struct termios tty;
memset(&tty, 0, sizeof(tty));
int serial = open("/dev/ser2", O_RDWR | O_NOCTTY);
if (!isatty(serial))
return false;
// Setting the Baud rate
cfsetispeed(&tty, B9600);
cfsetospeed(&tty, B9600);
// 8N1 Mode
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag |= CREAD | CLOCAL;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_iflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG);
tty.c_oflag &= ~OPOST;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
if ((tcsetattr(serial, TCSANOW, &tty)) != 0)
return false;
if ((tcflush(serial, TCIOFLUSH)) != 0)
return false;
while (1)
{
uint8_t wrBuff[3] = {0xAA, 0xBB, 0xCC};
uint8_t res = write(serial, wrBuff, 3);
printf("Write = %d", res);
res = read(serial, wrBuff, 3);
printf("Read = %d", res);
}
return 0;
}
I attempted the fix mentioned in the comments below regards the incorrect flag set but this did not fix the issue.

Serial reads from a sensor using USB-serial cable in linux using C

I have been trying to read the responses from a serial temperature sensor interfaced to my raspberry pi using a USB to serial converter.
I can see that the writes to the sensor device seem to work. However when I try to read back from the serial chip the read fails with -1.
I did try to use the same baud rate 9600 8 bit no parity settings using realterm program and was able to read and write hex values as expected, kindly point me in the right direction.
void serial_write(char parameter,char value) {
int fd;
uint8_t bytes_wr;
char wr_buffer[3];
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
ERROR("Error! in Opening ttyUSB0 \n");
else
DEBUG("ttyUSB0 Opened Successfully \n");
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings);
cfsetispeed(&SerialPortSettings,B9600);
cfsetospeed(&SerialPortSettings,B9600);
SerialPortSettings.c_cflag &= ~PARENB;
SerialPortSettings.c_cflag &= ~CSTOPB;
SerialPortSettings.c_cflag &= ~CSIZE;
SerialPortSettings.c_cflag |= CS8;
SerialPortSettings.c_cflag &= ~CRTSCTS;
SerialPortSettings.c_cflag |= CREAD | CLOCAL;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
SerialPortSettings.c_oflag &= ~OPOST;
if ((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0)
ERROR("ERROR ! in Setting attributes \n");
else
DEBUG("BaudRate=9600\tStopBits=1\tParity=none \n");
wr_buffer[0] = write;
wr_buffer[1] = parameter;
wr_buffer[2] = value;
bytes_wr = write(fd, wr_buffer,sizeof(wr_buffer));
DEBUG("Total Bytes written: %d \n", sizeof(wr_buffer));
close(fd);
}
The above function seems to write as expected to the serial port, however when I try to read, the reads fails with a -1
char serial_read(char parameter) {
int fd, read_length, i;
uint8_t bytes_wr;
char wr_buffer[2];
fd = open("/dev/ttyUSB0",O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
ERROR("Error! in Opening ttyUSB0 \n");
else
DEBUG("ttyUSB0 Opened Successfully \n");
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings);
cfsetispeed(&SerialPortSettings,B9600);
cfsetospeed(&SerialPortSettings,B9600);
SerialPortSettings.c_cflag &= ~PARENB;
SerialPortSettings.c_cflag &= ~CSTOPB;
SerialPortSettings.c_cflag &= ~CSIZE;
SerialPortSettings.c_cflag |= CS8;
SerialPortSettings.c_cflag &= ~CRTSCTS;
SerialPortSettings.c_cflag |= CREAD | CLOCAL;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
SerialPortSettings.c_oflag &= ~OPOST;
if ((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0)
ERROR("ERROR ! in Setting attributes \n");
else
DEBUG("BaudRate=9600\tStopBits=1\tParity= none\n");
wr_buffer[0] = read;
wr_buffer[1] = parameter;
bytes_wr = write(fd, wr_buffer,sizeof(wr_buffer));
DEBUG("Total Bytes written: %d \n", sizeof(wr_buffer));
usleep(8000);
tcflush(fd,TCIFLUSH);
char rd_buffer[4];
read_length = read(fd, rd_buffer,sizeof(rd_buffer));
DEBUG("Total bytes read = %d \n",read_length);
for (i==0;i<read_length;i++){
DEBUG("rd_buffer[%d]=%x \n",i,rd_buffer[i]);
}
close(fd);
return rd_buffer[0];
}
With realterm windows application all writes and reads seem to work fine.
From the open(2) manpage:
O_NONBLOCK or O_NDELAY
When possible, the file is opened in nonblocking mode.
Neither the open() nor any subsequent operations on the file
descriptor which is returned will cause the calling process to
wait.
For a serial connection, the end result will be that if you ask to read some number of bytes from the serial port and there are no characters waiting, then read will return with -1 and 'errno' will probably be EAGAIN or EWOULDBLOCK.
So your usleep(8000) was probably an attempt to wait long enough for the device to respond but the device may not have data for you; especially if it is in the middle of an adc operation, it might take longer than 8ms.
There are a few things you can do:
You can (in pseudo code):
int retries=10;
while(retries--) {
read_length = read(fd, rd_buffer,sizeof(rd_buffer));
if(read_length > 0)
break;
usleep(1000);
}
Unfortunately, one side effect of this is that if the temperature sensor is sending you a lengthy string and your program read()s while the temperature sensor is still writing, you will get a partial string. So if you know the length of string that you're waiting to receive, you could use an ioctl() to find out how many characters are waiting:
ioctl(fd, FIONREAD, &bytes_avail);
So the pseudo code would look more like:
int retries=10;
int bytes_avail=0;
while(retries--) {
if (ioctl(fd, FIONREAD, &bytes_avail) < 0) {
fprintf(stderr, "ioctl failed\n");
return; // Do something here
}
if (bytes_avail >= sizeof(rd_buffer)) {
read_length = read(fd, rd_buffer,sizeof(rd_buffer));
if(read_length > 0)
break;
}
usleep(1000);
}
If the temperature sensor sends an ascii string that is terminated with a newline or carriage-return, then the code would look different.

C program setting port parameters before opening port fails

I am trying to write c code on a Linux system, where I set the serial port parameters then open the serial port, then I have found out that even though the code compiles and runs, I cannot read and write from that serial port (so the serial port was not opened successfully)!
Code that works (not needed):
int fd;
char *portname;
portname = "/dev/ttyUSB0";
struct termios tty;
fd = open(portname, O_RDWR | O_NOCTYY | O_SYNC );
memset(&tty,0,sizeof tty);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOP;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tcsetattr(fd,TCSANOW,&tty);
Code that doesn't work (needed)
int fd;
char *portname;
portname = "/dev/ttyUSB0";
struct termios tty;
memset(&tty,0,sizeof tty);
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOP;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS;
tcsetattr(fd,TCSANOW,&tty);
fd = open(portname, O_RDWR | O_NOCTYY | O_SYNC );
Solid question: My application sequence requires me to set serial port parameters then open the serial port. Is there a way to do this? if yes, how?
I appreciate your help.
Update: Removed C++ code noticed by #Alter Mann.
Update: Removed zeroing-out the termios structure noticed by #sawdust.
In first case you get actual file descriptor fd and after use it. In second case you try setting up uninitialized file descriptor fd (probably 0 if it's declared in global scope) and after get actual value of it.
Below is workaround which works for me:
#include <fcntl.h>
#include <termios.h>
#include <strings.h>
#include <stdlib.h>
int main (int argc, char * argv [])
{
struct termios tty;
const char * portname = "/dev/ttyUSB0";
const int fd = open (portname, O_RDONLY);
if (-1 == fd) {
// Problem...
return EXIT_FAILURE;
}
if (tcgetattr (fd, &tty) < 0) {
// Problem...
return EXIT_FAILURE;
}
cfsetospeed (&tty, (speed_t) B9600);
cfsetispeed (&tty, (speed_t) B9600);
tty.c_cflag |= B9600;
tty.c_cflag |= (tty.c_cflag & ~CSIZE) | CS8;
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= IGNPAR;
tty.c_cflag &= ~(CRTSCTS);
tty.c_cflag &= ~(CSTOPB);
tty.c_iflag |= IGNBRK;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_lflag = 0;
tty.c_oflag = 0;
tcflush (fd, TCIFLUSH);
if (tcsetattr (fd, TCSANOW, &tty) ) {
// Problem...
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Resources