serialio java_如何解决错误java.io.IOException:串行通信的nativeavailable输入/输出错误?...

我有Arm处理器,它是AllWinner A13,RAM-512mb和OS-Linaro 13.01 Ubuntu(意思是debian).现在我正在为/ dev / ttyS0制作串行通信程序.我使用netbeans在

java中为双向串行通信制作了简单的程序.在我的处理器中,我将ttyS0的rx-tx缩短为环回连接检查.意味着我通过串口发送的东西,我回来了.但我得到错误.我在我的处理器上安装了openjdk-7,librxtx-java.我的代码和错误如下.如果有任何想法或解决方案,请告诉我.

package serialcomm_linaro;

import gnu.io.CommPort;

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import java.io.FileDescriptor;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

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(115200,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

InputStream in = serialPort.getInputStream();

OutputStream out = serialPort.getOutputStream();

(new Thread(new SerialReader(in))).start();

(new Thread(new SerialWriter(out))).start();

}

else

{

System.out.println("Error: Only serial ports are handled by this example.");

}

}

}

/** */

public static class SerialReader implements Runnable

{

InputStream in;

public SerialReader ( InputStream in )

{

this.in = in;

}

public void run ()

{

byte[] buffer = new byte[1024];

int len = -1;

try

{

while ( ( len = this.in.read(buffer)) > -1 )

{

System.out.print(new String(buffer,0,len));

}

}

catch ( IOException e )

{

e.printStackTrace();

}

}

}

/** */

public static class SerialWriter implements Runnable

{

OutputStream out;

public SerialWriter ( OutputStream out )

{

this.out = out;

}

public void run ()

{

try

{

int c = 0;

while ( ( c = System.in.read()) > -1 )

{

this.out.write(c);

}

}

catch ( IOException e )

{

e.printStackTrace();

}

}

}

public static void main ( String[] args )

{

try

{

(new TwoWaySerialComm()).connect("/dev/ttyS0");

}

catch ( Exception e )

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

我在外面.在这个我只发送123,然后我先返回23然后再返回1111 …更多时间,然后错误.而不是111111 ….我只想返回123.

enter code here

RXTX Warning: Removing stale lock file. /var/lock/LCK..ttyS0

123

23

111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

java.io.IOExcepti on: Input/output error in nativeavailable

at gnu.io.RXTXPort.nativeavailable(Native Method)

at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1429)

at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1341)

at serialcomm_linaro.TwoWaySerialComm$SerialReader.run(TwoWaySerialComm.java:66)

at java.lang.Thread.run(Thread.java:722)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值