I have an application where I want to simulate the connection between a device and a \"modem\". The device will be connected to a serial port and will talk to the software m
Here's pythonic version of pts-emulated (caf's) serial communication:
from serial import Serial
driver = MyDriver() # what I want to test
peer = serial.Serial()
driver.port.fd, peer.fd = posix.openpty()
driver.port._reconfigurePort()
peer.setTimeout(timeout=0.1)
peer._reconfigurePort()
driver.start()
# peer.write("something")
# driver.get_data_from_serial()
It has some advantages over mocking Serial, namely that Serial code is used and some serial port artefacts are exercised.
If you want to test opening of serial ports, you could swap master and slave around and use os.ttyname(salve_fd)
as serial port name. I can't vouch for side-effects of swapping master and slave around though. Most notable is that you can close and reopen slave, but fi you close master slave dies too.
This works like a charm if your test code runs within same process. I didn't iron out the kinks with multiple/separate processes yet.