LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem in viewing data on serial port (https://www.linuxquestions.org/questions/linux-newbie-8/problem-in-viewing-data-on-serial-port-898221/)

uchiha 08-19-2011 01:10 AM

problem in viewing data on serial port
 
Hi,

I wrote a simple code in C that intends to write data on the serial port (/dev/ttyGS0) and verify if that data is being written (using windows - COM4). The code intends to write the word "DATA " on the serial port once every second, but on the console terminal on windows (command prompt) I see the word data being printed 7x every seven seconds. Below is the code:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main(int argc, char *argv[])
{
int fd;
struct termios options;

fd = open ("/dev/ttyGS0", O_RDWR | O_NOCTTY | O_NDELAY);

memset(&options, 0x00, sizeof(options));

options.c_cflag = (B115200 | CS8 | CLOCAL | CREAD);

options.c_lflag = 0;
options.c_iflag = 0;
options.c_oflag = 0;

options.c_cc[VTIME] = 0;
options.c_cc[VMIN] = 1;

tcsetattr(fd, TCSANOW, &options);



for (; ;)
{
tcflush(fd, TCIOFLUSH);
sleep(1);
write (fd, "data ", 5);

}
close(fd);
return 0;
}


Do you have any ideas why this happens? thanks.

-uchiha

theNbomr 08-20-2011 09:46 AM

Well, you didn't test the return value of your call to open(), so it is hard to know for sure. I'd guess that your file descriptor, fd, is pointing at either the stdout or stderr stream.
--- rod.


All times are GMT -5. The time now is 07:25 PM.