Displaying I/O possible when reading data from the serial port
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Displaying I/O possible when reading data from the serial port
Hi,
I have got a problem while reading from the serial port. I'm working on iMX-31 board and Eclipse IDE. Whenever I'm trying to read any data through the serial port, it's displaying I/O Possible and the application is being terminated. After reading I'm trying to write this data on a file. Here are the excerpts from the code:
# int main()
{
if (openport()<0)
return 0;
}
int rc;
int size=1024;
int count;
unsigned char buf[1024];
FILE *fp=NULL;
fp=fopen("data","wb");
while(1)
{
rc=read(fd,buf,size); //While executing this Statement, application is being terminated and I/O Possible is displayed.
The code you posted tries to read from a file that is only opened for writing.
There is nothing in your posted code that reads data from a serial port.
Please provide the entire program or more information on what you are trying to accomplish.
I have reported this thread to be moved to the appropriate forum.
In this code I'm trying to read the data through the serial port of my board (iMX31 board). The data is being sent by the hyperterminal of other PC. What I want to do is to first open a file, then read data from the serial port and write it onto the file. For reading through the serial port I used read which is mentioned in the code in bold letters. The above code is not reading the data. So, suggest me the required modifications to be done on the code for reading data.
Well part of the problem is you are opening your serial port with O_NDELAY and then later when you read from it you don't check your error for EAGAIN. When you open non-blocking (O_NDELAY or O_NONBLOCK) then your read can return a < 0 response with errno getting set to EAGAIN when there is no immediate data to read and your read call would have blocked. So either use fcntl to turn off non-blocking or in your if (rc < 0) check for errno == EAGAIN before you break out of your while loop.
I am facing a similar problem. My code works fine in RHEL but in Fedora the code exits with the error I/O possible just after read. I am attaching the code. Kindly advice me how to solve this problem. Why this is happening in Fedora
void Serialinit()
{
struct termios options;
printf("\nSerial Port1 is open\n");
portfd = open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY);
if (portfd == -1) {
/* Could not open the port.*/
perror("open_port: Unable to open /dev/ttyS1 - ");
}
/*if(portfd < 0){
printf("OPENING COM PORT - FAILED\r\n");
return;
}*/
what's is the exit condition for that do { } while loop. When you have NDELAY set your read can exit with < 0 characters read and it's perfectly okay (unless an error really occurs, but I'd guess your seeing EAGAIN as your error). So you might either want to shut that off (the NDELAY) or check your error against EAGAIN and don't drop out of the loop if that's what you got for an error.
fd = open_port(); // Open tty device for RD and WR
printf("fd =%d\n",fd);
fcntl(fd, F_SETFL); // Configure port reading
tcgetattr(fd, &options); // Get the current options for the port
cfsetispeed(&options, B19200); // Set the baud rates to 19200
cfsetospeed(&options, B19200);
printf("1:\n");
options.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode
options.c_cflag &= ~PARENB; // No parity bit
options.c_cflag &= ~CSTOPB; // 1 stop bit
options.c_cflag &= ~CSIZE; // Mask data size
options.c_cflag |= CS8; // Select 8 data bits
//options.c_cflag &= CRTSCTS; // Disable hardware flow control
// Enable data to be processed as raw input
options.c_lflag &= ~(ICANON | ECHO | ISIG);
// Set the new attributes
// tcsetattr(fd, TCSANOW, &options);
int rd,wr;
while(1)
{ wr = write(fd,a,10);
if(wr <0)
{
perror("Write:");
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.