简体   繁体   English

快速串口从处理写入Arduino

[英]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. 我发现了一些类似的问题,但没有涉及Processing语言或Java的任何问题,所以我希望有人可以帮我解决这个问题。

edit 编辑

As John points out below, it appears serial just isn't fast enough to send this much data at the speed I want. 正如John在下面指出的那样,看起来串行的速度不够快,无法以我想要的速度发送这么多数据。 Does anyone know of other arduino interfaces that are available? 有没有人知道其他可用的arduino接口?

end edit 结束编辑

I am using an arduino to control a grid of 400 RGB LEDs I have hooked up. 我正在使用arduino控制我连接的400个RGB LED网格。 To send commands to the arduino I wrote a small program in Processing that manipulates a large array that represents the LEDs. 为了向arduino发送命令,我在Processing中编写了一个小程序来操作代表LED的大型数组。 I am then attempting to update the grid by sending 800 bytes of data to the arduino every 20ms at 115200 baud over serial. 然后,我尝试通过串行发送800字节的数据,每隔20分钟以115200波特率向arduino发送更新网格。 The Processing code that is called every 20ms is: 每20ms调用的处理代码是:

  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. 在arduino上我有一个数组(显示)代表arduino一侧的网格。 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. 当我在没有串行写入的情况下运行Processing代码时,一切都很好,以预期的速度运行。 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. CPU没有最大化或任何其他所以我假设它是我调用的serial.write方法。 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. 115200波特,8-N-1,每秒11,520字节,或86.8 usec /字节。

In 20 msec, you can send 230.4 bytes. 在20毫秒,您可以发送230.4字节。 Sending 800 bytes will take about 70 msec. 发送800个字节大约需要70毫秒。

Trying to send 800 bytes at 115200 baud every 20 msec isn't going to work. 尝试每20毫秒以115200波特率发送800个字节是行不通的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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