I have a java application that reads data from a serial port /dev/ttyUSB0. This application runs on Linux, using RXTX library to access the serial port from java.
Sometimes the serial port isn't closed properly by a worker thread, and when starting a new one I get javax.comm.PortInUseException.
Worker threads get stuck while calling close() method. This has already been asked but I cannot directly apply that hack.
Is there a way to force Linux to close the port? I can call external processes and run shell scripts as root. Once the port is closed by the OS, I intend to handle the proper exception in java.
I can't reboot the machine.
How to free serial port after javax.comm.PortInUseException in java/shell
3.5k views Asked by Alessandro Da Rugna At
2
There are 2 answers
2
chrylis -cautiouslyoptimistic-
On
First, you should debug your code and make sure that the thread responsible for closing the device node really does close it; since you're talking about access to a non-shareable hardware device, using the Holder pattern for the RXTX connection might be helpful.
If the program isn't properly closing the device node, the only option is to kill it. You can use fuser /dev/ttyUSB0 to see what process has it open and then kill that PID. This doesn't require root privileges, just access as the user who owns the process to be killed.
Related Questions in JAVA
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in LINUX
- Is there some way to use printf to print a horizontal list of decrementing hex digits in NASM assembly on Linux
- Why does Hugo generate different taxonomy-related HTML on different OS's?
- Writes in io_uring do not advance the file offset
- Why `set -o pipefail` gives different output even though the pipe is not failing
- what really controls the permissions: UID or eUID?
- Compiling eBPF program in Docker fails due to missing '__u64' type
- Docker container unable to make HTTPS requests to external API
- Whow to use callback_query_handler in Python 3.10
- Create kea runtime directory at startup in Yocto image
- Problem on CPU scheduling algorithms in OS
- How to copy files into the singularity sandbox?
- Android kernel error: undefined reference to `get_hw_version_platform'
- Is there a need for BPF Linux namespace?
- Error when trying to execute a binary compiled in a Kali Linux machine on an Ubuntu system
- Issue with launching application after updating ElectronJs to version 28.0.0 on Windows and Linux
Related Questions in BASH
- When does Bash read heredocs?
- Why `set -o pipefail` gives different output even though the pipe is not failing
- Run an external command within jq to manipulate each values of a particular key
- API key 401 error in .env.development file
- How to "Enable mobile data" on a Huawei E3372 4G USB dongle using a bash script in Windows
- ImageMagick / Bash : pipe ignored(?) when filename format variable used
- MacOS Bash-Script: while read p and echo
- Parse command line arguments and write useful usage message without additional code
- JQ JSON - Values to Array
- why variable substitution is so different?
- postbank_pdf2csv: how to setup with Cygwin in Windows?
- Custom Bash functions & custom statements - Need some advice
- unexpected operator == in square brackets when trying to use gum lib
- How to disable a bash builtin inside a docker container
- Use sed or rename find series of alphabet then replace with with the same alphabet and a dash -
Related Questions in SERIAL-PORT
- Nonin Oximeter 3231
- How would I go about filtering non-standardly formatted serial data which contains some junk binary between data entries?
- SerialPort timeouts in ReadChar() method, WPF, "The operation has timed out."
- Rust tokio_serial: async fn readable does not block execution. Runs with 100% CPU load
- Why is there a large ploting delay/lag in my real-time serial port ploting app after more than 10000 datapoints reading?
- I get "DevTools was disconnected from the page" error when connecting Arduino to the computer and switching between tabs
- RS422 communication using PySerial (Raspberry PI)
- Python script becomes unresponsive at 100% CPU usage (single core)
- Redirecting stdin and stdout to the same device in shell
- C# System.IO.Ports throws System.IO.Ports is currently only supported on Windows, but i'm currently using windows
- C# COM Port slowing down to read one byte a second after sending message
- What are the differences between a windows serial port and a macos serial port using NodeJS?
- How to increase baudrate on Device Manager Windows?
- Nextion with Arduino change text
- no console after using :wq in vim
Related Questions in RXTX
- How to not pulse DTR & RTS lines when opening serial port w RXTX
- Is it possible to change the baud rate using nrjavaserial-5.2.1 library without losing serial communication?
- BufferedReader.readLine() finished with non-zero exit value 1
- java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path:
- Communication with serial is not stable
- How to catch all kinds of interrupt Requests in jSerialCom library?
- How can I get the String from a running Thread?
- EXCEPTION_ACCESS_VIOLATION: No Core Dump Will Be Written
- RX/TX Serial Communication
- Serial port reading - Error : EXCEPTION_ACCESS_VIOLATION - C [rxtxSerial.dll+0x5b00]
- Is there any way to transfer data in C via RX/TX protocol?
- J2mod :I/O Exception- Failed to Read
- Java COM port listening with RxTx EXCEPTION_ACCESS_VIOLATION
- Java Serial read \u0002 how to remove?
- Problem with Serial communication RXTX and Arduino
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
I actually faced this problem at work a few months back (at least it looks a lot like it). I spent a lot of time (like a week) on it. I looked through the native code and found the issue (it's in the RXTX lib).
I'm going to recommend you implement the same solution I did - manage your threads and your ports properly! You shouldn't be relying on PortInUseException to check if a port is opened anyway - that's for 'exceptional' cases.
Just make sure you:
If you'd rather work around it than do it the right way - the issue is in the native app, so you need to find a way to 'restart' it. In my case it was all inside an OSGi framework so I could just uninstall the bundle that contained the native app and then install it again. In your case it might require restarting the whole Java process