Receive multiple values via pyserial and display in Python GUI

好久不见. 提交于 2019-12-11 05:21:39

问题


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

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.

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.

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

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.


Update

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


回答1:


Try splitting the text:

split_data = ping_data.split(',')

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]


来源:https://stackoverflow.com/questions/6154341/receive-multiple-values-via-pyserial-and-display-in-python-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!