How to listen to serial port

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Elfers
    New Member
    • Aug 2010
    • 9

    How to listen to serial port

    Hello,

    I need to write a script to listen to a serial port basically forever. The python script will be launched by another program and the input to the com port passed to the other program. As long as this other program is running, I need the python script to run and handle the com port traffic.

    I have this working (thats about it),

    Code:
    import serial
    from scala5 import sharedvars
    import time
    scalavars = sharedvars()
    enable = 1
    
    
    while (enable == 1):
        ser = serial.Serial('COM1', timeout = 3)
        ser.write('ready')
        value = ser.read(10)
        if (value != ''): ser.write(value + '_ok')
        scalavars.command = value
        ser.close()
    but it is not very elegant or proper coding. The biggest problem with it is that the loop tries to open the com port that is already open until the timeout runs out. Any suggestions for cleaning this up?
Working...