View topic - why I can't receive char from serial port?

Page 1 of 1

why I can't receive char from serial port?

PostPosted: Fri Aug 10, 2012 6:45 am
by kelles007
1.I start serial port service by "devc-8250 &"
2.setup parameter for port
if ( ( fd = open ( "/dev/ser1", O_RDWR) ) != -1 )
{
struct termios termios_p;
tcgetattr( fd, &termios_p );
cfsetispeed( &termios_p, 4800);
cfsetospeed( &termios_p, 4800);
tcsetattr( fd, TCSADRAIN, &termios_p );
flag |= CS8 | CREAD ;
flag &= ~CSTOPB;
flag &= ~PARENB;
termios_p.c_cflag = flag;
tcsetattr( fd, TCSADRAIN, &termios_p );
}
3.read(fd,buffer,size);
but I receive nothing,"read" is blocked,in fact,even if I use "cat /dev/ser",I can receive nothing,why?

Re: why I can't receive char from serial port?

PostPosted: Fri Aug 10, 2012 8:18 pm
by maschoen
kelles007 wrote:1.I start serial port service by "devc-8250 &"
2.setup parameter for port
if ( ( fd = open ( "/dev/ser1", O_RDWR) ) != -1 )
{
struct termios termios_p;
tcgetattr( fd, &termios_p );
cfsetispeed( &termios_p, 4800);
cfsetospeed( &termios_p, 4800);
tcsetattr( fd, TCSADRAIN, &termios_p );
flag |= CS8 | CREAD ;
flag &= ~CSTOPB;
flag &= ~PARENB;
termios_p.c_cflag = flag;
tcsetattr( fd, TCSADRAIN, &termios_p );
}
3.read(fd,buffer,size);
but I receive nothing,"read" is blocked,in fact,even if I use "cat /dev/ser",I can receive nothing,why?


I didn't read your code very carefully, but it looks ok. I would set the port using stty and eliminate everything between fopen and read and try again. Why are you so sure that it should be reading something? Do you have a line monitor on the input? read blocking when nothing is coming in would be normal. Also, if the tty parameters are off, you might also see nothing.

Re: why I can't receive char from serial port?

PostPosted: Sat Aug 11, 2012 1:16 pm
by denkelly
I can see two possiblities for why you may not be receiving data. Both have to do with control signal input to the port (especially if using a 3-wire cable).

...
termios_p.c_cflag &= ~(IHFLOW|OHFLOW)"; // add this to cancel input (and output) hw flow control
termios_p.c_cflag |= CLOCAL; // add this to ignore input modem control signals
tcsetattr( fd, TCSADRAIN, &termios_p ); // check for -1 return here - make sure it succeeds


Note IHFLOW and OHFLOW are QNX-only flags.

Of course the other reason may be that pins 2-3 are reversed! :)

Re: why I can't receive char from serial port?

PostPosted: Wed Oct 16, 2019 9:27 am
by johnbevy
Hi Kelles,

I am also facing the issue as you mentioned. Not able to receive characters via the serial port. But I confirmed that I am able to send data out using echo and write () system call.

Kindly help me if you have any ideas for this issue.