PySerial을 사용하여 전이중 통신을 수행 할 수 있습니까? 특히 필요할 때마다 입력 및 쓰기 포트를 지속적으로 모니터링 할 수 있습니까? 스레드 (및 직렬 인터페이스는 전이중이 아니오)를 사용하여 가능해야한다고 생각합니다. 그렇지 않다면, 전송하지 않을 때 직렬 포트를 모니터링하는 최선의 방법은 무엇입니까? 시간 초과?PySerial - 전이중 통신
편집 : 여기에 그것의 시도가 있습니다. 이 코드는 TI의 CC2540 Bluetooth 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()
간단한 예를 들려 주시겠습니까? 또는 제공된 코드 샘플과 관련된 즉각적인 문제를 나타낼 수도 있습니다. 두 번째 접근법의 문제점은 언제 데이터를 전송해야하는지 알 수 없다는 것입니다. 사용자는 언제든지 보낼 메시지를 선택할 수 있습니다. – stephenfin
읽은 스레드를 시작하지 않았습니다. 't1.start()'를 추가하십시오 – TJD
Doh! 매우 감사합니다! – stephenfin