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 8337757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:23:19+00:00 2026-06-09T04:23:19+00:00

I get bytes from the serial port which represents the voltage on my PIC

  • 0

I get bytes from the serial port which represents the voltage on my PIC board.
But I can’t convert these bytes(strings) to decimal because I get the error message above.
Here is the function(in fact, it’s associated with tkinter button)

def channel8():
    ser.write(chr(0xFF))
    print "you have select channel8"
    x=ser.read(2)
    w=int(x, 16)
    print w
    print "Voltage on channel8 is:" , x

ValueError: invalid literal for int() with base 16: ‘\x0e\xa3’

def channel8():
    ser.write(chr(0xFF))
    print "you have select channel8"
    x=ser.read(2)
    z=struct.unpack("h", x)
    #w=int(z, 16)
    print z

and i get this :

Voltage on channel8 is: (28942,)

can you please explain how did i get this value? it’s not matching anything 😀

  • 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-09T04:23:21+00:00Added an answer on June 9, 2026 at 4:23 am

    I think you should use struct module and unpack your binary data like this:

    struct.unpack("h", x)
    

    Because int is not really for working with binary data, but with hexadecimal strings like: EF1D.

    When you did x=ser.read(2) you received two bytes of binary data, there are two types of number representation supported by struct library: short(h) and unsigned short(H). Function struct.unpack receives two argument:

    • structure specification (a string of format characters)
    • binary data

    and returns a tuple with unpacked values(only one int in your case).

    So you need to change string w=int(x, 16) to w = struct.unpack("h", x)[0] or to w = struct.unpack("H", x)[0], it depends on data type.

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

Sidebar

Related Questions

how can I get 16 bytes binary form of the uuid from its string/canonical
How can I get the size in bytes of the data returned from the
I need to know how to get an array of bytes from a loaded
I know that in J2ME I can get a byte[] object from a String
We use an embedded device to send packets from a serial port over a
I have a class called SerialClient that sends/receives bytes over a serial port. When
I am receiving data from a device that's sending information over the serial port
I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:
Basically, I'm looking for .NET's BitConverter . I need to get bytes from String,
basically i am reading image bytes from file i have a loop which has

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.