Java的RXTX和Arduino的串行通信
-
18-09-2019 - |
题
我想用一个Arduino Pro的我的电脑(使用Windows 7 Netbeans和RXTX)之间进行通信,使用串行端口。 Arduino的实际上连接到使用电缆FTDI在PC。
本代码是基于Java的SimpleRead.Java发现这里。
目前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的RXTX,但我玩的Arduino和处理,它是很容易从Arduino的读取/写入值。 下面是带有处理的读取样本(文件>实施例>库>串行> SimpleRead)
/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
background(255); // Set background to white
if (val == 0) { // If the serial value is 0,
fill(0); // set fill to black
}
else { // If the serial value is not 0,
fill(204); // set fill to light gray
}
rect(50, 50, 100, 100);
}
/*
// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.
int switchPin = 4; // Switch connected to pin 4
void setup() {
pinMode(switchPin, INPUT); // Set pin 0 as an input
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.print(1, BYTE); // send 1 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}
*/
据我记得,波特啄你的设置中的Arduino当实例序列是非常重要的。如果使用9600发送例如,你应该使用相同数量的听。
此外它来发送你的信息的字节是很重要的,否则你就会有这样的东西在路上\ R或\ n。
更短的版本,尝试:
Serial.println(123456789123456789,BYTE);
越简单越好。
我认为你需要使用事件驱动的设计模式来解决这个问题。我强烈建议您访问: HTTP://www.whatisarduino。组织/斌/教程/爪哇+串行+ API +和+的Arduino