<br><br><div class="gmail_quote">---------- Forwarded message ----------<br>From: <b class="gmail_sendername">shibin k reeny</b> <span dir="ltr"><<a href="mailto:shibinkreeny@gmail.com">shibinkreeny@gmail.com</a>></span><br>
Date: Wed, Nov 23, 2011 at 11:18 AM<br>Subject: plz help with the serial port pgm....<br>To: <a href="mailto:kernelnewbies@kernelnewbies.org">kernelnewbies@kernelnewbies.org</a><br><br><br>rs232 read pgm is not working when i connect in loopback but write is wroking plz help...<br>
<br>pgm<br><br>#include <stdio.h><br>#include <stdlib.h><br>#include <string.h><br>#include <unistd.h><br>
#include <fcntl.h><br>#include <termios.h><br><br>#define _POSIX_SOURCE 1<br><br>#define BAUDRATE B9600<br>#define MODEMDEVICE "/dev/ttyS1"<br><br>int main(void)<br>{<br> int ret, fd;<br>
struct termios tio;<br> char buf[255];<br><br> /* Open device for reading and writing */<br> fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY);<br> if (fd < 0) {<br> fprintf(stderr, "Error opening device %s\n", MODEMDEVICE);<br>
goto done;<br> }<br> /* get current port attributes */<br> ret = tcgetattr(fd, &tio);<br> if (ret < 0) {<br> fprintf(stderr, "Error retreiving attributes, ret=%d\n", ret);<br> goto done;<br>
}<br> tcflush(fd, TCIFLUSH);<br><br> /* set port input speed */<br> ret = cfsetispeed(&tio, BAUDRATE);<br> if (ret) {<br> fprintf(stderr, "Error setting input Baud rate, ret=%d\n", ret);<br>
goto done;<br> }<br> /* set port output speed */<br> ret = cfsetospeed(&tio, BAUDRATE);<br> if (ret) {<br> fprintf(stderr, "Error setting output Baud rate, ret=%d\n", ret);<br> goto done;<br>
}<br><br> tio.c_cflag &= ~CRTSCTS; /* HW flow ctl OFF */<br> tio.c_cflag &= ~PARENB; /* no parity */<br> tio.c_cflag &= ~CSTOPB; /* 1 stop bit */<br> tio.c_cflag &= ~CSIZE; /* 1 stop bit */<br>
tio.c_cflag |= CS8; /* char size; 8N1 */<br> tio.c_cflag |= CREAD; /* enable receiver */<br> /* set port attributes */<br> ret = tcsetattr(fd, TCSANOW, &tio);<br> if (ret < 0) {<br> fprintf(stderr, "Error setting attributes, ret=%d\n", ret);<br>
goto done;<br> }<br> fcntl(fd, F_SETFL, FNDELAY);<br> ret = write(fd, "ab\r", 3);<br> if (ret < 0)<br> fprintf(stderr, "write() of 3 bytes failed!, ret=%d\n", ret);<br> <br>
printf("no of data written = %d\n",ret);<br> usleep(5000);<br> while (1) {<br> ret = read(fd, buf, sizeof(char));<br> if (ret > 0) {<br> printf("%c",buf[0]);<br> <br>
}<br> }<br>done:<br> close(fd);<br> return 0;<br>}<br><br><br>##################################<br>output../a.out <br>no of data written = 3<br><br><br><br><br>########################<br>
strace ./a.out<br>
read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>
read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>
read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)<br>read(3, 0xbfa8d5dd, 1) = -1 EAGAIN (Resource temporarily unavailable)
</div><br>