Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UART write binary code
#1
hello,

I am trying to send out a binary code with the UART.
with a hexadecimal code it works:
command = b'\x80'
scanner_uart.write(command)
but how can I send out a binary code?
e.g. 00001111
thank you.
Reply
#2
b'\x80' is just a representation of hexadecimal code and is equivalent to 01000000 binary
so 00001111 would be b'\x0f'

Please show enough code so that condition can be recreated.
Please post all errors
Reply
#3
You could try this (perhaps not the most efficient)
>>> bytes([int('00001111', 2)])
b'\x0f'
>>> int('00001111', 2).to_bytes(1, 'little')
b'\x0f'
« We can solve any problem by introducing an extra level of indirection »
Reply
#4
What do you mean by "send binary code"? Do you want to send a binary string (bytes 30 and 31)? That would be b'00001111' or bytes((30, 30, 30, 30, 31, 31, 31, 31)).

Do you want to send 15? That would be bytes((15)) or b'\08'. There is no escape sequence for binary digits like there is for hexadecimal digits.

Do you have a binary string that you want to convert to bytes? Gribouillis covers that in his post. I would not do this for literals in the program, but if you have entered strings that need to be converted, it will do the trick.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  write code that resides in parent directory franklin97355 3 471 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  How Write Part of a Binary Array? Assembler 1 412 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  needing some help to write some code for a game calculator rymdaksel 1 473 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 666 Dec-28-2023, 05:14 PM
Last Post: trix
  Python Code for Preorder Traversal of a Binary Tree Bolt 1 630 Sep-22-2023, 09:32 AM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 7,147 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  How to clean UART string Joni_Engr 4 2,549 Dec-03-2021, 05:58 PM
Last Post: deanhystad
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,933 Jul-17-2021, 04:19 PM
Last Post: bill_z
  Read/Write binary file deanhystad 3 3,252 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  How to write a code with İF function? Aycaaxx 1 1,870 Nov-03-2020, 05:46 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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