Skip to main content
python serial-programming-tutorial on linux

A short tutorial on how to setup a serial port communication between a Linux PC/Laptop and a AVR/PIC/MSP430 Microcontroller or Arduino using pySerial and Python.

 

 

 

The Tutorial deals with

  • Setting up permissions to read and write to a Linux serial port,
  • How to solve the /dev/ttyUSB0 or /dev/ttyACM0 access denied error.
  • How to add a user to tty and dialout groups.
  • How to solve dmesg :read kernel buffer failed :Operation not permitted error

Please note that this tutorial is Linux specific .

 

Refer

 

Contents

  1. Source codes 
  2. Installing Pyserial on Linux using pip 
  3. Serial Port naming system on Linux 
  4. Serial Ports permissions under Linux 
  5. Adding a user to tty and dialout groups 
  6. Opening the serial port on Linux using Python 
  7. Receiving data from Arduino/ Microcontroller using Python on Linux 
  8. References 

 

Source codes

​Please use

[user@localhost]$ python3 _No_PythonCodes_in_Repo.py

to run the codes in repo on Linux systems. 

Most of the codes do not have a #!/bin/python3 header.

Please note that the source codes on the website show only the relevant sections to highlight the process of programming the serial port.      
Please use the complete source codes from our github repo when building your own program.      
 

 

Hardware Used 

If you want to communicate with a bare microcontroller like ATmega32,ATmega32p,ESP32,Raspberry Pi Pico, MSP430 using Python ,You will need a USB to serial converter like USB2SERIAL.

buy USB to Serial,RS232,RS485 Converter (USB2SERIAL V3.0) from india bangalore (bengaluru)

 

Installing pySerial on Linux

On most Linux distro's python is installed by default. Here I am using Ubuntu 20.04 LTS version and pySerial is not installed by default in my system.

checking pyserial is installed or not on Linux

 

First install pip3 for your Linux version using the appropriate package manager

  1. You can then install Pyserial by using the pip command as shown below.
sudo python3 -m pip3 install pyserial
installing pyserial on linux using pip3

 

Serial Port naming convention on Linux,

In Linux there is no concept of COM number, instead the 

  • hardware serial ports are numbered as ttyS0,ttyS1 etc , 
  • USB to Serial Converters as ttyUSB0,ttyUSB1 etc. 
  • Arduino's as ttyACM0 

You can know more about finding the serial port number here.

Connect your Arduino to the USB port and  issue a

 sudo dmesg | tail

command at the terminal.

You have to use sudo in front of dmesg otherwise you will get a dmesg :read kernel buffer failed :Operation not permitted error.

 

serial communication between arduino and linux PC or Mac

Here Arduino is identified as ttyACM0 on Ubuntu

For USB to Serial converters like USB2SERIAL,

You can connect them to your USB port and issue a “ sudo dmesg | tail” command at the terminal.

finding out the serial port number for usb to serial converters in Linux

So in Linux change the second line to

SerialObj = serial.Serial('/dev/ttyUSB0')

for USB to serial Converters or

SerialObj = serial.Serial('/dev/ttyACM0')

for Arduino

 

Serial Port Permissions under Linux

On most Linux system access to serial ports (USB based Virtual Serial Port or Hardware Ports) are restricted due to security reasons.

If you tried to access the serial port  you will get a  ttyUSB0 or ttyACM0 access denied error.

On Linux to access the serial port the user must be part of 2 groups

  1. tty
  2. dialout

You should add yourself to these two groups using the usermod command.

Please note that you should have permission to run the sudo command (part of the Sudo group in Ubuntu) or part of the wheel group in Centos/RHEL/Rocky Linux.

     
Adding a user to tty and dialout groups for serial port access.

sudo usermod -a -G tty [username]
sudo usermod -a -G dialout [username]

replace [username]  with your username Eg rahul or molly.

The -a  makes sure that you are appending the user to the groups .

if -a  is missing you will get unsubscribed from all other groups except tty and dialout.

After the commands are run,

Logoff from your account and then log back in so that changes are saved.

 

Adding a user to tty and dialout groups on ubuntu 20.04 LTS for serial port programming for arduino

Adding the user molly to dialout and tty groups on Ubuntu 20.04 LTS (Debian based)

 

Adding the user rahul  to dialout and tty groups on Rocky Linux/RHEL/Centos7/8

Adding the user rahul to dialout and tty groups on Rocky Linux (Centos/RHEL/Fedora derivative)

Make sure to Log off after running the commands.

 

Opening a serial port on Linux using Python

To test whether our commands have worked, we will write a small code to open a serial port connection.

#!/bin/python3

import serial

SerialPortObj = serial.Serial('/dev/ttyUSB0')
print('\nStatus -> ',SerialPortObj)
 
SerialPortObj.close()    

This the bare minimum required to open the connection.

 

On our Repo you will find a similar file  called  "SerTest.py" which has a little bit more bells and whistles than the one shown above. I will be using that in the screenshots posted below.

Replace /dev/ttyUSB0  with your serial port number.

Make the script executable by using chmod command.

chmod  +x  your_python_file.py 

and then run it.

if the script is successful ,properties of your serial port will be printed on the terminal.

Python Arduino serial communication Code running on Ubuntu Linux

 Code running on ubuntu 20.04 LTS

Python linux serial communication running on Rocky Linux 8 or Centos 8

Code running on Rocky Linux 8.

 

Receiving data from Arduino/ Microcontroller

Here we will receive string send by the arduino using a python script and display it on the screen.

The Arduino code and full python code are in the repo.

Replace /dev/ttyUSB0  with your serial port number.

Make the script executable by using chmod command.

Execute the script  using the below command.

python3 _5_PySerial-Receive_String.py


The file _5_PySerial-Receive_String.py  does not have the #!/bin/python3 header so we have to explicitly specify to use python3 as shown above.

receiving serial data on linux from arduino for embedded data acquisition

The below block diagram shows how to interface any microcontroller to a PC with a standard USB to serial converter. 

block diagram showing connection between MSP430 UART and PC serial port or USB to serial converter

Here the TXD of the serial port is connected to the RXD of the microcontroller UART and vice versa. So when microcontroller transmits a byte it goes to the receive buffer of the serial port on the PC side and vice versa.

Ground lines are made common to both PC and microcontroller.

You have to use a USB to Serial Converter chip like FT232RL or buy a standard USB to serial converter board like  USB2SERIAL to convert serial signals to the USB ones.

msp430 interfaced with PC using a FT232 usb to serial converter  and python

 

References