0

ちょうど今、私はたまたま型変換が複雑すぎました (私はまだそれらの型を完全には理解していません)。
0~1024 の値を 4 バイト (int) として Arduino からシリアル リンク経由で Processing に転送しました。すぐに、短い (2 バイト) を送信して 2 倍の速度で通信できることに気付きました (非常に高速である必要があります)。
だから、これは私がarduinoのC++で持っているものです:

  // variable to store the value coming from the sensor
  unsigned short sensorValue = 0;  
  //Time when I last sent the buffer (serial link seems to need some rest)
  unsigned long last_time_sent = millis();
  //Buffer to save data I've collected
  byte buffer[256];
  //Position in buffer
  byte buffer_pos = 0;

  while(1) {
    //Get 0 - 1024
    sensorValue = analogRead(sensorPin);
    //(Try to) convert Short to two bytes. I don't even which is first and which is last
    for(byte i=0; i<2; i++) {
       //Some veird bit-shifting, all saved in buffer with an offset
       buffer[i+buffer_pos] = (byte)(sensorValue >> ((2-i) * 8));
    }
    //Iterate buffer position
    buffer_pos+=2;
    //Currently, I send the data allways
    if(true||millis()-last_time_sent>30||buffer_pos+2>=255)
      Serial.write(buffer, buffer_pos);
    //Temporary delay for serial link to rest
    delay(50);
  }

さて、Processing では、Java コードは次のようになります。

void serialEvent(Serial uselessParameter) {
  while (myPort.available() >= 2) {
    //java doesn't allow unsigned variables
    short number = 0;
    for(byte i=0; i<2; i++) {
      byte received = (byte)myPort.read();
      println("Byte received: "+Integer.toString((int)received));
      number |= myPort.read() << (2-i)*8;
    }
    //Save data for further rendering
    graph.add(number);  //Array of integers, java doesn't let me make array of short
  }
  //Clean old data
  while(graph.size()>MAX_GRAPH_SIZE)
    graph.remove(0);

}

出力にこれが表示されるため、arduino側で何か問題があると思います:

受信バイト: 0
受信バイト: -1
結果の 2 バイト数: -256

Arduino は約 681 の値を送信する必要があります (おおよその値を確認するために 1 桁のディスプレイがあります)。

4

1 に答える 1