2 years ago
#76788

RantWhy
Detecting python serial read timeout or serial interruption
I am making a program to talk to a piece of serial equipment. The code I have so far allows me to talk to the device just fine. However I would like to have the program do alert the user if the link does dead, along with triggering a different section of code. Ideally I would like the code to be able to detect a interruption in any part in the transmission.
I intend to have this code used with kivy with a user interface to indicate link status.
I have read about people using threading, and I have tried making timers with threading. But I think it all just gets messy.
Thank you for any input.
import serial
import time
# lineread is the variable to store the data read from comport
# b"\x80" is the format to read and write hex values this = 80 in hex
SerialConfig = serial.Serial(port='COM5', baudrate=9600, parity=serial.PARITY_EVEN, timeout=.1) # comport configurations
def Start_loop():
while True: # loop forever
lineread = SerialConfig.read() # lineread variable is = to serial configurations reading from com port
if lineread == b"\x80": # wait for device to start handshake
SerialConfig.write(b"\x80") # reply to device with handshake
del lineread # clear lineread variable to wait for next value to come in
while True: # keep looping till
lineread = SerialConfig.read() # lineread variable is = to serial configurations reading from com port
if lineread == b"\x80": # if comport sees reply respond with the following lines
SerialConfig.write(b"\xA0")
SerialConfig.write(b"\x90")
print("handshake complete")
python
serial-port
pyserial
0 Answers
Your Answer