1

I state that I have already written a similar question on StackOverflow thinking that the problem is with my computer software. Anyway, after some shell debugs, I notice that the real issue is on the Arduino Due serial communication. I am trying to send several data (around 500) from an Arduino Due to a computer software. In order to perform some test I write this little software:

  int dle = 10;
  int stx = 2;
  int etx = 3;
  int counter = 0; //It define the sample frequency
  int dataLength = 7; //How many data I have to read
  // The array of byte will be with this structure
  // DLE,STX,Data1HighByte,Data1LowByte,..,Data7LowByte,CounterHigh,CounterLow,DLE,ETX
  //int arrayLength = (dataLength + 1 ) * 2 + 4;
  byte buff[20];
  int val = 0;

  int myTime = 0; //it is used in order to define the sampleFrequency

  void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200, SERIAL_8N1);
    buff[0] = dle;
    buff[1] = stx;
    for (int i = 2; i < sizeof(buff) - 2; i++) buff[i] = 0;
    buff[sizeof(buff) - 2] = dle;
    buff[sizeof(buff) - 1] = etx;

    myTime = millis();
  }

  void loop() {

    //fill the buffer
    for (int c = 0; c < dataLength; c++) {
      val = analogRead(c);
      val = 30 + c; //as a test
      buff[c * 2 + 2] = (byte)((val & 0xFF00) >> 8);
      buff[c * 2 + 3] = ((byte)(val & 0x00FF));
    }
    buff[dataLength * 2 + 2] = (byte)((counter & 0xFF00) >> 8);
    buff[dataLength * 2 + 3] = ((byte)(counter & 0x00FF));

    counter++;

  //print the content on serial
    for (int i = 0; i < sizeof(buff); i++) {
      Serial.write(buff[i]);

    }

    if ( millis() - myTime >= 1000 ) { //if is passed 1 sec, the counter will be reset
     // Serial.println(counter);
     // delay(1000);
      myTime = millis();
      counter = 0; //This identify the sample frequency
    }
  }

As you can see, the arrays of bytes that I send have the form of DLE-STX-DATA-DLE-ETX. Anyway, some data are missing and some other data get strange values. Here are an output example (1000 samples):

    Reading 0 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,44,10,3
    Reading 1 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,45,10,3
    Reading 2 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,46,10,3
    Reading 3 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,47,10,3
    Reading 4 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,48,10,3
    Reading 5 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,49,10,3
    Reading 6 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,50,10,3
    Reading 7 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,51,10,3
    Reading 8 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,52,10,3
    ...
    Reading 173 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,217,10,3
    Reading 174 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,218,10,3
    Reading 175 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,219,10,3
    Reading 176 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,220,10,3
    Reading 177 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,221,10,3
    Reading 178 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,222,10,3
    Reading 179 time: ,0,33,0,34,0,35,0,36,0,170,10,3,10,2,0,30,0,31,0,32
    Reading 180 time: ,0,33,0,34,0,35,0,36,0,171,10,3,10,2,0,30,0,31,0,32
    Reading 181 time: ,0,33,0,34,0,35,0,36,0,172,10,3,10,2,0,30,0,166,10,
    Reading 182 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,167,10,3
    Reading 183 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,168,10,3
    Reading 184 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,169,10,3
    Reading 185 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,170,10,3
    Reading 186 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,171,10,3
    Reading 187 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,172,10,3
    Reading 188 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,173,10,3
    Reading 189 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,174,10,3
    ...
    Reading 607 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,8,10,3
    Reading 608 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,9,10,3
    Reading 609 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,10,10,3
    Reading 610 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,11,10,3
    Reading 611 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,12,10,3
    Reading 612 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,13,10,3
    Reading 613 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,14,10,3
    Reading 614 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,10,30,0
    Reading 615 time: ,31,0,32,0,33,0,34,0,35,0,36,1,145,10,3,10,2,0,30,0
    Reading 616 time: ,31,0,32,0,33,0,34,0,35,0,36,1,146,10,3,10,2,0,30,1
    Reading 617 time: ,140,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 618 time: ,141,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 619 time: ,142,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 620 time: ,143,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 621 time: ,144,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 622 time: ,145,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 623 time: ,146,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 624 time: ,147,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 625 time: ,148,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 626 time: ,149,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    Reading 627 time: ,150,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1
    ...
    Reading 994 time: ,189,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0
    Reading 995 time: ,190,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0
    Reading 996 time: ,191,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0
    Reading 997 time: ,192,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0
    Reading 998 time: ,193,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0
    Reading 999 time: ,194,10,3,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0

As you can see, in this example, Arduino lost some bytes. In addition, the counter has a strange behavior. It, when I "lost" the data jump to another "random" value. For example in the line 178-182

Reading 178 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,1,222,10,3
Reading 179 time: ,0,33,0,34,0,35,0,36,0,170,10,3,10,2,0,30,0,31,0,32
Reading 180 time: ,0,33,0,34,0,35,0,36,0,171,10,3,10,2,0,30,0,31,0,32
Reading 181 time: ,0,33,0,34,0,35,0,36,0,172,10,3,10,2,0,30,0,166,10,
Reading 182 time: ,10,2,0,30,0,31,0,32,0,33,0,34,0,35,0,36,0,167,10,3

it has the value of 1 in high byte and 222 in low byte. Anyway, at the next reading, the value become 0 for high byte and 170 for low byte. And lastly (line 181), it restarts normally to count with 0 in high byte and 166 in low byte.

I try to change the device (I test on an Uno and the problem is the same), the communication protocol (with parity, etc), change the baud and use another PC. The issue is always the same.

  • I suggest you start smaller and work up to your whole program. Begin with just sending the same thing over and over with a counter that increments. That way you will make sure you have a good serial connection to start with. – SDsolar Mar 14 '17 at 19:38
  • I try to send a simple package with this structure: DATA-COUNTER-DLE-ETX. In this test the data is a simple 30, the counter is a counter, and DLE=254, ETX=255. So, the array of bytes is composed of 6 bytes (2 for the data and 2 for the counter). Anway, the problem persist. – youngz Mar 15 '17 at 11:59
  • 1
    How about if you send only the counter and no other data? My point is that you want to try to trap the bug by proving the basics work. – SDsolar Mar 15 '17 at 17:57
  • It can be a problem at the other end of your cable. The code you show seems to be OK. What are you using for capturing data? – user31481 Nov 20 '17 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.