首页 > 解决方案 > Can't Send more Than 64 Byte to Arduino

问题描述

I need to communicate with Arduino from PC.

I have C# code. I send 87 bytes to the Arduino.

C# Code

byte[] MyBuffer = new byte[87];
// I initialize buffer with bytes(0-255) details are not important.
serialport1.Write(MyBuffer, 0, 87);

Arduino part is the most important. Because I am going to split these bytes to 5 different arrays.

byte FirstBlock[16];
byte SecondBlock[16];
byte ThirdBlock[16];
byte FourthBlock[16];
byte FifthBlock[16];
byte ReceivedBuffer[87];

int counter = 0

bool SYNC = false;

void loop(){
if(Serial.available())
   CropBytes();

if(SYNC == true){
   SplitBytes();
   DoSomeStaffWithBlocks();
   SYNC = false;
} } //End Loop

void CropBytes(){
   while(Serial.available()){
       byte ReceivedByte = Serial.read();
       ReceivedBuffer[counter] = ReceivedByte;
       // byte 35 means my byte transfer is over for ReceivedBuffer[86]

       if(counter == 86 && ReceivedBuffer[counter] == 35){
             SYNC = true;
             counter = 0;
        }
        else{
             counter = counter + 1;
        }
    }
}

void SplitBytes(){
  for (int i = 5; i < 21; i++)
  {
    FirstBlock[i - 5] = ReceivedBuffer[i];
  }

  for (int i = 21; i < 37; i++)
  {
    SecondBlock[i - 21] = ReceivedBuffer[i];
  }

  for (int i = 37; i < 53; i++)
  {
    ThirdBlock[i - 37] = ReceivedBuffer[i];
  }

  for (int i = 53; i < 69; i++)
  {
    FourthBlock[i - 53] = ReceivedBuffer[i];
  }

  for (int i = 69; i < 85; i++)
  {
    FifthBlock[i - 69] = ReceivedBuffer[i];
  }
}

The problem - FirstBlock[], SecondBlock[], ThirdBlock[] received successfully no problem. But after ThirdBlock, FourthBlock(after 11th byte) and FifthBlock(all bytes) arrays don't initialize successfully.

I don't know where is the problem.

标签: c#arraysarduinoserial-port

解决方案


推荐阅读