LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2009, 06:40 AM   #1
makphy
LQ Newbie
 
Registered: Aug 2009
Posts: 8

Rep: Reputation: 0
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.

if (rc<0)
{
printf("Read error= %d %s\n",errno,strerror(errno));
}
printf("Received %d bytes\n",rc);

.......
} #

I need to read the data and write it on a file.

Thanks for your help.
 
Old 08-31-2009, 07:47 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
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.

Last edited by michaelk; 08-31-2009 at 07:51 PM.
 
Old 08-31-2009, 11:27 PM   #3
makphy
LQ Newbie
 
Registered: Aug 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for replying.

The full code:

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<termios.h>
#include<string.h>
#include<errno.h>
#include<stdlib.h>
#define BAUD B9600

int openport();
void closeport();

static int fd = 0;

int openport()
{
fd = open("/dev/ttymxc2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("open error\n");
}
else
{
struct termios my_termios;
fcntl(fd,F_SETFL,FASYNC); // Asynchronous Mode
tcgetattr(fd,&my_termios); // Get initial parameters of serial

cfsetispeed(&my_termios,BAUD); //Setting input speed
cfsetospeed(&my_termios,BAUD); //Setting output speed

my_termios.c_cflag |= (BAUD | CS8 | CREAD | CLOCAL); //Device not to become master
my_termios.c_cflag &= ~CSTOPB; // 1 stop bit
my_termios.c_cflag &= ~PARENB; // No parity bit
my_termios.c_cflag &= ~CRTSCTS; // No hardware control
my_termios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //Set echo off
my_termios.c_lflag &= ~(IXON | IXOFF | IXANY); // No software control

tcflush(fd,TCIFLUSH); // Flush input buffer
tcflush(fd,TCOFLUSH); // Flush output buffer

tcsetattr(fd,TCSANOW,&my_termios);
}
return fd;
}

void closeport()
{
if (fd > 0)
{
close(fd);
}
}

int main()
{
if (openport() < 0)
return 0;

int rc;
int size = 1024;
unsigned char buf[1024];
FILE *fp = NULL;
int count;

fp = fopen("data","wb");

if (fp == NULL)
{
puts("Can't open file\n");
}

while(1)
{
rc = read(fd,buf,size);

if (rc < 0)
{
printf("Read error = %d %s\n",errno,strerror(errno));
break;
}
if (rc == 0)
{
printf("Read returned no data\n");
continue;
}
printf("Received %d bytes\n",rc);

count = fwrite(buf,sizeof(buf),rc,fp); //Write received data to file

if (count != rc)
{
printf("fwrite error = %d %s\n",errno,strerror(errno));
break;
}
fflush(fp);
}
closeport();
fclose(fp);

return 0;
}

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.
 
Old 09-01-2009, 04:37 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Please use code tags in you post your program. Not sure why you are getting that error. Look at this how to:


http://www.faqs.org/docs/Linux-HOWTO...ing-HOWTO.html
 
Old 09-01-2009, 09:52 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Moved: This thread is more suitable in Programming and has been moved accordingly to help your question get the exposure it deserves.
 
Old 09-01-2009, 10:29 PM   #6
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
Old 09-02-2009, 06:24 AM   #7
makphy
LQ Newbie
 
Registered: Aug 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for your suggestion..Now the code is working properly...
 
Old 01-13-2012, 03:31 AM   #8
Leena
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
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;
}*/


options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
options.c_cflag = B4800|CS8|CLOCAL|CREAD;
options.c_cc[VMIN]=1;
options.c_cc[VTIME]=0;
tcflush(portfd,TCIFLUSH);
fcntl(portfd, F_SETFL, FASYNC);

if(tcsetattr(portfd, TCSANOW, &options))
{
perror("cannot set the attributes\n");
}
tcflush(portfd, TCIOFLUSH);

}

do
{
int no_of_char_read = 0;

no_of_char_read =read(portfd, &datas,1);
//no_of_char_read =read(portfd, buftty,255);

//printf("datas:%x\n", datas);

if(count < 0)
count = 0;
buffers[count] = datas; // store in buffers array
printf("buffers[%d]: %x\n", count, buffers[count]);

}
 
Old 01-14-2012, 06:33 PM   #9
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
Old 06-18-2013, 05:14 AM   #10
sushantbhangale
LQ Newbie
 
Registered: Aug 2012
Posts: 4

Rep: Reputation: Disabled
Genertae Error IO POSSIBLE

#include <stdio.h> // Standard input/output definitions
#include <string.h> // String function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions

int open_port(void)
{

int fd; // File descriptor for the port
fd = open("/dev/ttyS0", O_RDWR | O_NONBLOCK);

if (fd == -1)
{
fprintf(stderr, "open_port: Unable to open /dev/ttyS0 %s\n",strerror(errno));
// exit(EXIT_FAILURE);
}
return (fd);
}

int main(void){

int fd = 0; // File descriptor
struct termios options; // Terminal options
char *a="Sushant ME";
//a = malloc(100);

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:");
}

printf("Enter the data:--");
memset(a,'\0',sizeof(a));
rd = read(fd,a,10);
if(rd <0)
{
perror("Read:");
}
printf("a= %s\n",a);

}


///////////////////////////////////
// Simple read and write code here//
//////////////////////////////////
// Close file descriptor & exit
close(fd);
return 0;
}
 
  


Reply



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
Reading data from serial port at 8 Kb/sec harshadnatiye Linux - Newbie 2 07-20-2009 07:42 AM
Serial port reading jayadhanesh Linux - Newbie 3 05-19-2009 06:54 PM
reading from serial port IC009562 Linux - Software 2 11-08-2007 11:25 PM
help reading data from a serial port skydemon Linux - General 1 08-10-2006 08:48 AM
reading data from a serial port mchitrakar Linux - Networking 3 12-04-2004 01:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 PM.

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