Results 1 to 4 of 4
I have two usb2serial adapters connected to my PC with a null modem (I am doing some testing to learn serial comm for a particular application). I am writing to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 08-12-2010 #1Just Joined!
- Join Date
- Aug 2010
- Posts
- 2
Reading serial port with C
I have two usb2serial adapters connected to my PC with a null modem (I am doing some testing to learn serial comm for a particular application). I am writing to /dev/ttyUSB0 a trying to read /dev/ttyUSB1. I have used screen (screen /dev/ttyUSB1 115200) and am able to see the characters being written. I am also able to read and write with Python with the following code
I need to be able to do this in C. I am able to do the writing and have confirmed with screen as described in the first paragraph. I cannot however read. My C code is as followsCode:import serial,time,sys ser1 = serial.Serial('/dev/ttyUSB0',baudrate=115200) ser2 = serial.Serial('/dev/ttyUSB1',timeout=2,baudrate=115200) ser1.open() ser2.open() ser1.flush() ser2.flush() ser1.write('a') read = ser2.read(1) ser1.close() ser2.close()
What am I doing wrong?Code:#include <stdio.h> #include <fcntl.h> #include <termios.h> int main() { int fd1, fd2, n,i,maxfd,res; char buffer[1]; char *bufptr; fd_set readfs; struct timeval timeout; struct termios options1; struct termios options2; bufptr = buffer; fd1 = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd1 < 0) printf("Not connected to ttyUSB0\n"); fcntl(fd1, F_SETFL, 0); tcflush(fd1, TCIFLUSH); tcgetattr(fd1, &options1); cfsetispeed(&options1, B115200); cfsetospeed(&options1, B115200); options1.c_cflag &= ~PARENB; // set no parity, stop bits, data bits options1.c_cflag &= ~CSTOPB; options1.c_cflag &= ~CSIZE; options1.c_cflag |= CS8; tcsetattr(fd1, TCSANOW, &options1); fd2 = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY); if (fd2 < 0) printf("Not connected to ttyUSB1\n"); fcntl(fd2, F_SETFL, 0); tcflush(fd2, TCIFLUSH); tcgetattr(fd2, &options2); cfsetispeed(&options2, B115200); cfsetospeed(&options2, B115200); options2.c_cflag &= ~PARENB; // set no parity, stop bits, data bits options2.c_cflag &= ~CSTOPB; options2.c_cflag &= ~CSIZE; options2.c_cflag |= CS8; tcsetattr(fd2, TCSANOW, &options2); if (fd1 > fd2) maxfd = fd1+1; else maxfd = fd2+1; maxfd = fd2+1; n = write(fd1,"a\n",1); printf("Wrote %d bytes.\n",n); close(fd1); FD_SET(fd2, &readfs); timeout.tv_usec = 0; timeout.tv_sec = 1; res = select(maxfd, &readfs, NULL, NULL, &timeout); if (res == 0) printf("Serial not ready to read.\n"); i = read(fd2,buffer,1); if (i != 1) printf("Unable to read.\n"); close(fd2); return(0); }
Thanks,
Bill
- 08-16-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,144
I'll have to look at this closer later. I have to go teach a class tonight, so it may be a day or so before I have an answer for you, unless someone else gets to it first.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-16-2010 #3Just Joined!
- Join Date
- Aug 2010
- Posts
- 2
Rubberman,
Thanks for replying. I was able to get the above code to work. I change the write command from
toCode:n = write(fd1,"a\n",1);
I was able to run the code just fine. The end goal is to try to do faster than normal serial communication. I think I am running into a OS interrupt frequency limitation. My kernel is compiled at 1000 hz timer interrupt frequency. I need to be able to communicate a little faster than 1000 hz across the serial line. I have no idea of how to do this but would love to hear any thoughts on the issue.Code:n = write(fd1,"a\r",1);
Bill
- 08-21-2010 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 10,144
It depends upon how the serial port was configured (see the stty command). The way it was configured was to use just a carriage-return (\r) to empty the buffer. The new-line (\n) character won't do that. Python is probably a bit smarter in dealing with that cruft than you on C are...
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
