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.
I'm writing a program which reads and writes data on the serial device.
Reading works fine. But with writing I'm struggling.
Here is my code reduced to the minimum:
Code:
//----< Open serial device >----------------------------------
int fileDescriptor;
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
if (fileDescriptor == -1) {
perror("Error while opening serial interface occurred!");
return -99;
}
// set new parameters to the serial device
struct termios newtio;
tcgetattr(fileDescriptor, &oldTio);
// flush it before restore it.
bzero(&newtio, sizeof(newtio));
// newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
// canonical disable -> RAW input
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_cflag = BAUDRATE | CRTSCTS | CLOCAL | CREAD;
// When the OPOST option is disabled, all other option bits in c_oflag are ignored.
// newtio.c_oflag &= ~OPOST;
// newtio.c_oflag |= OPOST;
newtio.c_oflag = 0;
secs vor next char during burst
tcflush(fileDescriptor, TCIFLUSH);
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}
//---- Open serial device ----------------------------------
char acki[32] = { "test test\n\0" };
// finally writing
if (write(fd, acki, strlen(acki)) != strlen(acki)) {
perror("can't write);
}
I'm testing my program with a Nullmodem-Cable plugged in to /dev/ttyS0 and /dev/ttyUSB0
One end is the program by me, at the other end I use Screen. I checked the setup by running Screen on both ends and writing messages to each other end.
I guess the error is somewhere in the Serial-Settings. Any suggestions? Thanks
Without seeing all of your code it is difficult to provide help. What you posted does not look like it will compile without errors. Homework?
One error that pops out:
Code:
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
vs
write(fd, acki, strlen(acki))
Sorry, I merged the given code from two functions into one. Of course fd is the valid and open filedescriptor. Anyway, writing works just fine, and returns the number of written bytes. No homework. I'm trying to interrogate my solar power converter.
Again, I'm fine while reading from the serial device without any problems. I just have the impression that in case of writing nothing comes out.
Quote:
Originally Posted by salasi
Err, but what does happen? Do you get nothing, lost characters or are all you characters corrupt?
And you cable, what connections have you made in addition to txdata and rxdata? And are you trying to use software or hardware flow control?
I'm loosing all characters which I try to write out.
I'm using a ordinary Null-Modem cable which worked fine during a test with a Minicom or Screen apps at both ends.
I guess I'm using the standard software flow control.
If you can read then the serial port parameters are ok and so you should be able to transmit characters as well.
If nothing comes out then you still might have errors in your code which is why I asked if you would post your entire program.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.