[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

cant able to read serial Port

hello All,
  i am very new in Linux , and i made a small program in C that jst sends and tries to receive character from serial port.
  it is working fine for sending, but not receiving character from other end .. on other end Windows Xp running.  Read() function retuns error
  no = 11, i m putting my small code,
        plz help  me ...
thank in advance
#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 readport(int fd, char *result)
 int iIn = read(fd, result, 254);
 result[iIn-1] = 0x00;
 if (iIn < 0) {
   printf("SERIAL EAGAIN ERROR\n");
   printf("\nerro NO =%d\n",errno);
   printf("SERIAL read error %d-----> %s\n", errno, strerror(errno));
   return 0;
 return 1;

int fd;
int nread;
char c;
char bufptr[5];
char buffer[5];
buffer[0]= 'S';
buffer[1]= 'S';
buffer[2]= 'S';
buffer[3]= 'S';
buffer[4]= 'S';
int i;
char sResult[254];
struct termios options;
// buffer = "Sorry";
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
 if (fd == -1)
 printf("\n\n\t\t Sorry Yaar Port Nahi Khul Raha \n\n\n");
 return 1;
 printf("\n\n\t\t Port Khul gaya.... \n\n\n");
 fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
 options.c_cflag &= ~PARENB;
 options.c_cflag &= ~CSTOPB;
 options.c_cflag &= ~CSIZE;
 options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options);
fcntl(fd, F_SETFL, FNDELAY);
nread = read(fd,buffer,5);
//if (nread < 0)
//write(2,"ERROR in read\n",20);
printf("\nnread = %d\n",nread);
printf("tong = %s\n",buffer);

 fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
 if (!readport(fd,sResult)) {
  printf("read failed\n");
  return 1;
 printf("readport=%s\n", sResult);
 return 0;


   Devesh Sharma

"Have A Nice Time"



Unlimited freedom, unlimited storage. Get it now
[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]