我对Python和编程还不熟悉。我试图用pyserial编写一个设备驱动程序。我打开了一个线程,可以从设备读取数据并将其发送到std-out。在我的主循环中,我使用了一个函数,它将std中的指令作为字符串读取,并使用字典将它们写入设备。在
我的程序正在读取我的指令,但没有显示任何应该从设备中输出的数据-我知道它会写入设备,因为当我使用不在字典中的指令时,它会崩溃。下面是我的代码结构:
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)
多线程对象的正确处理方式是什么?在
你可以这样做:
相关问题 更多 >
编程相关推荐