我们通过串行连接与远程设备连接 . 远程设备发送心跳字符 - 发送下划线“". On the rasbperry pi tkinterface is running a GUI application. A thread has been created to handle the serial communications. The " " is sent every 2 tenths of a second. When the thread recognizes that the " _”,线程可以发送命令 . 如果时机正确,则该过程看起来应该是这样的 . .
是否可以在收到响应时产生UART中断,以免给处理指令循环的进程带来负担 .
这是线程中的代码
Cfg.RSdata = 's' # poll for status updates
Cfg.PIC_CMD = Cfg.PIC_Ver
while self.port.isOpen():
RS485read = self.port.read()
RS485read = RS485read.decode('utf-8', "ignore")
if RS485read != "":
if RS485read == Cfg.HeartBeat: # Rx _ heartbeat
'''
after the heart beat see if there is data to be sent
if Cfg.RSdata is not empy then send the data
'''
if Cfg.RSdata != None:
GPIO.output(23, GPIO.HIGH) # set pin High to write
#print('sending character', Cfg.RSdata)
Serial_Out(Cfg.RSdata.encode('utf-8'))
if Cfg.PIC_CMD == Cfg.PIC_Stop: # X
Cfg.RSdata = 'X' # poll for status updates
Cfg.PIC_CMD = Cfg.PIC_Ver
if Cfg.PIC_CMD == Cfg.PIC_Run: # Y
Cfg.RSdata = 'Y' # poll for status updates
Cfg.PIC_CMD = Cfg.PIC_Ver
#time.sleep(.0002)
GPIO.output(23, GPIO.LOW) # set pin low to read
elif ord(RS485read) != 0:
'''
if we did not receive a heart beat character or a null
then we need to process the response received
'''
x=0
if Cfg.PIC_CMD == Cfg.PIC_Ver: # ?
#print('?')
Cfg.PIC_Ver_Rx = []
cnt = Cfg.PIC_Ver_len
for x in range(0, cnt):
Cfg.PIC_Ver_Rx.append(RS485read)
RS485read = self.port.read()
RS485read = RS485read.decode('utf-8', "ignore")
Cfg.RSdata = 's' # poll for status updates
Cfg.PIC_CMD = Cfg.PIC_Ver
if Cfg.PIC_CMD == Cfg.PIC_Stat: # s
Cfg.PIC_Stat_Rx = []
cnt = Cfg.PIC_Stat_len * (Cfg.MotorControlModules + 1)
for x in range(0, cnt):
Cfg.PIC_Stat_Rx.append(RS485read)
RS485read = self.port.read()
RS485read = RS485read.decode('utf-8', "ignore")
Status_Processing(Cfg.PIC_Stat_Rx)