Debug Skill

Byte manipulation on Python platform

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, it needs to split the data through pyserial module.

        data_request =
        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.

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


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
    # send python command
    recv_msg = ''
    while True:
        read_line = serial_port.readline()
        if read_line == CLI_PREFIX.encode():
            print('Command finished')
        if read_line != b'':
            recv_msg += read_line.decode()
    print('Message: {}'.format(recv_msg))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.