3.12. UART

serial_ctrl.serial_config(baud_rate, data_bit, odd_even, stop_bit)
Description:

Sets the baud rate, data bit, parity bit, and stop bit attributes of the serial port

Parameters:
  • baud_rate – Set the baud rate, which can be 9600, 19200, 38400, 57600, or 115200
  • data_bit – Set the data bit, which can be cs7 or cs8
  • odd_even_crc – Set parity check. For details, refer to the odd_even_crc table.
  • stop_bit – Set the stop bit, which can be 1 or 2
Returns:

None

Example:

serial_ctrl.serial_config(9600, 'cs8', 'none', 1)

Example description:
 

Set the baud rate to 9600, 8 data bits, no parity check, and 1 stop bit for the serial port

serial_ctrl.write_line(msg_string)
Description:Send string information and automatically add line breaks ('\\n')
Parameters:msg_string (string) – The string information that needs to be sent. Automatically append line breaks ('\\n') after the string when sending it.
Returns:None
Example:serial_ctrl.write_line('RoboMaster EP')
Example description:
 Write 'RoboMaster EP\\n' to the serial port and add line breaks automatically. You only need to send 'RoboMaster EP' in this case.
serial_ctrl.write_string(msg_string)
Description:Send string information
Parameters:msg_string (string) – String information that needs to be sent
Returns:None
Example:serial_ctrl.write_string('RoboMaster EP')
Example description:
 Write 'RoboMaster EP' to the serial port
serial_ctrl.write_number(value)
Description:Convert the numeric parameter to a string and send the string through the serial port
Parameters:value (int) – The value that needs to be sent
Returns:None
Example:serial_ctrl.write_number(12)
Example description:
 Write the '12' string to the serial port
serial_ctrl.write_numbers(value1, value2, value3...)
Description:

Convert the list of numbers to a string and send the string through the serial port

Parameters:
  • value1 (int) – Value 1 in the list that needs to be sent
  • value2 (int) – Value 2 in the list that needs to be sent
  • value3 (int) – Value 3 in the list that needs to be sent
Returns:

None

Example:

serial_ctrl.write_numbers(12,13,14)

Example description:
 

Write the '12,13,14' string to the serial port

serial_ctrl.write_value(key, value)
Description:

Converts the parameter to a string of key-value pairs and sends the string through the serial port

Parameters:
  • key (string) – The key to be sent
  • value (int) – The value to be sent
Returns:

None

Example:

serial_ctrl.write_value('x', 12)

Example description:
 

Write the 'x:12' string to the serial port

serial_ctrl.read_line([timeout])
Description:Read the string ending with '\\n' from the serial port
Parameters:timeout (float) – An optional parameter, which indicates the timeout period in seconds. The default value of this parameter is permanent blocking.
Returns:The string read from the serial port
Return type:string
Example:recv = serial_ctrl.read_line()
Example description:
 Read a string ending with '\\n' from the serial port
serial_ctrl.read_string([timeout])
Description:Read the string from the serial port (the string may or may not end with '\\n')
Parameters:timeout (float) – An optional parameter, which indicates the timeout period in seconds. The default value of this parameter is permanent blocking.
Returns:The string read from the serial port
Return type:string
Example:recv = serial_ctrl.read_string()
Example description:
 Read a string from the serial port
serial_ctrl.read_until(stop_sig[, timeout])
Description:

Reads a string from the serial port until the string matches the specified 'stop_sig' ending character

Parameters:
  • stop_sig – The specified ending character, whose type is char and range is [ '\n' | '$' | '#' | '.' | ':' | ';' ]
  • timeout (float) – An optional parameter, which indicates the timeout period in seconds. The default value of this parameter is permanent blocking.
Returns:

The matched string read from the serial port

Return type:

string

Example:

serial_ctrl.read_until('#')

Example description:
 

Read a string from the serial port until the ending character of the string matches '#'

odd_even_crc

Hint

For the description of the module, refer to UART.