I am trying to setup a program with twisted to emulate a serial devices that sends/receives AT commands. Different devices open a different amount of serial ports and use these ports for different things.
I would like my application to be able to open as many SerialPorts as needed and to know what serial Device is writing to dataReceived. I did not want to run each port on a different reactor or thread.
Is there anyway to do this ?
class VirtualDeviceBase(LineReceiver):
def __init__(self, reactor, serial_address):
[...]
def open_port(self):
self.serial_device = SerialPort(self, serial_address, reactor)
self.serial_device2 = SerialPort(? , serial_address, reactor)
def dataReceived(self,data):
[...]
I have tried this:
class VirtualDeviceBase(LineReceiver):
class Protocol(LineReceiver):
def __init__(self,reactor,address):
[...]
def open_port(self):
new_protocol = self.Protocol()
self.serial_device = SerialPort(self, serial_address, reactor)
self.serial_device2 = SerialPort(new_protocol , serial_address, reactor)
and it does not throw any errors but than neither of them call dataRecevied any more.
Just instantiate two SerialPort
s. Give them whatever protocols you like. They can share a reactor. A single reactor can handle many different event sources.
from twisted.internet.protocol import Protocol
from twisted.internet.serial import SerialPort
from twisted.internet.task import react
class Echo(Protocol):
def dataReceived(self, data):
print("Received: {}".format(data))
def main(reactor):
SerialPort(Echo(), "com0", reactor)
SerialPort(Echo(), "com1", reactor)
react(main, [])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.