Python Forum

Full Version: sending byte in code?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 ,
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'
Great - it's working

Thanks ,