通过 pyserial 接收多个值并在 Python GUI 中显示
我正在尝试使用 Python 中的串行通信接收数据,我可以做到,但我需要改进我的代码。
我正在从 Arduino 发送一个“数据包”,其格式为“&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()
感谢您的精彩而简单的回答!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试拆分文本:
对于上面的示例,
split_data
将包含['4', '25']
。然后您可以像这样访问数据:
Try splitting the text:
split_data
will contain['4', '25']
for the example above.You can then access the data like so: