4
votes

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()
1
some serial devices require a null modem adapter (i've used power supplies that won't work unless used w/ a null modem cable) have you checked this yet?pyInTheSky
yes the hardware setup is correct since my setup works when I use a C application in place of my python script. It seems that I'm missing something in my script.Baz
Why is there a VCOM3? How are VCOM2 and VCOM3 connected? I would expect HW <> COM1 <> Script <> VCOM2 <> SIMULATORcircus
I create a pair between these two using Virtual Serial Ports Emulator.Baz

1 Answers

2
votes

you cannot ignore the signals, especially if you use HW based flow-control. I assume that you at least have to link the following signals between both ports besides RX(read) and TX(write)

CTS -> RTS
DSR -> DTR

Comments about your code:

  1. You defined a timeout, this means the IO function will block (even if it is only for a short amount of time). Consider setting it to 0

  2. You call read without parameter. This will only read a single byte. Any reason why you do not want to read more at once? Would reduce the overhead.

  3. Consider adding a condition to exit the while loop. At the moment the code will run until pyserial throws an exception.