CM3-Home technical documentation Buy

Serial port interface

A serial port interface at 3.3 volt is available on the CM3-Home to connect external microcontrollers like Arduino or ESP8266.

The signals are available on screw terminals as visible below and are at 3.3 volt level. The lines are not-tolerant to 5 volt.


How to use the serial port

This port is visible in Linux as /dev/ttyUSB3 device.

Example in Python

Install the Python serial library:

sudo apt-get update
sudo apt-get install python-serial 

This example sends a string on the serial port each 1 second at 115200,N,8,1 and checks if any character is received from a remote serial terminal.

#!/usr/bin/python
import serial
import time

ser = serial.Serial(
    port='/dev/ttyUSB3',
    baudrate=115200,
    timeout=1,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)
ser.flushOutput()
ser.flushInput()

rx_counter=0
tx_counter=0
while True:
    tx_counter=tx_counter+1
    ser.write("Tx counter = %d | Rx counter = %d\n\r" % (tx_counter,rx_counter))
    print("Tx counter = %d | Rx counter = %d" % (tx_counter,rx_counter))

    a=ser.read(1)

    if a:
        rx_counter=rx_counter+1
        ser.write("Tx counter = %d | Rx counter = %d | Received: %s %02x\n\r" % (tx_counter,rx_counter,a,ord(a)))
        print("Tx counter = %d | Rx counter = %d | Received: %s %02x" % (tx_counter,rx_counter,a,ord(a)))

Listing ports

The command:

python -m serial.tools.list_ports 

will print a list of available serial ports.

Note

This serial port is slightly different from the Raspberry PI3 /dev/ttyAMA0. Some programs initialize pyserial in this way

def connect(self):
    """ Connects to the device and starts the read thread """
    self.serial = serial.Serial(dsrdtr=True, rtscts=True, port=self.port, baudrate=self.baudr$
                                timeout=self.timeout,*self.com_args,**self.com_kwargs)

This doesn't work correctly on the /dev/ttyUSB3, the hardware handshake criteria must be disabled, as is in the default configuration, they are not managed:

def connect(self):
    """ Connects to the device and starts the read thread """
    self.serial = serial.Serial(dsrdtr=False, rtscts=False, port=self.port, baudrate=self.baudr$
                                timeout=self.timeout,*self.com_args,**self.com_kwargs)

Links

Home page CM3-Home technical documentation Buy