在 Java 中从 COM 端口读取,错误 0x5 在 ..\rxtx\src\termios.c(892)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10578021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading from COM port in Java, Error 0x5 at ..\rxtx\src\termios.c(892)
提问by Sin5k4
I'm writing a small app in Java to read from COMport, and since we use 64 bit systems I had to use RXTX. The problem is when I try to run my app I get the following error:
我正在用 Java 编写一个小应用程序来从COM端口读取,由于我们使用 64 位系统,我不得不使用 RXTX。问题是当我尝试运行我的应用程序时出现以下错误:
"Error 0x5 at ..\rxtx\src\termios.c(892):Access Denied"
“..\rxtx\src\termios.c(892) 处的错误 0x5:拒绝访问”
Tried my code and also the codefrom RXTX site, anyone had any experience with this before?
尝试了我的代码以及来自 RXTX 站点的代码,之前有人有这方面的经验吗?
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();
}
}
}
回答by Kay
I ran into this problem because the port wasactually in use. A previous instance of javaw.exe appeared in the Windows task manager, it hogged the port.
我就遇到了这个问题,因为港口是实际使用。以前的 javaw.exe 实例出现在 Windows 任务管理器中,它占用了端口。
The reason why java processes did hang in CommPortIdentifier was a hardware issue: When plugging the USB-2-serial converter that I happened to use into a USB2 port, all worked fine. When plugged into a USB-3 port, CommPortIdentifier code would hang, and then subsequent instances of Java received the termios.c: Access Denied error.
java 进程在 CommPortIdentifier 中挂起的原因是硬件问题:将我碰巧使用的 USB-2 串行转换器插入 USB2 端口时,一切正常。当插入 USB-3 端口时,CommPortIdentifier 代码将挂起,然后 Java 的后续实例收到 termios.c: Access Denied 错误。
回答by Eugene
This shell command has been helped to me in Android (busybox required for stty):
这个shell命令在Android中对我有帮助(stty需要busybox):
su
chmod 666 /dev/ttyO2
stty -F /dev/ttyO2 speed 115200
回答by user1112699
I had used the rxtx api, and worked fine.
我使用过 rxtx api,并且工作正常。