Serial Programming in Raspberry Pi

Posted on Wed 14 March 2018 in raspberry-pi

Raspberry Pi uses serial port for console tty. In order to use this serial port to communicate with Mac machine, we will temporarily disable getty service:

$ sudo systemctl stop serial-getty@ttyS0.service
$ sudo chmod 666 /dev/ttyS0

Then the application can use /dev/ttyS0 to communicate with other device using serial cable.

In macOS, we can use minicom to connect Raspberry Pi using USB-serial cable. You may find something like tty.usbserial-xxxxxx in /dev directory. So connect to it:

$ minicom -D /dev/tty.usbserial-xxxxxx

Note: You need to set up serial port parameter, e.g. bps, before connection.

$ minicom -s

    ???????????????????????????????????????????????????????????????????????Ŀ
    ? A -    Serial Device      : /dev/tty.usbserial-A96DT7VN               ?
    ? B - Lockfile Location     : /usr/local/Cellar/minicom/2.7.1/var       ?
    ? C -   Callin Program      :                                           ?
    ? D -  Callout Program      :                                           ?
    ? E -    Bps/Par/Bits       : 115200 8N1                                ?
    ? F - Hardware Flow Control : No                                        ?
    ? G - Software Flow Control : No                                        ?
    ?                                                                       ?
    ?    Change which setting?                                              ?
    ?????????????????????????????????????????????????????????????????????????
            ? Screen and keyboard      ?
            ? Save setup as dfl        ?
            ? Save setup as..          ?
            ? Exit                     ?
            ? Exit from Minicom        ?
            ????????????????????????????

Tips: If you want to send '\r\n' (CR+LF) to the device in minicom, you can type ctrl+m ctrl+j