Reading from COM port in Java, error 0x5 on .. \ rxtx \ src \ termios.c (892)

I am writing a small Java application to read from the COM port, and since we use 64-bit systems, I had to use RXTX. The problem is that when I try to run my application, I get the following error:

"Error 0x5 at .. \ rxtx \ src \ termios.c (892): Access Denied"

Tried my code, as well as the code from the RXTX website, has anyone had experience with this before?


Here is my code:

import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * This version of the TwoWaySerialComm example makes use of the * SerialPortEventListener to avoid polling. * */ public class TwoWaySerialComm { public TwoWaySerialComm() { super(); } void connect ( String portName ) throws Exception { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); if ( portIdentifier.isCurrentlyOwned() ) { System.out.println("Error: Port is currently in use"); } else { CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); if ( commPort instanceof SerialPort ) { SerialPort serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); InputStream in = serialPort.getInputStream(); // OutputStream out = serialPort.getOutputStream(); // // (new Thread(new SerialWriter(out))).start(); serialPort.addEventListener(new SerialReader(in)); serialPort.notifyOnDataAvailable(true); } else { System.out.println("Not a serial port..."); } } } public static class SerialReader implements SerialPortEventListener { private InputStream in; private byte[] buffer = new byte[1024]; public SerialReader ( InputStream in ) { this.in = in; } public void serialEvent(SerialPortEvent arg0) { int data; try { int len = 0; while ( ( data = in.read()) > -1 ) { if ( data == '\n' ) { break; } buffer[len++] = (byte) data; } System.out.print("Result="+new String(buffer,0,len)); } catch ( IOException e ) { e.printStackTrace(); System.exit(-1); } } } public static void main ( String[] args ) { try { (new TwoWaySerialComm()).connect("COM1"); } catch ( Exception e ) { // TODO Auto-generated catch block e.printStackTrace(); } } } 
+4
source share
3 answers

I ran into this problem because the port was really used. The previous javaw.exe instance appeared in the Windows task manager, it clogged the port.

The reason java processes hung in the CommPortIdentifier was a hardware problem: when I connected the USB-2 converter that I used to use in the USB2 port, everything worked fine. When connected to the USB-3 port, the CommPortIdentifier code will hang, and then subsequent instances of Java will receive the termios.c: Access Denied message.

+4
source

This shell command helped me on Android (busybox is required for stty):

 su chmod 666 /dev/ttyO2 stty -F /dev/ttyO2 speed 115200 
+1
source

I used rxtx api and worked fine.

-4
source

Source: https://habr.com/ru/post/1412312/


All Articles