RS232 - Serial Port

The RS232 class provides a wrapper for basic UART functionality for RS232#1.
It offers basic functions to configure, send and receive data.

The class needs to be initialized with the following parameters:

Parameter name

Value

Description

uart_baudrate

9600, 19200, 38400, 57600,

115200, 230400, 460800,

921600

RS232#1 baudrate

Methods

class RS232(uart_baudrate, uart_rxbuf=4096)

The RS232 class is used to create an object that can communicate via the RS232 connector available on the SBC.

The class needs to be initialized with the following parameters:

Parameter name

Value

Description

uart_baudrate

9600, 19200, 38400, 57600,

115200, 230400, 460800,

921600

RS232#1 baudrate

uart_rxbuf

any, default value is 4096

RX buffer size.

We recommend to leave the default value

unless you know how to deal with

UART RX buffers

read(size=None)

Read data via RS232

Args:

size: number of bytes to receive. Use read() to read all available data.

Return:

Received data

Example to read data:
>>> import sbc
>>> rs232 = sbc.RS232( 115200 )
>>> print(rs232.read(12))
b'Hello World!'

Note

The ouput messages from the RS232#1 are being stored in the RS232 uart_rxbuf buffer.
The default buffer size (4096 bytes) should be enough to read continuously, but be aware that if you don’t read the buffer during a long time, the buffer will be completed and the new messages from RS232#1 will be lost, and by the time you read the buffer you will read “old” messages.
If you want to discard old messages, you can empty the buffer with uart_init() method.

write(data)

Write data via RS232

Args:

data: data to send

Return:

Number of bytes written

Example to write data:
>>> import sbc
>>> rs232 = sbc.RS232( 115200 )
>>> sent_bytes = rs232.write( "Hello World!" )
>>> print('Bytes sent: ', sent_bytes)
Bytes sent:  12
uart_init()

Initializes RS232 UART port

forward_to(uart_fwd=None)

Fordwards RS232 data to a peripheral with UART

Args:

uart_fwd: peripheral object (Gps, XBee)

Example to forward information received on the RS232 socket to GPS1 UART1:
>>> import sbc
>>> rs232 = sbc.RS232( 115200 )
>>> gps1 = sbc.Gps( 1, 115200 )
>>> rs232.forward_to( gps1 )
forward_disable()

Disables RS232 forwarding

get_uart_id()

Get RS232#1 UART port.

Return:

UART port

Note

Since the get_uart_id() method of the RS232 class returns a microPython UART object, it has all its methods.
To do not duplicate the documentation, we suggest to take a look at the official microPython project documentation to see what methods you can additionaly use.

Example of additional methods:
>>> import sbc
>>> rs232 = sbc.RS232( 115200 )
>>> print('There are: ',rs232.any(),' characters to be read')
There are:  1580  characters to be read
>>> print(rs232.get_uart_id().readline())
b'$GNGGA,142222.00,4136.52091,N,00036.70322,E,2,10,1.27,203.6,M,49.8,M,,0000*4D\r\n'