I am trying to access one serial port (
/dev/ttyACM0) on my Rasbperry with two Python scripts, which obviously doesn't work because one blocks the other. The thing is that one script only needs to read from the serial port, while the other only writes to it.
I can't combine the two scripts because the write-script only gets executed every so often to send some commands, and the read-script needs to log occasional messages from the other device (thus needs to be listening all the time).
I tried to write a third Python program, which acted as a relay between the scripts and the single serial port. It would accomendate the serial port and write the incoming data from the device to a virtual port (
/dev/ttyACM0_read), from which the read-script would read. The write-script would write to a second virtual port (
/dev/ttyACM0_write), from where the relay programm reads the data and writes it to the serial port.
This failed when I was unable to create the virtual ports (
"could not open port, no such file or directory"). Searches on creating virtual serial ports brought up nothing useful.
While searching I came across
socat, but I couldn't make out if it was of any use in this case, as it (as far as I understood) can only create a connection between two ports.
So, maybe someone can tell me how to create a virtual port so I can use it with my relay python program, or one comes up with a different solution on the one-port-two-scripts-dilemma I am in.
Platform info: Linux armv6l stretch v9
Update: To clarify my usecase, here are the python scripts I use:
write-script - when calling an URL (
hostname.local/foo)on the Raspberry, the serial port is opened briefly and a command is send.
import web import serial urls = ( '/foo', 'foo', ) class foo: def GET(self): ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1) ser.write("<311>") ser.close() return "Success" if __name__ == "__main__": app = web.application(urls, globals()) app.run()
read-script - Catches incoming data from the serial port and writes it to a file.
import serial ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1) while 1 #If serial data is available, write it to txt file ser.close()
There is no specific reason for using an virtual port, I just thought that this would be the simpliest solution.
As @meuh pointed out in the comments, unsetting exclusive mode on the port would work, but I was unable to achive that using
pi@pizero:~ $ setserial /dev/ttyACM0 ^session_lockout Cannot set serial info: Operation not supported pi@pizero:~ $ sudo setserial /dev/ttyACM0 ^session_lockout pi@pizero:~ $
After this, nothing changed and the second python script returned EBUSY (Device or resource busy) as before.