Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 11-07-2007, 09:26 AM   #1
LQ Newbie
Registered: Oct 2007
Posts: 2

Rep: Reputation: 0
Unhappy reading from serial port

Hi all,

I am trying to read from serial port /dev/ttyS0, which is connected to a device. but my problem is i could not able to read anything with read system call.

below given is the settings with which i am opening the serial port.

newtio.c_cflag = CS8| CRTSCTS | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
tcflush(fd, TCIFLUSH);

i am trying to configure the port for hardware flow control. with baud rate 38400 and 8E1 configuration.

i am able to write to the port. but not able to read from the port. is there any specific settings we need to do?

any help is appreciated.

Old 11-08-2007, 04:30 PM   #2
LQ Newbie
Registered: Aug 2007
Posts: 4

Rep: Reputation: 0
Serial port reads

Hi there,

You don't show the open or read code - but what you do show looks OK.
It's a while since I wrote any serial port stuff, but I've hacked up some code that works here - It too is based on the SerialPortHowTo examples.
I allow the read to return - even if it has nothing - but loop until it does.

One possibility is that your external device is not behaving as
you expect. If you have the hardware perhaps you could try this:
A serial cable connecting 5 to 5, 2 to 3 and 3 to 2.
Both connectors also have 7 & 8 linked - and 4 & 6 linked. (null modem)
First run two copies of minicom or GTKterm etc. You should be able to type in one and see the characters in the other.
Now replace one of the terminals with the code below.
It should show the characters typed and stop when you send ^C.
Finally replace the other terminal with your device and see if you
get anything from it.

Good luck!

#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUM_PORTS 5
#define SERIAL_PORT1 "/dev/ttyS0"
#define SERIAL_PORT2 "/dev/ttyS1"
#define SERIAL_PORT3 "/dev/ttyS2"
#define SERIAL_PORT4 "/dev/ttyUSB0"
#define SERIAL_PORT5 "/dev/ttyUSB1"
#define BAUDRATE B1200


static struct termios orig, new;

int main(int argc, char * argv[])
int tmp, port;
int res;
int fd;
char buf[50];
char rxch, *ptr;

struct termios oldtio,newtio;
char *DeviceName;
if(argc < 2) {
printf("Usage %s n Serial port to use.\n", argv[0]) ;

tmp = atoi(argv[1]);

if((tmp >= NUM_PORTS) || (tmp < 0)) tmp = 0;
port = tmp;

DeviceName = SerialDevices[port];
printf("Using %s\n", DeviceName);

fd = open(DeviceName, O_RDWR | O_NOCTTY);
if(fd < 0) { printf("Can't open %s\n",DeviceName); exit(-1); }

tcgetattr(fd,&oldtio); /* save current port settings */

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 0; /* inter-character timer unused. Non zero value t here means the rd returns after t * 100ms */
newtio.c_cc[VMIN] = 0; /* No minimum number of chars - the read may return with nothing */

tcflush(fd, TCIFLUSH);
// fcntl(fd, F_SETFL, FNDELAY);
printf("Port %s set up\n", DeviceName);

do {
res = read(fd,buf,1); /* returns # of chars input */
if(res > 0) {
printf("Got %d bytes\n", res);
ptr = buf;
if(res < 0) {
// printf("Serial port read error\n");
res = 0;
rxch = *ptr++;
rxch &= 0x7f;
if(rxch == 3)
printf("\n Quiting\n");
tcsetattr(fd,TCSANOW,&oldtio); // Restore serial port settings
if((rxch >= ' ') && (rxch < 0x7f)) printf("%c\n", rxch);
} while(1);
Old 11-08-2007, 11:25 PM   #3
LQ Newbie
Registered: Oct 2007
Posts: 2

Original Poster
Rep: Reputation: 0


thanks for the reply,

now i am able to read response from the device which i am talking to. the problem was in the serial port settings flag.

i configured the c_cflag as below:

newtio.c_cflag |= (CS8 | CREAD | PARENB);

and c_cc[VMIN] =2;
c_cc{VTIME] = 0;

and it is working.

i think the CLOCAL, CRTSCTS are not needed. but do not know why those are not needed. i was experimenting with the flags, and it worked surprisingly with the above settings.

do u know why it is like that?



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble reading from serial port glo Linux - Newbie 0 02-09-2007 10:42 AM
help reading data from a serial port skydemon Linux - General 1 08-10-2006 08:48 AM
[REQUEST] C prog || SERIAL PORT reading nawin_g Programming 4 05-03-2006 09:53 AM
reading data from a serial port mchitrakar Linux - Networking 3 12-04-2004 01:24 PM
Reading Baud Rate of Serial Port zaheer031 Programming 1 09-27-2004 12:47 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration