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

  • SEARCH
  • Home
  • 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 8709141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:17:51+00:00 2026-06-13T04:17:51+00:00

When i send a command over serial, the slave responds with a hexidecimal sequence,

  • 0

When i send a command over serial, the slave responds with a hexidecimal sequence, i.e.:

this series:

05 06 40 00 02 05 F6 5C

gives me

05 07 40 05 02 05 71 37 FF

The response always ends with the FF byte. So i want to read the bytes into a buffer untill i encounter FF. Than the buffer should be printed and the function should return.

import serial

s_port = 'COM1'
b_rate = 2400

#method for reading incoming bytes on serial
def read_serial(ser):
    buf = ''
    while True:
        inp = ser.read(size=1) #read a byte
        print inp.encode("hex") #gives me the correct bytes, each on a newline
        buf = buf + inp #accumalate the response
        if 0xff == inp.encode("hex"): #if the incoming byte is 0xff
            print inp.encode("hex") # never here
            break
    return buf   

#open serial
ser = serial.Serial(
    port=s_port,
    baudrate=b_rate,
    timeout=0.1
)

while True:

    command = '\x05\x06\x40\x00\x02\x05\xF6\x5C' #should come from user input
    print "TX: "
    ser.write(command)
    rx = read_serial(ser)
    print "RX: " + str(rx)

gives me:

TX: 
05
07
40
05
02
05
71
37
ff

Why is the condition never met?

  • 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-06-13T04:17:52+00:00Added an answer on June 13, 2026 at 4:17 am

    It’s because you’re comparing apples and oranges. inp.encode("hex") returns a string. Let’s say you read the letter "A". "A".encode("hex") returns the string "41" and 0x41 != "41". You should either do:

    if '\xff' == inp:
        ....
    

    Or, convert inp into a number using ord():

    if 0xff == ord(inp):
        ....
    

    Then it should work as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this test code to send HELLO over a USB serial port:
I have to send a command over serial and receive back an answer based
I've got a global command like this !i::Send {Up} (pressing alt+I will move the
I'm trying to send a sms via a Nokia phone over serial which is
Currently our application connects to an Arduino over a serial port. We send some
I'm trying to send a command to a device over my Bluetooth RFCOMM socket
I'm writing a little application for osx to listen and send commands over a
I have a class the communicates with a device over TCP. I send commands,
I want to send a command to a server, and find out if I
I like the python-send-buffer command, however I very often use Python embedded in applications,

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.