This blog is to share some demo or guideline how to work the embedded byte operation on python.

For example, if you are using the PYSERIAL module (UART) and communicate with external MCU, you need to know how to put or extract the byte data through UART python module. Here are some blogs to describe how to do it.

Here are some URLs:

In my experience, it is really challenge when I was first time to work with python. It needs to spend a lot of time for seeking for the example from web.

For example, when I implemented the example https://github.com/jimmywong2003/nrf52_ble_transfer_jpg, it needs to split the data through pyserial module.

https://github.com/jimmywong2003/nrf52_ble_transfer_jpg/blob/master/file_read_jpg/load_jpg_file.py

        data_request = ser.read(8)
        print (data_request)
        (payload_len, opcode, request_length, offset_addr) = struct.unpack('BsHL', data_request)

it is used to split the data_request string into multiple variables.

How to pack the data to send it out.

            ser.write(struct.pack("B", payload_len))

I have used a lot of the information from the following URL as below.

http://www.sunyouqun.com/2020/03/python-serial-test-bytes-type/

Basically, you can find a lot of interesting passages on the Bluetooth Low Energy and Nordic SDK information from above blog.

You can spend some time to have a look.

Bytes on the Python

When performing serial port test tasks, there are two common formats for the PC to send serial port instructions to the client:

  • ASCII string, such as:AT+CFUN=1
  • Integer array, such as:[0x59, 0x03, 0x01, 0x02, 0x03]

In Python programs, the parameters of the data transceiver API of the PySerial library are all bytes type.

if you need to represent the byte array inside the python,

x01\x02' or b'hello'

Therefore, in the actual program, it is necessary to interchange strings or integer types with bytes. This article summarizes some common conversion techniques.

1. Convert string to bytes type

cmd_str = 'AT+CFUN=1'
cmd_bytes = cmd_str.encode() 
# cmd_bytes = b'AT=CFUN=1'

2. Hexadecimal array converted to bytes type

cmd_hex = [0x01, 0x02, 0x03, 0x04]
cmd_bytes = bytes(cmd_hex) 
# cmd_bytes = b'\x01\x02\x03\x04'

cmd_byte = 0x05
cmd_bytes = bytes([cmd_byte]) 
# cmd_bytes = b'\x05'

3. Convert multi-byte integers to bytes type

cmd_number = 0x010203
cmd_bytes = cmd_number.to_bytes(3, 'big') 
# cmd_bytes = b'\x01\x02\x03'

4. Convert bytes type to integer array

cmd_bytes = b'\x01\x02\x03\x04'
cmd_hex_list = list(cmd_bytes) 
# cmd_hex_list = [0x01, 0x02, 0x03, 0x04]

cmd_bytes = b'\x05'
cmd_byte = cmd_bytes[0] 
# cmd_byte = 0x05

5. Convert bytes type to string array (hexadecimal form)

cmd_bytes = b'\xf1\xf2\xf3\xf4'
cmd_str = ['0x{:02X}'.format(x) for x in list(cmd_bytes)] 
# cmd_str = ['0xF1', '0xF2', '0xF3', '0xF4']

6. Bytes type converted to ASCII string

cmd_bytes = b'hello'
cmd_str = cmd_bytes.decode()
# cmd_str = 'hello'

7. Convert bytes type to multi-byte integer

cmd_bytes = b'\x00\x01'
cmd_hex = int.from_bytes(cmd_bytes, 'big')
# cmd_hex = 0x0001

Example

How to use the Nordic SDK CLI example to put the data

import serial

CMD_COLORS_OFF = 'cli colors off'
CMD_PYTHON = 'python'
CLI_PREFIX = 'uart_cli:~$ '

def serial_command(cmd):
    serial_cmd = cmd + '\r'
    return bytes(serial_cmd.encode())

if __name__ == '__main__':
    serial_port = serial.Serial(port='COM34', baudrate=115200, timeout=0.1)
    # disable color
    serial_port.write(serial_command(CMD_COLORS_OFF))
    # send python command
    serial_port.write(serial_command(CMD_PYTHON))
    
    recv_msg = ''
    while True:
        read_line = serial_port.readline()
        if read_line == CLI_PREFIX.encode():
            print('Command finished')
            break
        if read_line != b'':
            recv_msg += read_line.decode()
    print('Message: {}'.format(recv_msg))

Thanks for your interests on my blog. Since 2019, I have created this blog and shared the idea how to do some funny stuffs. I am very pleasure that I get quite a lot of positive feedback. I really hope that this blog helps your own embedded solution development. May I get support from you to keep it in order to maintain the wordpress host service? Your appreciation would be very helpful.


https://jimmywongiot.com/2021/05/26/asking-for-support/