I'm using Windows 7, python 2.7.6, and pyserial 2.7, and talking to multiple Teensy 3.0 boards on different COM Ports.
First I send a query to the Teensy asking for its model and serial number, but sometimes there is nothing in the return buffer for me to read (so it doesn't satisfy the while self.serial.inWaiting() statement in my code below). My code can run without that check, but usually the next time I try to write data to that port, I get a Write Timeout Error from serialwin32, and this causes my program to quit.
Traceback (most recent call last):
File "E:\test.py", line 163, in <module>
x.send(poweronstring)
File "E:\test.py", line 69, in send
self.serial.write(data)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 295, in write
raise writeTimeoutError
SerialTimeoutException: Write timeout
Then, the next time I run the code after getting the above error, I get a COM Port error when I try to open the same port that had the write Timeout error. I only get this error after the write Timeout issue.
Traceback (most recent call last):
File "E:\test.py", line 138, in <module>
device = Port(types[s], available_com[u])
File "E:\test.py", line 25, in __init__
timeout = 30
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
SerialException: could not open port 'COM3': WindowsError(2, 'The system cannot find the file specified.')
Here is the relevant part of my code:
import os
import time
import serial
import math
class Port:
def __init__(self, name, port):
self.name = name
self.serial = serial.Serial(
port = port,
baudrate = 9600, #apparently when talking to Teensy, it goes at max USB speed
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 30
)
self.clear_buffers()
self.buffer1 = ''
time.sleep(1)
def check(self):
self.serial.write('?'.encode('ascii')) # same as self.serial.write('?')
time.sleep(1)
while(self.serial.inWaiting() > 0):
self.buffer1 += self.serial.read(1).decode('ascii')
if(self.buffer1[6:23] == mac_ids[s]): #mac_ids is my known list of serial numbers
print(self.name + ' ' + self.serial.port)
self.clear_buffers()
time.sleep(0.4)
else:
print('Read error: ' + self.name + ' does NOT match ' + self.serial.port)
self.clear_buffers()
time.sleep(0.4)
def clear_buffers(self):
self.serial.flushInput()
self.serial.flushOutput()
validx = []
s = 0
u = 0
while(s < num_com):
if(u < num_com):
device = Port(types[s], available_com[u]) #types and available coms are known names and ports, like Rx1, Rx2, and COM3, COM4
validx.append(device)
device.check()
u = u + 1
s = s + 1
poweronstring = ('P'.encode('ascii')) # same as ('P')
for x in validx:
x.serial.write(poweronstring)
# I get my writeTimeoutError on these serial.write commands, for the Ports that didn't respond with a serial number above
The weird thing is that, if I don't flush the input and output buffers through pyserial at the end of this section of my code, I get the model and serial numbers the next time I read data from the port that apparently doesn't have anything in it here.
I've tried adding delays after opening and after writing, up to 5 seconds each just to make sure. I added a writeTimeout of 2 to my init, and I still get the writeTimeoutError. This also caused my program to hang sometimes. I check the serial.outWaiting() to be sure that the initial write of '?' wasn't interfering, and the output buffer is empty.
Sorry to be so long-winded, but I want to be thorough. My main question: how can I prevent this writeTimeoutError? And also: why am I not getting a response from my Teensy, and why does it show up in the next read instead? I really appreciate any help or insight you can give!
Does it matter if I'm using a 64-bit OS? I just heard that pyserial only works with 32-bit, is that true?
EDIT: If I set the writeTimeout = 0 for each port, then I don't get the write timeout error. However, I'm still having the issue where I don't see anything in the return buffer from the Teensy, and now my program is hanging later on. After this, I get the COM Port error quoted above on the next time running the code.