I'm using pyserial to open two ports, and then write to each what I read from the other. I then have a physical com port connected to one of these ports and a virtual com port connected to the other. The virtual com port is in turn connected to a second virtual com port to which my simulator connects:
Hardware device <> COM1 <> Python Script <> VCOM2 <> VCOM3 <> Simulator
I can see that the communication is correctly entering and exiting the com ports in my script but something isn't right since the the hardware is failing to communicate correctly with the simulator.
I have an old c app that I can run in place of the Python Script and this works correctly. However, it is really badly written and I've no real interest in fixing all its bugs. So I'm hoping I can replace this app with a python script. I eventually wish to log the data passing through the ports with a timestamp.
I am using the correct baud rate in both cases, however I seem to be missing something. Should I be transferring signals between each port, DTR for example? pyserial has these functions:
sendBreak(duration=0.25)
setBreak(level=True)
setRTS(level=True)
setDTR(level=True)
getCTS()
getDSR()
getRI()
getCD()
What signals am I interested in?
EDIT:
When I poll these values for each port:
getCTS(), getDSR(), getRI(), getCD()
I Get: True, False, False, True COM1 False, False, False, False VCOM2
However, I see that CD becomes false sometimes. How do I transfer this out through VCOM2 or do I need to do this?
EDIT:
Here's my code. Once communication starts the script locks up and I need to restart my computer to release the port. I can't kill the associated python process on Windows 7...
import serial
class NewMonitor():
def __init__(self, com_port_1, com_port_2):
self.read_time_in_seconds = 0.1
self.serialPort1 = serial.Serial(com_port_1, 9600, timeout=self.read_time_in_seconds, rtscts=True, dsrdtr=True)
self.serialPort2 = serial.Serial(com_port_2, 9600, timeout=self.read_time_in_seconds, rtscts=True, dsrdtr=True)
try:
while True:
item = self.serialPort1.read()
self.serialPort2.write(item)
self.serialPort2.setRTS(self.serialPort1.getCTS())
self.serialPort2.setDTR(self.serialPort1.getDSR())
item = self.serialPort2.read()
self.serialPort1.write(item)
self.serialPort1.setRTS(self.serialPort2.getCTS())
self.serialPort1.setDTR(self.serialPort2.getDSR())
finally:
self.serialPort1.close()
self.serialPort2.close()