在 Tkinter 文本框中显示时,发出处理从串行端口读取的数据 [英] Issue processing data read from serial port, when displaying it in a Tkinter textbox

查看:18
本文介绍了在 Tkinter 文本框中显示时,发出处理从串行端口读取的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在从串行连接读取(并用 tkinter 文本框显示)数据,但我无法按照我的意愿处理返回的数据,以便运行我的测试.更简单地说,即使显示了 machine response = 0x1,我也无法从全局 serBuffer 中读取它.

So i am reading (and displaying with a tkinter textbox) data from a serial connection, but i can't process the returning data as i would like to, in order to run my tests. In more simple terms, even though the machine response = 0x1 is displayed, i can't read it from the global serBuffer.

在将它显示到 textbox 之前,我会从测试 function 内部读取,然后检查响应是否在 string 中,但是现在我将读取的数据(字符串)传递给一个全局变量,然后尝试读取它,它似乎不起作用,除非我从 readserial<中删除 serBuffer = ""/代码>.但这会导致一个新问题.当我按下按钮发送命令时,它会发送它,但仅在我第二次按下它之后才会收到响应,之后每次都收到响应.因此,如果我运行一次测试,我会得到 Fail ,但每次都通过.

Before displaying it to the textbox i would read from inside the test function and then check if the response was in the string, but now that i pass the read data(strings) to a global variable and then try to read it, it doesn't seem to work, UNLESS i remove the serBuffer = "" from readserial. That results in a new issue though. When i press the button to send the command it sends it, but only receives the response after the second time i press it, and every time after. So as a result i get a Fail if i run the test once, but i get a pass everytime after.

带有所需响应的图片(测试函数不读取0x1并始终返回FAIL)

Picture with the desired response ( that the test function doesn't read 0x1 and always returns FAIL)

带有非期望响应的图片(只有在第二次按下它之后才会收到响应,之后每次都收到响应.因此,如果我运行一次测试,我会得到一个失败,但我每次都通过).

Picture with the non-desired response ( that only receives the response after the second time a press it, and every time after. So as a result i get a Fail if i run the test once, but i get a pass every time after).

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()

推荐答案

我做了一些更改,请检查这个.

I made some changes, check this one.

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)  

这篇关于在 Tkinter 文本框中显示时,发出处理从串行端口读取的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆