Serial is just a way to send data (often text) between two devices bidirectionally. Operating systems generally expose the serial device to user space programs for them to use as they see fit. Under Linux they can be accessed through /dev/ttyACM0
or /dev/ttyUSB0
or /dev/ttyAMA0
. Under Mac through /dev/cu.usbserial
or similar. And under windows it's called something like COM1
.
The Raspberry Pi's serial device is at /dev/ttyAMA0
from the pi's perspecitive. By default it writes the startup logs and then attached a terminal (aka a getty) to the serial device which allows you to control the pi over the serial line from a pc.
You can view and send commands to this terminal from your computer with a USB to serial adapter and a serial program like picocom
or screen
(or one of many others) on Linux/Mac or putty
on Windows. The baud rate that the getty uses is 115200
, this is the speed of the serial connection and must match on both sides of the serial line or you will just get corrupted data.
This is nice for general access to the pi without a network connection but is not the best way to control the pi programmatically. Instead it is better to write your own program to listen on the serial port on the pi and do things when it receives messages on it.
Disable the serial getty
To do this we need to first turn off the getty and logs that are configured by default. You can do this on rasbian by following this guide. On the latest raspbian you can do this by running the following. Or on older versions follow this
sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service
sudo sed 's/console=serial0,115200 //' -i /boot/cmdline.txt
Then reboot. The terminal program running on your computer should be silent for this boot.
Python Serial Listener
Using python you can read from and write to a serial device using pyserial. Start by installing pyserial on the pi:
sudo apt-get install python-serial
Then save the following to a file called echo.py
(or anything you want).
#!/usr/bin/env python2
import serial
with serial.Serial('/dev/ttyAMA0', 115200) as ser:
while True:
line = ser.readline()
print(line.strip())
ser.write(line.upper() + '\n')
Run this on your pi python2 echo.py
and then use the serial program we used before to talk to it on your computer. Note that most serial program send '\r' when you hit enter, but the above waits for '\n', you should be able to send that by pressing Ctrl+J.
Python Serial Writer
Once you have this working you can write another python program on your computer (not the pi this time) to send the messages for you. Remember to replace /dev/ttyUSB0 with you actual serial device. Save the following to send.py
#!/usr/bin/env python2
import serial
with serial.Serial('/dev/ttyUSB0', 115200) as ser:
ser.write('Hello world!' + '\n')
line = ser.readline()
print(line.strip())
With echo.py
running on the pi you can run python2 send.py
on your computer and see them communitcate.
Now you can expand these programs to do anything you want.