Python Forum
Multiple process access to a serial port
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple process access to a serial port
#1
Hi all,


I need to implement the multiple process access to a serial port.
Do anybody implemented that ?

My code call will look like the following:
[bash]
python3 xbee_process_1.py &
python3 xbee_process_2.py &
python3 xbee_process_3.py &
[/bash]

For now, without of multiprocess support, the single process looks like this:
from digi.xbee.devices import XBeeDevice
xbee = XBeeDevice('/dev/ttyUSB0', 9600)
xbee.open()
Obviously, when I'm using this code in each process, they start to interfere with each other and see the different serial port errors.
I know that XBeeDevice contains the XBeeSerialPort as one of its dependencies and contains ._serial_port instance as a descriptor for the serial port.
So I'm going to implement my own serial port instance with necessary process locks and rewrite the ._serial_port instance:

from xbee_device_wrapper import xbee_device_wrapper
xbee = xbee_device_wrapper('/dev/ttyUSB0', 9600)
from digi.xbee.devices import XBeeDevice
from digi.xbee.serial import XBeeSerialPort
import traceback

class serial_port_proxy(XBeeSerialPort):
    def __init__(self, baud_rate, port, **kwargs):
        super().__init__(baud_rate, port, **kwargs)  

    def open(self):
        print("\n-----------", traceback.extract_stack())
        return super().open()

class xbee_device_wrapper(XBeeDevice):
    def __init__(self, port, baud_rate):

        super().__init__(port, baud_rate)
        self._serial_port = serial_port_proxy(baud_rate, port)
        self.open()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  MCU reboots after opening Serial port when ran from Raspberry PI zazas321 3 327 Mar-19-2024, 09:02 AM
Last Post: zazas321
  Waiting for input from serial port, then move on KenHorse 2 832 Oct-17-2023, 01:14 AM
Last Post: KenHorse
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,306 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Serial Port As Global Prasanjith 2 1,420 Mar-23-2023, 08:54 PM
Last Post: deanhystad
  python serial port barryjo 2 1,601 Dec-27-2021, 11:09 PM
Last Post: barryjo
  Run multiple process using subprocess Shiri 3 3,729 Nov-28-2021, 10:58 AM
Last Post: ghoul
  Process multiple pdf files Spartan314 1 1,297 Oct-27-2021, 10:46 PM
Last Post: Larz60+
  is there a way to mention port range or search for port dynamically with qConnection Creepy 0 1,449 Sep-09-2021, 03:15 PM
Last Post: Creepy
  How to Properly Open a Serial Port in a Function bill_z 2 4,350 Jul-22-2021, 12:54 PM
Last Post: bill_z
Question Python3 - serial port reload parovelb 4 5,698 Apr-08-2021, 09:18 AM
Last Post: parovelb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020