简体   繁体   中英

Rapid Serial Port writing to Arduino from Processing

I need some help speeding up writing to serial. I found a few similar questions to this but nothing that dealt with the Processing language or Java so I'm hoping someone can help me with this issue I have.

edit

As John points out below, it appears serial just isn't fast enough to send this much data at the speed I want. Does anyone know of other arduino interfaces that are available?

end edit

I am using an arduino to control a grid of 400 RGB LEDs I have hooked up. To send commands to the arduino I wrote a small program in Processing that manipulates a large array that represents the LEDs. I am then attempting to update the grid by sending 800 bytes of data to the arduino every 20ms at 115200 baud over serial. The Processing code that is called every 20ms is:

  noStroke();
  int dataPos = 0; // position in LED data array
  byte[] dataLedGrid = new byte[400*2]; // array for bytes to send
  for(int j=0; j<LEDS_TALL; j++) {
    for(int i=0; i<LEDS_WIDE; i++) {
      int pos = j*20+i;
      int r = ledGrid[LEDS_WIDE-i-1][LEDS_TALL-j-1][0], g = ledGrid[LEDS_WIDE-i-1][LEDS_TALL-j-1][1] ,b = ledGrid[LEDS_WIDE-i-1][LEDS_TALL-j-1][2];
      int colorData = ((g & 0x1F) << 10) | ((b & 0x1F) << 5) | (r & 0x1F);
      dataLedGrid[dataPos] = byte(colorData & 0x00FF);
      dataLedGrid[dataPos+1] = byte(colorData & 0xFF00);
      dataPos+=2;

      // draw LED squares on gui
      fill(ledGrid[i][j][0], ledGrid[i][j][1], ledGrid[i][j][2]);
      rect(SIDE_PANEL_WIDTH+(LED_SQUARE_SIDE+LED_SQUARE_SPACING)*i+HORIZONTAL_MARGIN,
      (LED_SQUARE_SIDE+LED_SQUARE_SPACING)*j+VERTICAL_MARGIN, 
      LED_SQUARE_SIDE, LED_SQUARE_SIDE);
    }
  }
  myPort.write(dataLedGrid); // write to serial

On the arduino I have a 1D array (Display) that represents the grid on the arduino side. The loop code is:

void loop() {

  unsigned int pos, c1, c2;

  if (Serial.available() > 0) {
    for(byte j=0; j<20; ++j) {
        for(byte i=0; i<20; ++i) {
          c1 = Serial.read();
          c2 = Serial.read();

          pos = i+20*j;
          if(j % 2 != 0)         // it's a square of leds created by a zigzaging line
            pos = 20*(j+1)-i-1;  // so I have to reverse every other line

          Display[pos] = (unsigned int)(c1<<8 | c2);
        }
    }
    show();
  }
}

Now the code itself works fine but when the serial writing slows everything down. When I run the Processing code without the serial writing everything is fine an runs at the intended speed. However, when I add the serial writing in, everything becomes slightly choppy. The CPU doesn't max out or anything so I'm assuming it's the serial.write method I am calling. What can I do to speed up this code or remove the lag from serial writing?

Thanks for your help!

Do the math.

115200 baud is, at 8-N-1, 11,520 bytes per second, or 86.8 usec/byte.

In 20 msec, you can send 230.4 bytes. Sending 800 bytes will take about 70 msec.

Trying to send 800 bytes at 115200 baud every 20 msec isn't going to work.

尝试将Serial.setTimeout(0)添加到arduino sketch的setup()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM