是否可以使用 PySerial 实现全双工通信?具体来说,是否可以连续监控端口的输入并在需要时写入?我想应该可以使用线程(串行接口(interface)是全双工的,不是吗?)。如果不是,那么在不传输时监视串行端口的最佳方法是什么?超时?
编辑:这是我的尝试。此代码针对 TI 的 CC2540 蓝牙 LE 芯片。在发送 GATT 初始化消息时,我希望得到回复(详细说明芯片的操作参数)...不过我什么也没得到
import serial
import threading
from time import sleep
serial_port = serial.Serial()
GAP_DeviceInit = \
"\x01\x00\xfe\x26\x08\x03\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00"
def read():
while True:
data = serial_port.read(9999);
if len(data) > 0:
print 'Got:', data
sleep(0.5)
print 'not blocked'
def main():
serial_port.baudrate = 57600
serial_port.port = '/dev/ttyACM0'
serial_port.timeout = 0
if serial_port.isOpen(): serial_port.close()
serial_port.open()
t1 = threading.Thread(target=read, args=())
while True:
try:
command = raw_input('Enter a command to send to the Keyfob: \n\t')
if (command == "1"):
serial_port.write(message)
except KeyboardInterrupt:
break
serial_port.close()
最佳答案
是的,串口硬件是全双工的。是的,你可以用线程同时做Rx和Tx。或者,您可以使用一个单线程循环,该循环以较短的超时时间进行读取并在读取和写入之间交替。
关于python - PySerial - 全双工通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487151/