pyserial code example

Example 1: install serial python

python -m pip install pyserial

Example 2: pyserial example code

>>> import serial
>>> ser = serial.Serial('/dev/ttyUSB0')  # open serial port
>>> print(ser.name)         # check which port was really used
>>> ser.write(b'hello')     # write a string
>>> ser.close()             # close port

Example 3: pyserial read

# https://pyserial.readthedocs.io/en/latest/pyserial_api.html
# pip install pyserial
import serial

if __name__ == "__main__":
    with serial.Serial('COM10') as serial_port:
        line = serial_port.readline()