Java RXTX 和 Arduino 之间的串行通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2009173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serial Communication between Java RXTX and Arduino
提问by SharpBarb
I'm trying to communicate between my PC (Windows 7 using Netbeans and RXTX) with an Arduino Pro, using the serial port. The Arduino is actually connected to the PC using an FTDI cable.
我正在尝试使用串行端口在我的 PC(使用 Netbeans 和 RXTX 的 Windows 7)与 Arduino Pro 之间进行通信。Arduino 实际上使用 FTDI 电缆连接到 PC。
The code is based on the Java SimpleRead.Java found here.
该代码基于此处找到的 Java SimpleRead.Java 。
Currently the Arduino simply prints out a string when it starts up. My Java program should print the number of bytes that have been read and then print out the contents. The Java program works, sort of...
目前,Arduino 只是在启动时打印出一个字符串。我的 Java 程序应该打印已读取的字节数,然后打印出内容。Java 程序可以工作,有点……
If the string is long (>10 bytes or so) the output will get broken up.
如果字符串很长(> 10 字节左右),输出将被分解。
So if on the Arduino I print
所以如果在 Arduino 上我打印
Serial.println("123456789123456789"); //20 bytes including '\r' and '\n'
The output of my Java program may look something like:
我的 Java 程序的输出可能类似于:
Number of Bytes: 15
1234567891234
Number of Bytes: 5
56789
or
或者
Number of Bytes: 12
1234567891
Number of Bytes: 8
23456789
I'm thinking it's a timing problem, because when I manually go through the code using the debugger, the result string is always what it should be: one 20 byte string.
我认为这是一个时间问题,因为当我使用调试器手动检查代码时,结果字符串总是应该是这样的:一个 20 字节的字符串。
I've been messing with various things but I haven't been able to fix the problem.
我一直在处理各种事情,但我一直无法解决问题。
Here is the part of the code that is giving me problems:
这是给我带来问题的代码部分:
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");
}
}
采纳答案by R Samuel Klatchko
Serial data is just a stream of data. Depending on when you read it and the buffering that is happening, only part of the data may be available when you read it.
串行数据只是一个数据流。根据您读取它的时间和正在发生的缓冲,读取时可能只有部分数据可用。
Since you are using line oriented data, what you will want to do is buffer the data until you see the line terminator and only then process the data.
由于您使用的是面向行的数据,因此您要做的是缓冲数据,直到您看到行终止符,然后才处理数据。
回答by George Profenza
I haven't used Java RXTX, but I've played with Arduino and Processing and it's pretty easy to read/write values from Arduino. Here is a read sample that comes with Processing(File > Examples > Libraries > Serial > SimpleRead)
我没有使用过 Java RXTX,但我玩过 Arduino 和 Processing,从 Arduino 读取/写入值非常容易。这是 Processing 附带的读取示例(文件 > 示例 > 库 > 串行 > 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
}
*/
As far as I remember, the baud thingy you setup in Arduino when you instantiate Serial is pretty important. If you use 9600 to send for example, you should use the same number to listen.
据我所知,当您实例化 Serial 时,您在 Arduino 中设置的波特率非常重要。例如,如果您使用 9600 发送,您应该使用相同的号码来收听。
Also it's pretty important to send your information as BYTE, otherwise you'll have stuff like \r or \n in the way.
此外,将您的信息作为BYTE发送也非常重要,否则您会遇到 \r 或 \n 之类的东西。
Shorter version, try:
较短的版本,请尝试:
Serial.println(123456789123456789,BYTE);
The simpler the better.
越简单越好。
回答by yadoo86
I think you need to use event driven design patterns to solve this problem. I highly recommend you to visit: http://www.whatisarduino.org/bin/Tutorials/Java+Serial+API+and+Arduino
我认为你需要使用事件驱动的设计模式来解决这个问题。我强烈建议您访问:http: //www.whatisarduino.org/bin/Tutorials/Java+Serial+API+and+Arduino