通过在Python GUI pyserial和显示接收多个值 [英] Receive multiple values via pyserial and display in Python GUI

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

问题描述

我想接收使用Python中,这是我能做到的串行通信数据,但我需要改善我的code。

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的位置值。在这个包,我有与&作为startbyte,而新换行/ 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的价值观?它也可能是值得注意的是,这些值会发生变化,它们将变化到传感器从阿尔杜伊诺发送值。

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.

下面是code我现在,我用它来收到,没有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 。这code从声纳接收一个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.

更新

<通过下面的评论者回答>。
这里是正确的code,做的工作。

<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]

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

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