12

シリアルポートを使用して、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");
    }
}
4

3 に答える 3

7

シリアルデータは単なるデータの流れです。いつ読み取るか、および発生しているバッファリングによっては、読み取るときにデータの一部しか使用できない場合があります。

ライン指向のデータを使用しているので、ラインターミネータが表示されるまでデータをバッファリングしてから、データを処理します。

于 2010-01-05T22:11:52.193 に答える
3

私はJavaRXTXを使用していませんが、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
}

*/

私が覚えている限り、シリアルをインスタンス化するときにArduinoで設定するボーは非常に重要です。たとえば、9600を使用して送信する場合は、同じ番号を使用して聞く必要があります。

また、情報をBYTEとして送信することも非常に重要です。そうしないと、\rや\nのようなものが邪魔になります。

短いバージョン、試してみてください:

Serial.println(123456789123456789,BYTE);

シンプルであるほど良い。

于 2010-01-06T11:28:40.883 に答える
1

この問題を解決するには、イベント駆動型のデザインパターンを使用する必要があると思います。http://www.whatisarduino.org/bin/Tutorials/Java+Serial+API+and+Arduinoにアクセスすることを強くお勧めします 。

于 2012-02-18T23:14:25.893 に答える