Dee*_*zer 6 python tkinter pyserial python-3.x
因此,我正在从串行连接中读取(并使用tkinter文本框显示)数据,但是为了运行测试,我无法按自己的意愿处理返回的数据。用更简单的术语来说,即使machine response = 0x1显示了,我也无法从global读取它serBuffer。
在将其显示给textboxi 之前,我将从测试内部进行读取function,然后检查响应是否在中string,但是现在我将读取的数据(字符串)传递给全局变量,然后尝试读取它,似乎没有工作,除非我serBuffer = ""从中删除了readserial。但是,这导致了一个新问题。当我按下按钮发送命令时,它将发送该命令,但仅在第二次按下后以及每次之后才收到响应。因此,Fail如果我运行一次测试,我会得到一个结果,但是之后我每次都会通过。
带有所需响应的图片(test function不读取0x1并始终返回FAIL)
带有不期望的响应的图片(仅在第二次按下后以及每次之后才收到响应。因此,如果我运行一次测试,则结果为“失败”,但之后每次均得到通过) 。
import tkinter as tk
import serial
from serial import *
serialPort = "COM3"
baudRate = 115200
ser = Serial(serialPort, baudRate, timeout=0, writeTimeout=0) #ensure non-blocking
#make a TkInter Window
mainWindow = tk.Tk()
mainWindow.wm_title("Reading Serial")
mainWindow.geometry('1650x1000+500+100')
scrollbar = tk.Scrollbar(mainWindow)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
log = tk.Text ( mainWindow, width=60, height=60, takefocus=0)
log.pack()
log.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=log.yview)
#make our own buffer
#useful for parsing commands
#Serial.readline seems unreliable at times too
serBuffer = ""
ser.write(b'\r\n')
def readSerial():
while True:
c = (ser.read().decode('utf-8', 'ignore')) # attempt to read a character from Serial
# was anything read?
if len(c) == 0:
break
# get the buffer from outside of this function
global serBuffer
# check if character is a delimeter
if c == '\r':
serBuffer += "\n" # don't want returns. chuck it
if c == '\n':
serBuffer += "\n" # add the newline to the buffer
# add the line to the TOP of the log
log.insert('1.1', serBuffer)
serBuffer = "" # empty the buffer
else:
serBuffer += c # add to the buffer
mainWindow.after(100, readSerial) # check serial again soon
def test():
command = b" test command \r\n"
ser.write(command)
global serBuffer
time.sleep(0.5)
if "0x1" in serBuffer:
print('PASS')
return 'PASS'
else:
print('FAIL')
return 'FAIL'
button = tk.Button(mainWindow, text="Pone Test", font=40, bg='#b1c62d', command=test)
button.place(relx=0.8, rely=0, relwidth=0.1, relheight=0.05)
# after initializing serial, an arduino may need a bit of time to reset
mainWindow.after(100, readSerial)
mainWindow.mainloop()
Run Code Online (Sandbox Code Playgroud)
注释:但仅在第二次按下后以及每次之后才收到响应。因此,如果我运行一次测试,我会失败,但是之后每次都通过
提高第一次超时时间,100以提高500矿石利用率。
# after initializing serial, an arduino may need a bit of time to reset
mainWindow.after(100, self.readSerial)
Run Code Online (Sandbox Code Playgroud)
要找出第一个响应的延迟,请尝试以下操作:
注意:您必须在不运行的情况下执行此操作
def readSerial,以防止同时出现in buffer“
command = b" test command \r\n"
self.ser.write(command)
delay = 0.0
# wait until you get `.in_waiting` data.
while not self.ser.in_waiting:
time.sleep(0.1)
delay += 0.1
print('.', end='')
if delay >= 10:
print('BREAK after {} no in_waiting'.format(int(delay * 10)))
break
print('Delay:{}, in_waiting:{}'.format(delay, self.ser.in_waiting))
Run Code Online (Sandbox Code Playgroud)
以下对我有用。
注意:我使用
OOP语法。
last_command
serBuffer = ""
last_command = None
Run Code Online (Sandbox Code Playgroud)复制就绪read_buffer到last_command,仅空read_buffer
def readSerial(self):
while True:
c = (self.ser.read().decode('utf-8', 'ignore')) # attempt to read a character from Serial
# was anything read?
if len(c) == 0:
break
# get the buffer from outside of this function
global serBuffer
# check if character is a delimeter
if c == '\r':
serBuffer += "\n" # don't want returns. chuck it
if c == '\n':
serBuffer += "\n" # add the newline to the buffer
global last_command
last_command = serBuffer
# add the line to the TOP of the log
# log.insert('1.1', last_command)
print('readSerial.last_command:"{}"'.format(bytes(last_command, 'utf-8')))
serBuffer = "" # empty the buffer
else:
serBuffer += c # add to the buffer
print('readSerial:"{}"'.format(bytes(serBuffer, 'utf-8')))
self.after(100, self.readSerial) # check serial again soon
Run Code Online (Sandbox Code Playgroud)做 test()
def test(self, write=True):
print('test(write={})'.format(write))
if write:
command = b" test command \r\n"
self.ser.write(command)
self.after(500, self.test, False)
elif last_command is not None:
print('last_command:{}'.format(bytes(last_command, 'utf-8')))
if "0x1" in last_command:
print('PASS')
else:
print('FAIL')
else:
# ATTENTION: This could lead to a infinit loop
# self.after(500, self.test, False)
pass
Run Code Online (Sandbox Code Playgroud)输出:
Run Code Online (Sandbox Code Playgroud)test(write=True) readSerial:"b' '" readSerial:"b' t'" readSerial:"b' te'" readSerial:"b' tes'" readSerial:"b' test'" readSerial:"b' test '" readSerial:"b' test c'" readSerial:"b' test co'" readSerial:"b' test com'" readSerial:"b' test comm'" readSerial:"b' test comma'" readSerial:"b' test comman'" readSerial:"b' test command'" readSerial:"b' test command '" readSerial:"b' test command \n\r'" readSerial.last_command:"b' test command \n\r\n'" test(write=False) last_command:b' test command \n\r\n' FAIL注意:我得到了
FAIL,因为0x1在last_command使用PORT = 'loop://'哪个回显时没有输入内容!
我做了一些更改,检查这一点。
def readSerial():
while True:
c = (ser.read(1).decode('utf-8', 'ignore')) from Serial
if len(c) == 0:
break
global serBuffer
if c == '\r':
serBuffer += ""
if c == '\n':
serBuffer += "\n"
log.insert(tk.END, serBuffer)
log.see(tk.END)
log.update_idletasks()
serBuffer = ""
else:
serBuffer += c
mainWindow.after(500, readSerial)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161 次 |
| 最近记录: |