本文介绍了通过 pyserial 接收多个值并在 Python GUI 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 中的串行通信接收数据,我可以这样做,但我需要改进我的代码.

I am trying to receive data using serial communication in Python, which I can do, but I need to improve my code.

我正在从 Arduino 发送一个数据包",格式为&4,25/n",关键因素是4"和25"位置的值.在这个数据包中,我有&"作为起始字节,新换行符/n"作为终止符.这样我就可以知道什么时候发送了一个新的数据包,它结束了.

I am sending a "packet" from Arduino that is in the form of "&4,25/n" with the key factors being the values in the positions of "4" and "25". In this packet, I have the "&" as a startbyte, and the new line feed "/n" as a terminator. This is so that I can tell when a new packet is sent, and it ends.

我如何接收这个数据包&4,24/n"并提取4,24"位置的值?还值得注意的是,这些值会发生变化,它们会因 Arduino 发送的传感器值而异.

How can I receive this packet "&4,24/n" and extract the values that are in the locations that "4,24"? It may also be worth noting that the values will change, they will vary to sensor values sent from the Arduino.

这是我现在拥有的代码,我用来接收单个值,没有 startbyte,使用新的换行来终止数据包.

Here is the code I have right now, that I use to receive one single value with, no startbyte, uses the new line feed to terminate the packet.

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
from PythonCard import model
class MainWindow(model.Background):
    def on_SetSpdBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
    def on_FwdBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('F')
        ser.write(chr(spd))
    def on_LftBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('L')
        ser.write(chr(spd))
    def on_RitBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('R')
        ser.write(chr(spd))
    def on_RvsBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('B')
        ser.write(chr(spd))
    def on_StpBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('S')
        ser.write(chr(spd))
    def on_GetPing_mouseClick(self, event):
        ser.write('~')
        ser.write('P1')
        ser.write('p2')
        retval = ser.readline()
        ping_data = retval.strip() # strip out the newline
        self.components.PngDis.text = str(ping_data)

app = model.Application(MainWindow)
app.MainLoop()

这与资源文件一起为我提供了一个 GUI 来通过 VNC.此代码从声纳接收一个 ping 值并将其报告给 GUI 以进行显示.我需要为两个不同的传感器显示两个不同的 ping 值.

This, along with a resource file, is giving me a GUI to control my robot remotlely via VNC. This code receives one ping value from a sonar and reports it to the GUI to be displayed. I need two different ping values for two different sensors to be displayed.

更新

这是有效的正确代码.

<Answered by below commenter.>Here is the correct code that does work.

import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
from PythonCard import model
class MainWindow(model.Background):
    def on_SetSpdBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
    def on_FwdBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('F')
        ser.write(chr(spd))
    def on_LftBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('L')
        ser.write(chr(spd))
    def on_RitBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('R')
        ser.write(chr(spd))
    def on_RvsBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('B')
        ser.write(chr(spd))
    def on_StpBtn_mouseClick(self, event):
        spd = self.components.SpdSpn.value
        ser.write('@')
        ser.write('S')
        ser.write(chr(spd))

    def on_GetPing_mouseClick(self, event):
        ser.write('~')
        ser.write('P1')
        ser.write('p2')
        retval = ser.readline()
        ping_data = retval.strip() # strip out the newline, if you read an entire line
        split_data = ping_data.split(',')
        L_Ping = split_data[0]
        R_Ping = split_data[1]
        self.components.PingLeft.text = str(L_Ping)
        self.components.PingRight.text = str(R_Ping)

app = model.Application(MainWindow)
app.MainLoop()

感谢您提供出色而简单的答案!

Thanks for a great and simple answer!

推荐答案

尝试拆分文本:

split_data = ping_data.split(',')

对于上述示例,

split_data 将包含 ['4', '25'].
然后您可以像这样访问数据:

split_data will contain ['4', '25'] for the example above.
You can then access the data like so:

first_val = split_data[0]
second_val = split_data[1]

这篇关于通过 pyserial 接收多个值并在 Python GUI 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!