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

CODE:

#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;
}


main()
{
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;
}
else
{
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);
write(fd,"devesh",6);
usleep(500000);
/*
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);


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




[DEV'H]
Devesh Sharma
"Have A Nice Time"


Bollywood, fun, friendship, sports and more. You name it, we have it on http://in.promos.yahoo.com/groups --
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list

Relevant Pages

  • Re: K&R exercise 1-18
    ... _without_ the string terminator and has MAXIMUM+1 array elements. ... char lineis in this context equivalent to char *line ... int main ... You lose one character by overwriting it with '\n'. ...
    (comp.lang.c)
  • Re: Write to file
    ... You can read the file one character at a time. ... compared to the current char. ... Once the (lgh - 1)th char ... void initnext(int *next, const char *id, int lgh) ...
    (comp.lang.c)
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)
  • Re: Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
    ... My fix is now reflected at the above address. ... I don't think that overwriting the n-th character with a ... single char location by letting a set sign bit indicate that a line ... int main ...
    (comp.lang.c)
  • [EXPL] TIBCO RendezVous Buffer Overflow (Exploit)
    ... TIB/Rendezvous Routing Communications Daemon (add router buffer ... TIB/Rendezvous Secure Daemon (port 7580) ... char *daemon; ... unsigned int DataOffset; ...
    (Securiteam)