从Java中的COM端口读取,错误0x5位于..\rxtx\src\termios.c(892)
我正在用Java编写一个从端口读取的小应用程序,因为我们使用64位系统,所以我不得不使用RXTX。问题是,当我尝试运行我的应用程序时,出现以下错误: “位于..\rxtx\src\termios.c(892)的错误0x5:访问被拒绝” 试过我的代码,也试过RXTX站点上的代码,有人有过类似的经验吗从Java中的COM端口读取,错误0x5位于..\rxtx\src\termios.c(892),java,serial-port,rxtx,termios,Java,Serial Port,Rxtx,Termios,我正在用Java编写一个从端口读取的小应用程序,因为我们使用64位系统,所以我不得不使用RXTX。问题是,当我尝试运行我的应用程序时,出现以下错误: “位于..\rxtx\src\termios.c(892)的错误0x5:访问被拒绝” 试过我的代码,也试过RXTX站点上的代码,有人有过类似的经验吗 这是我的密码: import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import
这是我的密码:
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();
}
}
}
我使用了rxtx api,并且工作得很好。我遇到了这个问题,因为该端口实际上正在使用。以前的javaw.exe实例出现在Windows任务管理器中,它占用了端口
java进程挂起CommPortIdentifier的原因是一个硬件问题:当我将碰巧使用的USB-2串行转换器插入USB2端口时,一切正常。当插入USB-3端口时,CommPortIdentifier代码将挂起,随后Java实例收到termios.c:Access Denied错误。此shell命令已在Android中得到帮助(stty需要busybox):
您可以更改,只是为了测试::
while((data=in.read())>-1)
inwhile((data=in.read())>0)
每当我在连接时拔下USB-2-COM适配器时,控制台中就会出现此错误。
su
chmod 666 /dev/ttyO2
stty -F /dev/ttyO2 speed 115200