通过pyserial接收多个值并显示在Python GUI中

| 我正在尝试使用Python进行串行通信来接收数据,但是我需要改进代码。 我正在从Arduino发送一个\“ packet \”,其形式为\“&4,25 / n \”,关键因素是\“ 4 \”和\“ 25 \”位置中的值。在此数据包中,我以\“&\”作为起始字节,以换行符\“ / n \”作为终止符。这样一来,我就可以知道何时发送新数据包,并且该数据包结束了。 如何接收此数据包\“&4,24 / n \”并提取\“ 4,24 \”位置中的值?还可能值得注意的是,这些值将改变,它们将根据Arduino发送的传感器值而变化。 这是我现在拥有的代码,用于接收一个没有开始字节的单个值,它使用换行符终止数据包。
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值才能显示两个不同的传感器。 更新资料 <由以下评论者回答。> 这是起作用的正确代码。
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()
感谢您的简单回答!     
已邀请:
        尝试分割文本:
split_data = ping_data.split(\',\')
上面的示例中,ѭ3contain将包含
[\'4\', \'25\']
。 然后,您可以像这样访问数据:
first_val = split_data[0]
second_val = split_data[1]
    

要回复问题请先登录注册