Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5980825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:51:24+00:00 2026-05-22T21:51:24+00:00

I am trying to receive data using serial communication in Python, which I can

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:51:24+00:00Added an answer on May 22, 2026 at 9:51 pm

    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]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send data using the AsyncUDPSocket class. And I can send
I've been trying to do TCP communication using my Wavecom Fastrack modem. What I
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
I have a DataGridView. Some of the cells receive their data from a serial
I'm trying to create a stream of data to the browser using websocket. The
I'm trying to build a C++ extension for python using swig. I've followed the
The issue I am trying to resolve is the following: The C++ serial code
I've been trying to get flash to receive TCP messages from a small Serial
I'm writing a serial port software for Windows. To improve performance I'm trying to
I'm currently using Twisted 10.1 to receive and parse UDP packets, but the standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.