Main Menu

search

You are here

Serial Communication on the rPi

[last updated: 2022-10-03]
rPi home page
-----

... the usual disclaimers about info here being an approximation and just mostly notes from what I've read and/or tried,
with whatever success or not...

----------------------------------------------

  • rPi's have one or more hardware UARTs onboard that implement serial communication with whatever peripheral module you want to talk to.
  • Use python programs to communicate with a peripheral with the rPi's UARTs.
  • Each UART implements a port, or a named serial channel.
    ----------------------------------------------
  • Setup/configure the serial port for your use:
    • By default, the Raspberry Pi’s serial port is configured to be used for console input/output.
      To use the serial port for hardware peripherals, like RS485, the serial port console login must be disabled.
      After doing raspi-config and rebooting,
      the "[... ...] console [ttyAMA0] enabled" line will be gone from the dmesg output.

    • From web:
      • I didn't have to do any of these things on the rPiZero's that I tested.

      • Add this line to /boot/config.txt:
        dtoverlay=disable-bt
      • Edit /boot/cmdline.txt to remove text referring to the serial console.
      • Add this somewhere?:
        enable_uart=1

    ---------------------------------------------------------

  • Test the serial port:

    ---------------------------------------------------------

  • CLI commands to investigate ports:
    • $ dmesg | grep tty
      produces this output (with variations depending on rPi model and firmware rev):
      [... ...] kernel ...
      [... ...] console [tty1] enabled
      [... ...] 20201000.serial: ttyAMA0 at MMIO 0x20201000 (irq = 81, base_baud = 0) is a PL011 rev2
      [... ...] console [ttyAMA0] enabled


      Note the last line says that the AMA0 port is enabled to the console. This must be changed to use the port for your project
      See setup/config below.

    • $ python -m serial.tools.list_ports
      will print a list of available ports.
    • $ ls -l /dev Not sure what this tells me...

    ---------------------------------------------------------

  • Random Notes:
    • Saw a reference saying that the uart, that is the serial port on the rPi, in addition to having Tx & Rx, also uses GPIO-17 & 16 for RTS (request-to-send) and CTS (clear-to-send) signals. No clue what if anything to do about that. For now I'll ignore it, since the MAX485 doesn't do that kind of full-duplex hand-shaking...
    • The following methods may raise SerialException when applied to a closed port.
      read(size=1)
      Parameters: size – Number of bytes to read.
      Returns: Bytes read from the port.
      Return type: bytes
      Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.
      Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise.
    • while ser.in_waiting: # or: while ser.inWaiting():
    • print ser.readline()
    • For versions prior to pyserial 3.0, use .inWaiting().
    • To determine your pyserial version, do this:
      import serial
      print(serial.__version__)

    • Modbus:
      is a communication/messaging protocol.
      It can be used on RS485, RS232, or any number of other serial communication hardware implementations.

    ------------------------

  • Initializing and manipulating the serial port:
    • start with 'import serial'.
    • Then start & open the port with:
      ser = serial.Serial()
      ser.port = "/dev/ttyAMA0"
      ser.baudrate = 9600
      ser.open()
      • Alternatively, you can:
        ser = serial.Serial(port, baud, timeout=1)
        eg: ser = serial.Serial('/dev/ttyACM0', 9600, timeout=0.050)
    • confirm it's open with:
      if ser.isOpen():
    • did a rudimentary program that just opens the serial port and writes a bunch of data
      hooked up scope to Tx line and indeed see activity

    ----------------------------------------------------

  • Links:

.

.

.

eof