Sharing serial port between Node-Red & Python

Im using Node-Red to send binary data to a serial device (RS232), I also have a Python script which is executed using the pythonshell node which is sending binary data to the same port (At different times)

I’m getting an error when I execute the Python script “Device disconnected or multiple access on port”

When I stop Node-red and run the script I don't get the error.

Is there a way to implement the above flow so the two processes can access the serial port at different times? For example, if i inject the pythonshell node it disables Node-red sharing the port?

The only solution I can think of is to convert the Python script to Java and run the process in Node-red? However I don't know how to convert between programming languages.

I’m new to code so any help/suggestions would be greatly appreciated.

I don't think there is a way to get node-red to release the port. A solution might be to modifiy the python script so that instead of sending the port it returns the data that needs to be sent. Then run this from an exec node in node-red, capture the output and pass it to the port.

Serial ports are not designed to allow multiple processes to access them at the same time. Indeed, generally the drivers wont allow it.

If Colin's suggestion isn't enough, you can get drivers that give you a virtual serial port which can be shared. However, you need to understand the limitations.

https://www.startpage.com/do/dsearch?query=how+to+create+a+shared+serial+port+on+linux&cat=web&pl=opensearch&language=english

[quote="Colin, post:2, topic:20329"]
exec node
[/quote] Thanks for your suggestion Colin! Not sure it will work for this purpose. The Python script will send binary data to the serial device, the serial device then sends a response back which the Python script will process. Depending on the response from the device the script then sends a new command to the device. So the data that’s sent to the Serial device changes (It’s not always the same command, for example there’s a function to count hashed password in the script).
Probably a silly question but Is there a way to modify the Python script to replace the “Import serial” function so it sends the binary data from the Python script directly the serial node (Or any node) in Node-red instead of the serial port? then the response from the device is sent to the script for it to process?

Not silly at all. Though you wouldn't use NR's serial node for that but rather one of the other communications nodes such as the TCP or UDP nodes. Or you could use the websocket node with a suitable Python websocket library. Or you could create a web API in Node (various ways, the http-in/out nodes being the simplest) and call that from Python.

Somewhat depends on the volume and type of data you want to transfer.

If you were to go down this route, I’ve good experiences with Quart, a Python async based library that allows/supports websockets. Documentation is up to par with its primary inspiration Flask too. Though keep in mind it sets the websocket on the Python side and you would use the websocket request in Node-RED rather than the other way around.

https://pgjones.gitlab.io/quart/