Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending byte in code?
#1
Hello,
I'm working on some BLE code that need to send 01 \ 00 in order to turn on a remote LED.

this is the command I'm using
 await client.write_gatt_char(Led_Char, b'\01') # send new value 
and right now I'm working not smart :

New_Led_Status = input(''' Enter new led status: 3 - Led1 - off 2 - Led1 on 1 - Led on , 0 - led off , ''' ) if New_Led_Status == "1": await client.write_gatt_char(Led_Char, b'\01',) elif New_Led_Status == "0": await client.write_gatt_char(Led_Char, b'\00') elif New_Led_Status == "2": await client.write_gatt_char(Led_Char, b'\02') 
I want to enter the value inside 1 command and not write the same command many times(I'm going to handle more options)
what do I need to change?



and another issue

when I run the code on Windows - it's working,
when I run the code on Linux - it's not sending , I get nothing on the remote device
on linux I have
Python 3.7.3
on windows I have
Python 3.10

can this be reason ? - if so what do I need to change?
*** using the same code , and I don't get any errors

what could be the problem?

Thanks ,
Reply
#2
There is an int.to_bytes() method. You could convert your int string to an int, then convert that to bytes.
New_Led_Status = 2 New_Led_Status = int(New_Led_Status).to_bytes(length=1, byteorder='big') print(New_Led_Status)
Output:
b'\x02'
Reply
#3
Great - it's working

Thanks ,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 19,148 Sep-18-2020, 10:10 PM
Last Post: tienttt
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 77,340 Feb-08-2020, 06:58 PM
Last Post: karkas
  Byte array is sorted when sending via USB daviddlc68 1 4,046 Aug-16-2019, 10:11 AM
Last Post: wavic
  4 byte hex byte swap from binary file medievil 7 27,878 May-08-2018, 08:16 AM
Last Post: killerrex
  Sending Hex Code to Machine from Laptop using TCP/IP Protocal Brian_Cheong 2 12,525 May-12-2017, 01:01 PM
Last Post: sparkz_alot

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.