I am writing an executable that uses a 3rd party C library (libmodbus if it matters) to communicate via serial device (in my case, /dev/ttyUSB0
or similar to talk RS-485 via an FTDI chipset based USB-to-RS485 adapter). This executable, based on CLI args, can initiate commands (in my case, act like a modbus client) then await a response (in my case, from an external modbus server), or listen for incoming commands (in my case, act like a modbus server) then generate a response.
I would like to automate the testing of my executable, without a need for some external device. In other words, I'd like to launch two instances of my executable, where:
- The first instance is put into modbus client mode and uses
/dev/xxxx
for comms - The second instance is put into modbus server mode and uses
/dev/yyyy
for comms - Set up
/dev/xxxx
and/dev/yyyy
to both act as serial devices that are are essentially the two ends of the same wire.
From what I read on pty manpage, I believe /dev/xxxx
and /dev/yyyy
are the two ends of a pseudo-terminal. Which brings me to my questions:
- The man page refers to BSD-style pseudoterminals which seem more appropriate to what I am trying to do.
- Is my understanding of BSD-style pseudoterminals correct?
- If so, is it possible to create BSD-style pseudoterminals on non-BSD linux distributions? In pacticular, I am using debian 10, 11, 12 (and debian based, like Ubuntu 20.04, 22.04)
- The man page also refers to UNIX 98 pseudoterminals, which are implemented using
posix_openpt()
. However, even after the subsequentgrantpt()
andunlockpt()
, I can only one one/dev/pts
device for the client side of the pty, with the master side being only a file descriptor inside the executable.- Is my understanding (which is loosely based on code like this) correct?
- If so, what tricks may I use to convert the master side file descriptor to an proper
/dev/xxxx
which is the only API available to get a modbus context
- Are there other "standard" linux tools for doing what I am attempting to do? It seems like tools like the ones mentioned here are expecting to connect an executable's STDIO to the pty.
– Paul Grinberg Jul 24 '23 at 19:08socat -x -lu -d -d -d pty,rawer,wait-slave,echo=0,link=/tmp/portA pty,rawer,echo=0,link=/tmp/portB