-
Notifications
You must be signed in to change notification settings - Fork 0
SERIAL COMMUNICATION
This page explains the serial communication between the Raspi and the Arduino.
LINKS:
https://www.elinux.org/Serial_port_programming
https://pyserial.readthedocs.io/en/latest/shortintro.html
STEP 1: Define a serial port using ser = serial.Serial(port=prt, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=dt). This serves as the serial object to call all serial functions associated with the library. Check Python3 serial communication for parameters and examples.
NOTE: Define port as prt by prt = '/dev/ttyACM0'. ACM(X), X values will change every time the device is disconnected and connected after a reboot or using different USB ports. Use the command ls /dev/tty* in the RaspberryPi shell to find out which port the device is connected to.
STEP 2: ser.write(msg.encode()) will encode the message to the appropriate encoding to transfer through serial communication. msg in the payload is the data that is transferred through the serial port. The recommended encoding is UTF-8 and this is default in the encode function. For other parameters, refer to the Python wiki and the link.
STEP 3: Transmitted data can be read back into the Pi to check the data transfer by using myData = ser.readline(), and myData = myData.decode("utf-8","ignore"). ser.readline() reads the output on the serial port and myData.decode(“utf-8” ) decodes the data read through serial port into utf-8 format such that it is human readable.