- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用串行端口在我的 PC(使用 Netbeans 和 RXTX 的 Windows 7)与 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");
}
}
最佳答案
串行数据只是一种数据流。根据您读取它的时间和正在发生的缓冲,当您读取它时可能只有部分数据可用。
由于您使用的是面向行的数据,因此您要做的是缓冲数据,直到看到行终止符,然后才处理数据。
关于java - Java RXTX 与 Arduino 串口通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2009173/
RXTX网站http://rxtx.qbang.org/wiki/index.php/Download已经下降了一段时间。您知道下载适用于 windows、linux 和 raspberry pi 的
我正在尝试使用 RXTX API 通过 Java Marine API 从 NMEA 0183 GPS 获取 GPS 数据。 运行 Java Marine 提供的 SerialPortExample.
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我使用的是 Windows 7 64 位。我编写了一个小应用程序,它使用 RXTX 通过串行端口进行通信。我在 Windows 64 位上使用了 rxtxSerial.dll,它在 Eclispe 和
让我直接进入问题。 比方说,我有一个 Arduino Uno (COM4) 和 Arduino Mega (COM5),它们都通过串行端口与我的操作系统通信。我怎样才能检测到 COM4 真的是一个 A
我正在尝试编写的 Java 串行读取器遇到问题,问题似乎在于 inputstream 方法...... 我的 readSerial() 方法出现错误,它给出错误“无法从类型 InputStream 对
我有一个内置于 netbeans 的 java 程序,它使用 Here 中的 RXTX 。到目前为止,它在 Windows 和 Linux 上都运行得很好。 我的问题:是否可以将所需的 RXTX 文件
我从 RXTX 网站编辑了一个已经制作好的示例。我也是 Java 和串行通信编程的新手。 该应用程序完美运行了一次。它从 PIC 读取缓冲区并发送我输入的数字。 LED 亮起,PIC 发回相同的缓冲区
我正在尝试制作一个 Java 接口(interface)来向 GPRS 模块发送一些 AT 命令。 我已经有了一个关于Processing的工作界面,但我迁移到纯Java,因为我发现制作图形界面更容易
我正在尝试使用 RXTX java 库通过 GUI 中的单击监听器发送字符串,但我无法编辑 RXTX 的代码来发送字符串,它只响应来自控制台的输入,那么我如何编辑代码以不仅发送从控制台输入的数据? 我
我正在使用 RXTX 在 JAVA 和微 Controller 之间进行通信。 这是打开连接、发送和接收数据的JAVA代码 package app; import gnu.io.CommPort; i
我已经使用 RXTX 大约一年了,没有遇到太多问题。我刚刚启动了一个新程序来与新硬件进行交互,所以我重新使用了我在其他项目中使用过的 connect() 方法,但是我遇到了一个以前从未见过的奇怪问题。
在试图找出 this problem 时(感谢任何帮助),我在运行 RXTX 的同时使用 PortMon 监控其 Activity 并注意到 RXTX 不断检查数据是否可用,即使 Java 客户端仅通
我找到了一个通过串行端口在 Java 中与 Arduino 进行通信的代码,并想尝试让它工作以扩展它以实现项目创意,但我不断收到此错误 Stable Library =================
在将 RXTX 用于许多不同的项目之后,我遇到了许多恼人的差异和问题,这些问题只能归结为库中的错误 - 死锁、竞争危险和 RXTX 库深处的监控线程被保持打开状态以防止程序关闭(即使我使用的所有端口都
我正在尝试运行此函数,并得到带有文本的配置 gnu.io.rxtx.properties 窗口:尚未检测到 gnu.io.rxtx.properties。 没有一致的方法来检测此操作系统上的端口。在进
这是我的代码,我正在使用 rxtx 。 public void Send(byte[] bytDatos) throws IOException { this.out.write(by
我目前正在开发一个项目,该项目需要与连接在通讯端口上的设备进行通信。我下面有一个函数,它搜索串行端口,将它们添加到 HashMap ,然后返回 HashMap 。我注意到一个问题,每当我尝试从 Has
我正在尝试编写一个程序,让 Arduino Uno 通过串行端口与 Java 程序通信。我按照此处的说明进行操作 ( http://playground.arduino.cc/Interfacing/
我在 Windows 上使用 RXTX 2.1-7,但遇到一个问题。每当我使用 RXTX 库运行程序时,都会打印以下通用版本控制信息: 稳定库 ============================
我是一名优秀的程序员,十分优秀!