Java RXTX与Arduino之间的串行通信
我正在尝试使用串行端口在我的PC(使用Netbeans和RXTX的Windows 7)和Arduino Pro之间进行通信。Arduino实际上使用FTDI电缆连接到电脑 代码基于找到的JavaSimpleRead.Java 目前,Arduino只需在启动时打印一个字符串。我的Java程序应该打印已读取的字节数,然后打印出内容。Java程序可以工作,有点 如果字符串很长(>10字节左右),输出将被中断 如果我在Arduino上打印Java RXTX与Arduino之间的串行通信,java,serial-port,arduino,rxtx,Java,Serial Port,Arduino,Rxtx,我正在尝试使用串行端口在我的PC(使用Netbeans和RXTX的Windows 7)和Arduino Pro之间进行通信。Arduino实际上使用FTDI电缆连接到电脑 代码基于找到的JavaSimpleRead.Java 目前,Arduino只需在启动时打印一个字符串。我的Java程序应该打印已读取的字节数,然后打印出内容。Java程序可以工作,有点 如果字符串很长(>10字节左右),输出将被中断 如果我在Arduino上打印 Serial.println("1234567891234567
Serial.println("123456789123456789"); //20 bytes including '\r' and '\n'
Java程序的输出可能类似于:
Number of Bytes: 15
1234567891234
Number of Bytes: 5
56789
或
我认为这是一个计时问题,因为当我使用调试器手动检查代码时,结果字符串总是应该的:一个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和Processing,从Arduino读取/写入值非常容易。 这是一个随处理而来的读取示例(文件>示例>库>序列>SimpleRead) 就我所记得的,你在Arduino中实例化Serial时设置的波特率是非常重要的。例如,如果使用9600进行发送,则应使用相同的号码进行侦听 另外,以字节的形式发送信息也非常重要,否则会遇到\r或\n之类的问题 较短的版本,请尝试:
Serial.println(123456789123456789,BYTE);
越简单越好 我认为您需要使用事件驱动的设计模式来解决这个问题。我强烈建议您访问:出于某种原因,我假设数据将在单个流中连续发送。在PC端,我只需要查看串行输出,所以我不认为我会费心存储完整的输入线,然后显示它。我想我会在数据输入时逐字节显示数据。所以我将使用aByte=input.read();而不是input.read(readBuffer);Thanks@SharpBarb-无需切换到一次读取一个字节。如果你真的想这样做,我建议你先读取一个缓冲区,然后在你的应用程序中分别处理每个字节。嗨,没有Papplet处理可以吗?因为我尝试与我的java代码集成…感谢您的回复,@geory让我说我想从arduino获得模拟输入,每当我向arduino发送一些东西时,arduino都会回复模拟值。根据链接中的代码,它只会打印出来,而不是保存在变量中,如何将chunck存储到变量中?因为public synchronized void serialEvent无法返回值,所以我认为…@wizztjh这是我的想法,但您有一段代码:byte chunk[]=new byte[available];读取(块,0,可用)。。。之后,日期存储在字节中,您应该能够使chunk变量对类可见,而不仅仅是该块。如果new需要字符串,可以创建一个类变量,并在input.read部分之后将其值设置为新字符串(chunk)。嗯
/**
* 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
}
*/
Serial.println(123456789123456789,BYTE);