シリアルポートを使用して、PC(NetbeansとRXTXを使用するWindows 7)とArduinoProの間で通信しようとしています。Arduinoは実際にはFTDIケーブルを使用してPCに接続されています。
このコードは、ここにあるJavaSimpleRead.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
デバッガーを使用して手動でコードを実行すると、結果の文字列は常に本来あるべき状態、つまり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");
}
}