Thursday, April 28, 2022

Working with RXTXcomm.jar for Ubuntu 16.04 LTS for serial communication

I have been working on IOT based project where I have to connect with a NFC device and communicate with the device using serial communication. I have used RXTXComm.jar on window. Windows 10 is my development environment. I started developing the application, and when almost finished I came to know that my production environment is Linux. Basically, I need to run the application where the system has Ubuntu 16.04 LTS as OS. As most developer knows RXTXComm.jar uses a dll file called “rxtxSerial.dll” for window OS to communicate with serial ports. On migrating java code to Ubuntu I need to add the shared library(.so file) for  “rxtxSerial.dll” to communicate with the server. That is not a big challenge as these files are available on the web  and can be downloaded from the URL below.

http://rxtx.qbang.org/pub/rxtx/rxtx-2.0-7pre2/INSTALL.

You will get all the .so files as per your system requirement.  Whether your system is 32 bit or 64 bit you can use the respective .so file from the downloaded resources. Make sure your follow the instruction for installing the shared library. I got the required shared library(.so file) for RXTXComm.jar and able to run my GUI application for serial communication.  The .so file should be in your java library path or you can add it to source folder. Here too there was another surprise for me. I could not get COM ports connected to my system using the API available in RXTXComm.jar. In my system (Ububtu 16.04 LTS) ttyUSB0 is the only COM port available and it was clearly visible when I run “lsusb” or  “dmesg | grep ttyUSB” command from terminal.  But it was not showing in my application where I was populating a combo box with available serial ports. After a lot of search on Google I came to know that we need to provide correct permission ttySxx or ttyUSBxx  which is basically in /dev folder. The user you are logged in must have to be in “dialout” group. If you run a ls-ltr command as show below you can view the serial ports with their permission.

$ ls -ltr /dev/tty*

crw-rw---- 1 root dialout 4, 65 2022-04-28 01:08 /dev/ttyUSB0

crw-rw---- 1 root dialout 4, 66 2012-04-28 01:08 /dev/ttyS1

crw-rw---- 1 root dialout 4, 66 2012-04-28 01:08 /dev/ttyS2

To add the root user or any user to dialout group use the following command.

sudo usermod -a -G dialout username

After running the above command I could able to get list of COM ports. In my case there is only one USB COM port named ttyUSB0. Still there is a challenge as the production environment where my application will work is 32bit device. Here my PC on which I was testing the application is 64 bit.  To make sure the application runs on 32 bit Ubuntu, I created a runnable jar file. I copied the RXTXComm.jar and librxtxSerial.so in the same location where my runnable jar. Then I used the following command to run my runnable to jar to view the GUI.

$ sudo java -jar myapp.jar

Application run successfully and I did able view the  COM port, connect the COM port and communicate with the device connected to the COM port. 

Hope this post helps the budding developers and they should not face the same issue as I did. Lastly I put down the refence sites that helped me to solve the above issue.

References : 

1. http://rxtx.qbang.org/wiki/index.php/Installation

2. https://stackoverflow.com/questions/38753097/unable-to-find-serial-ports

3. https://stackoverflow.com/questions/15533310/rxtx-cant-list-port-on-ubuntu


 

 

 

 

 

 

 

 

No comments:

Post a Comment