0

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:

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

Community
  • 1
  • 1
Stefan Jaritz
  • 1,999
  • 7
  • 36
  • 60
  • What bytes does it read? Is it that "hello" text back, or what? – hyde Feb 09 '17 at 13:57
  • 1
    Anyway, using threads may have its own complications (safely communicating between threads). You might want to consider more traditionall `poll`/`select` loop (here's a decent guide for using `select`, it's more about network sockets but applies to any socket: http://beej.us/guide/bgnet/) – hyde Feb 09 '17 at 14:00
  • RS422 is half-duplex, while RS485 supports full-duplex. – 0andriy Jul 19 '19 at 15:59

0 Answers0