JavaのRXTXとArduinoの間のシリアル通信
-
18-09-2019 - |
質問
私は、シリアルポートを使用して、Arduinoのプロと私のPC(Windows 7を使用してNetBeansとRXTX)との間で通信しようとしています。 Arduinoのは、実際にはFTDIのケーブルを使ってPCに接続されています。
のコードは、Java SimpleRead.Javaに基づいて、ここでを見つけました。 の
それは起動時に現在のArduinoは、単純に文字列を出力します。私のJavaプログラムが読み込まれたバイト数を印刷して、内容を印刷する必要があります。 Javaプログラムの動作、並べ替えの...
の文字列は、出力がアップ壊れてしまいます(> 10バイト程度)長い場合。
ArduinoのIプリント上のであれば、
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
。1つの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を使用していないが、私はアルドゥイーノと処理でプレーしたし、それがアルドゥイーノから/書き込み値を読み取るために非常に簡単です。 ここでの処理が付属して読取サンプルがある([ファイル]> [例>ライブラリ>シリアル> 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のようなものがあるでしょう、の BYTE のように情報を送信するためにはかなり重要です。
短いバージョン、試してみてください。
Serial.println(123456789123456789,BYTE);
より良いシンプルな。
私はあなたがこの問題を解決するには、イベント駆動型のデザインパターンを使用する必要があると思います。私は非常に訪問することをお勧めします:ます。http://www.whatisarduino。 ORG / binに/チュートリアル/ Javaの+シリアル+ API +と+ Arduinoのの