I am new to Python and programming in general. I am trying to write a device driver using pyserial. I opened a thread that would read data from the device and send it to std out. On my main loop I used a function that would read instructions from std in as strings and write them to the device using a dictionary.
My program is reading my instructions but is not displaying any data that should be coming out of the device - I know its writing to the device because it crashes when I use an instruction not in my dictionary. Here is how my code is structured:
import serial
import threading
#ser is my serial object
def writeInstruction(ser):
#reads an instruction string from stdin and writes the corresponding instruction to the device
instruction = raw_input('cmd> ')
if instr == 'disable_all': defaultMode(ser)
else: ser.write(dictionaryOfInstructions[instruction])
time.sleep(.5)
def readData(ser):
# - Reads one package from the device, calculates the checksum and outputs through stdout
# - the package content (excludes the Package head, length, and checksum) as a string
while True:
packetHead = binascii.hexlify(ser.read(2))
packetLength = binascii.hexlify(ser.read(1))
packetContent = binascii.hexlify(ser.read(int(packetLength, 16) - 1))
if checkSum(packetHead + packetLength + packetContent):
print packetContent
readThread = threading.Thread (target = readData, args = ser)
readThread.start()
while True:
writeInstr(ser)
What is the proper way to handle serial objects in multi-threaded programming?