Python Forum
send hexadecimal package variable UART
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
send hexadecimal package variable UART
#1
Smile 
Hello , guys
i am new using python i would like to send a package of hexadecimal values using the uart the problem it is two of the values are variables i have done a lot of research and i cant find anything it will be really helpful here is my code basically i am trying to send this

Output:
x = b'\x41\x0A\x01\variable\variable'
contition=True
while contition:
  main_input  = int(input("select a number: "))

  if main_input == 13:
   contition == False
   break

  elif main_input == 1:
    light_val = int(input("insert the new threshold value: "))
    a = (1, 10, light_val)
    csum = sum(a)
    csum = hex( csum^ 0xFF)
    print(csum)
    b = hex(light_val)
    c = hex(10)
    d = hex(1)
    e = hex(65)
    y = (e, c, d, b, csum) 
    x = b'\x41\x0A\x01\x05\EF'
    
    #ser.write(x)
    #ser.write(Y)
    print(Y)
  else:
    print(" Not Valid choice")
thanks a lot
Larz60+ write Oct-30-2020, 02:33 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

fixed for you this time. Please use tags on future posts.
Reply
#2
Are you trying to send string (ascii) representations or are you trying to send byte values?

To send a ten, do you want to send a numeric 10, or an ascii "1" followed by an ascii "0"?

The "output" you show above is a bytes object (so numeric), while you have hex() calls all over your code which is creating strings.
Reply
#3
bolofred is correct.

for unsigned bytes:
hexadecimal is the base 16 representation of byte (8 bits) data

decimal is base 10
octal base 8
binary base 2
char is value assigned to first 127 bits of a byte, or all 256 bits if extended ascii is used.
html is decimal value, zero padded, preceeded with &#


Example:
Output:
the ascii string 'cow' ============================================= | chr | hex | dec | oct | html | binary | ============================================= | c | 63 | 99 | 143 | &#099 | 0110 0011 | --------------------------------------------- | o | 6f | 111 | 157 | &#111 | 0110 1111 | --------------------------------------------- | w | 77 | 119 | 167 | &#077 | 0111 0111 | =============================================
Each is a representation of the same byte value.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UART & I2C slow down a loop trix 4 631 Dec-28-2023, 05:14 PM
Last Post: trix
  How to clean UART string Joni_Engr 4 2,503 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  checking for valid hexadecimal digits Skaperen 3 6,414 Sep-02-2021, 07:22 AM
Last Post: buran
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,853 Jul-17-2021, 04:19 PM
Last Post: bill_z
  wrong data reading on uart fahri 6 3,390 Sep-29-2020, 03:07 PM
Last Post: Larz60+
  decimal or hexadecimal digits to int Skaperen 3 4,182 Sep-26-2017, 07:03 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020