Отправка большого массива из Arduino на ПК - PullRequest
0 голосов
/ 06 октября 2019

Я пытаюсь отправить байтовый массив из Arduino из-за приложения в форме окна C #. Проблема в том, что не полный массив прибывает. Первые 100-200 байтов верны, но все остальные равны нулю. Это совершенно случайно, насколько они верны. Вот мой код arduino abd C #.

if (commandString.equals("IMPOR")) {
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, LOW);
  byte buf[2048];
  for (int i = 0; i < 1024; i++) {
    if (Serial.availableForWrite() > 2) {
      byte lb = SRAM.readByte(2 * i);
      byte hb = SRAM.readByte(2 * i + 1);
      int16_t raw2 = word(hb, lb);
      buf[2 * i] = raw2 & 255;
      buf[2 * i + 1] = (raw2 >> 8)  & 255;
    }
  }
  Serial.write(buf,sizeof(buf));
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);
}

И c #:

int[] bdata = new int[1024];
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        int bytes = 2048; // ComPort.BytesToRead;
        byte[] buffer = new byte[bytes]; //Párosak a low bytok. Páratlanok a hugh byteok
        ComPort.Read(buffer, 0, bytes);
        int[] intbuffer = Array.ConvertAll(buffer, x => (int)x);
        //int[] bdata = new int[bytes / 2];
        for(int i = 0; i < 1024; i++)
        {
            bdata[i] = intbuffer[2 * i + 1] * 256 + intbuffer[2 * i];
        }
    }

DatarectainedTreshlod установлен на 2048. Вот картинка из отладки: Debug

Кто-нибудь знает, в чем может быть проблема? SRAM - 23LCV512.

...