How to Use RS232 and RS422 with Python on NVIDIA Jetson Modules? - Forecr.io

How to Use RS232 and RS422 with Python on NVIDIA Jetson Modules?

Jetson Nano | Jetson Xavier NX

18 February 2021
WHAT YOU WILL LEARN?

1- Installation python and serial communication packages

2- RS232 and RS422 settings for the board

3- Communication via RS232 and RS422

ENVIRONMENT

Hardware:  Applied to all Forecr Products

OS: Applied to all Jetpack versions



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 out which 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 specify 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 scripts four different scripts that set to these properties for NVIDIA Jetson Nano and NVIDIA Jetson Xavier NX separately.

Install the zip file at the top of the post and extract it. Then go to the extracted file location.

Our scripts set the communication way as "Full Duplex". If you would like to use "RS232" protocol, run the script like below.


chmod +x *.sh
./rs232_nx.sh


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"


sudo python3
>>> 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 another machine which is communicating with NVIDIA Jetson Module. Set the port COM and bitrate in TeraTerm-Setup-Serial port setup. (Set the bitrate value 9600.) 



You can read the input line by following command. When you run this command, you should send the message from the other sideio communication line.


>>>line = port.read(10)


Then, you can print what you read as below. We write “b’forecr’“ to the line part.


>>>print(line)


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".


Thank you for reading our blog post.