Hej,
I am running a custom build Linux for an embedded device. This device has a RS485 which is mapped to /dev/ttyS2. Actually I am looking for an example how to set up an asynchronous communication (async read/write calls). My actual solution works like that:
- open ttyS2
- setup ttyS2 like in this example "how to open, read, and write from serial port in C"
- configure ttyS2 RS485 like in this example "kernel.org RS485 SERIAL COMMUNICATIONS"
- set some pins of my custom mainboard
- create pthread and call read on ttyS2
- wait in my main function for inputs and send them via write to ttyS2
I discovered that every time when I am writing to ttyS2 and reading from it artefacts where generated. An example:
int fd;
fd = open("/dev/ttyS2", O_RDWR);
@my RX thread:
for (;;) {
amount = read(fd, rxBuffer, 1024);
printf("[rx]:%i\n", amount);
}
@my main:
char * myText = "hello";
printf("[tx]: %s\n",myText);
amount = write(fd, myText,strlen(myText));
The output looks like:
[tx]: hello
[rx]: 1
[rx]: 1
[rx]: 1
[rx]: 1
[rx]: 1
This means that the ttyS2 switches the RS485 between rx/tx and generates the artificial bytes. When using an RS232 it's not failing(because RS232 is duplex).
I like to keep my asynchronous program design. But I need to interrupt the RX operations before initiating the TX ones. So far I did not discovered an "chancel(fd, RX)" command for making read to stop.
How would you solve this problem? Ideas and examples are welcome