I've got a Python program which is reading data from a serial port via the PySerial module. The two conditions I need to keep in mind are: I don't know how much data will arrive, and I don't know when to expect data.
Based on this I have came up with the follow code snippets:
#Code from main loop, spawning thread and waiting for data s = serial.Serial(5, timeout=5) # Open COM5, 5 second timeout s.baudrate = 19200 #Code from thread reading serial data while 1: tdata = s.read(500) # Read 500 characters or 5 seconds if(tdata.__len__() > 0): #If we got data if(self.flag_got_data is 0): #If it's the first data we recieved, store it self.data = tdata else: #if it's not the first, append the data self.data += tdata self.flag_got_data = 1
So this code will loop forever getting data off the serial port. We'll get up to 500 characters store the data, then alert the main loop by setting a flag. If no data is present we'll just go back to sleep and wait.
The code is working, but I don't like the 5s timeout. I need it because I don't know how much data to expect, but I don't like that it's waking up every 5 seconds even when no data is present.
Is there any way to check when data becomes available before doing the read
? I'm thinking something like the select
command in Linux.
Note: I found the inWaiting()
method, but really that seems it just change my "sleep" to a poll, so that's not what I want here. I just want to sleep until data comes in, then go get it.
PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on. It also supports remote serial ports via RFC 2217 (since V2. 5).
Yes serial port hardware is full duplex. Yes, you can use threads to do Rx and Tx at the same time. Alternatively, you can use a single thread loop that does reads with a short timeout and alternates between reading and writing.
To check that it is installed, start Pyzo and at the command prompt type in: import serial If it just gives you another >>> prompt, all is good.
Ok, I actually got something together that I like for this. Using a combination of read()
with no timeout and the inWaiting()
method:
#Modified code from main loop: s = serial.Serial(5) #Modified code from thread reading the serial port while 1: tdata = s.read() # Wait forever for anything time.sleep(1) # Sleep (or inWaiting() doesn't give the correct value) data_left = s.inWaiting() # Get the number of characters ready to be read tdata += s.read(data_left) # Do the read and combine it with the first character ... #Rest of the code
This seems to give the results I wanted, I guess this type of functionality doesn't exist as a single method in Python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With