arduino 与java通信_Java RXTX和Arduino之间的串行通信

我正在使用串行端口在我的电脑(

Windows 7使用Netbeans和RXTX)与Arduino Pro之间进行通信. Arduino实际上使用FTDI电缆连接到PC.

该代码是基于Java SimpleRead.Java发现的here.

目前,Arduino在启动时只会输出一个字符串.我的Java程序应该打印已读取的字节数,然后打印出内容. Java程序的工作原理

如果字符串长(> 10字节左右),则输出将被分解.

所以如果在Arduino我打印

Serial.println("123456789123456789"); //20 bytes including '\r' and '\n'

我的Java程序的输出可能看起来像:

Number of Bytes: 15

1234567891234

Number of Bytes: 5

56789

要么

Number of Bytes: 12

1234567891

Number of Bytes: 8

23456789

我认为这是一个时间问题,因为当我手动使用调试器编写代码时,结果字符串总是应该是:一个20字节的字符串.

我一直在搞乱各种各样的事情,但我无法解决问题.

这是代码给我的一部分问题:

static int baudrate = 9600,

dataBits = SerialPort.DATABITS_8,

stopBits = SerialPort.STOPBITS_1,

parity = SerialPort.PARITY_NONE;

byte[] readBuffer = new byte[128];

...

...

public void serialEvent(SerialPortEvent event)

{

if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE) {

try {

if (input.available() > 0) {

//Read the InputStream and return the number of bytes read

numBytes = input.read(readBuffer);

String result = new String(readBuffer,0,numBytes);

System.out.println("Number of Bytes: " + numBytes);

System.out.println(result);

}

} catch (IOException e) {

System.out.println("Data Available Exception");

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值