How to test a serial port on Linux
If you are having problems with your serial port or are unsure whether it is configured correctly follow the following steps:
Check port is installed
List the /dir directory and check which ports are installed. In our case we are looking for the ttyAMA0 port.
dir /dir
Run a loopback test
A loopback test is performed by installing a jumper wire between the TX and RX pins. This will cause any data that is sent to the TX pin to be played back on the RX pin. So any data sent to TX is looped back to the RX pin like an echo. If we hear the echo then we know the port is working.
- Install a jumper wire between RX and TX (pin 8 and pi 10 on Raspberry)
2. Run the following code:
If the port is working correctly you will see the following output:
python loopback.py
('Opened port', '/dev/ttyAMA0')
('Sent', 38, 'bytes')
This data was sent to the serial port
If the port is not working then you will only see the following:
python loopback.py
('Opened port', '/dev/ttyAMA0')
('Sent', 38, 'bytes')