How to use RS232 and RS422 with Python on NVIDIA Jetson Modules
Updated: 6 days ago
In this blog, we will explain how to use RS232 and RS422 serial communications with python on NVIDIA Jetson Modules. There is a little difference between NVIDIA Jetson Nano and NVIDIA Jetson Xavier NX while using RS232 and RS422 serial communications. The difference is based on setting general purpose output pins which enables to choose the serial communication protocol.
For testing these serial communication protocols, you need to have "USB-to-Serial" adapters. We have used Moxa Uport 1130 adaptors. The driver of these devices can be found in their official sites. Some adaptors have supports multiple protocols like "RS422" and "RS485". It should be specified on device manager which protocol will be used. We have used Windows 10 operating system and we used "TeraTerm" program for serial interface.
You can click here to check the basics of the serial communication on NVIDIA Jetson Nano module.
You can click here to check the basics of the serial communication on NVIDIA Jetson Xavier NX module.
We need to install "python3-pip" package.
sudo apt-get install python3-pip
After installing "python3-pip" package, we need to install "pyserial" package.
pip3 install pyserial
There is a script named "nvgetty.sh" .We need to set the lines of this script as comment lines. You can see the full path of script below.
sudo gedit /etc/systemd/nvgetty.sh
Change the lines of code snippet like below.
There are two GPIO pins which used to set properties of serial communication. One of them is used to set the communication as "Half Duplex" or "Full Duplex". The other pin is used to set serial communication protocol as "RS232" and "RS422". We created four different scripts that set to these properties for NVIDIA Jetson Nano and NVIDIA Jetson Xavier Nx seperately. Our scripts set the comunication way as "Full Duplex". If you would like to use "RS232" protocol , run the script like below.
After setting the general purpose output pins, please run the python3 command line and type the commands below. Please pay attention to the path of serial communication port. In this example, the correct is "/dev/ttyTHS0"
>>> import serial
>>> port = serial.Serial('/dev/ttyTHS0') # open serial port
>>> port.write(b'hello') # write a string
You should see the message correctly on the other side. We run "TeraTerm" serial communication interface program from the another machine which is communicating with NVIDIA Jetson Module.
You can read the input line by following command. When you run this command, you should send the message from the other side communication line.
>>>line = port.read(10)
Than, you can print what you read as below.
In case of using "RS422" serial communication, you should run related script to set general purpose outputs.
The rest of process should be same with "RS422".
Thanks for reading !