title: Wrong data transmission in serial communication between Arduino and Raspberry
body: <p>I'm trying to sendo datas from Arduino Uno to RaspberryPi 3B. I need to send 14 int (max value: 6000) from Arduino as soon as Raspberry ask for them. Each one of this 14 number came from ad ADC (SPI communication).</p>
  
  <p>ARDUINO SIDE</p>
  
  <pre><code>#include "SPI.h"
  byte incomingByte = 0;   // for incoming serial data
  
  const int N_SENSORI=14;
  const int DATAOUT = 11;
  const int DATAIN = 12;
  const int SPICLOCK = 13;
  const int SLAVESELECT = 10;
  
  //===============SPI COMMUNICATION================
  short write_read_spi16(short what) {
    digitalWrite(SS, LOW);
    short res = SPI.transfer16(what);
    digitalWrite(SS, HIGH); 
    return res;
  }
  
   //===============CONVERT INT IN BYTE AND SEND IT================
  void longInt2Byte( int x){
    unsigned char buf[sizeof( int)];
    memcpy(buf,&amp;x,sizeof(int));
  Serial.write(buf,sizeof(buf));
  
  }
  //=======================================
  
  void setup() {
  
    Serial.begin(115200);     
    SPI.begin();
    pinMode(DATAOUT, OUTPUT);
    pinMode(DATAIN, INPUT);
    pinMode(SPICLOCK,OUTPUT);
    pinMode(SLAVESELECT,OUTPUT); 
  }
  void loop() {
  
  
  if (Serial.available() &gt; 0) {         
    incomingByte= Serial.read();
  
    if (incomingByte=='E') {
  
      write_read_spi16(0b1000001101110000);  //THIS IS FOR THE ADC COMMUNICATION
  
      for (int i = 1; i&lt;N_SENSORI+1; i++) { //DONE 14 TIMES.
        short s = write_read_spi16(0b1000001101110000 | (i&lt;&lt;10));
        int Data = s&amp;0xFFF;
        longInt2Byte(Data);
          }       
        }
   }}
  </code></pre>
  
  <p>// C++ SIDE</p>
  
  <pre><code>void stimulationController::SetupDario(){
  
      cout&lt;&lt;"Dentro al setupDario"&lt;&lt;endl&lt;&lt;flush;
      buffer=new char [1000];
      const char* _portNameSensorsDario="/dev/ttyACM1";
      //SERIAL PORT FOR HAND SENSORS
      struct termios options;
      SerialHandleSensors=open(_portNameSensorsDario, O_RDWR | O_NOCTTY | O_NDELAY); //SerialHandleSensors=open(_portNameSensors, O_RDWR | O_NOCTTY | O_NDELAY); non blocking
      if (SerialHandleSensors == -1 )
      {
          cout&lt;&lt;endl&lt;&lt;"......ERROR: Unable to open: "&lt;&lt;_portNameSensorsDario&lt;&lt;endl;
          return;
      }
      else
      {
          fcntl(SerialHandleSensors, F_SETFL,0);
          cout&lt;&lt;"......OPENED PORT: Succesfully opened: "&lt;&lt;_portNameSensorsDario&lt;&lt;endl;
      }
  
      //GET THE OPTIONS, MODIFY AND SET
      tcgetattr(SerialHandleSensors,&amp;options);
      cfsetispeed(&amp;options,B115200); //BAUD RATE IN
      cfsetospeed(&amp;options,B115200); //BAUD RATE OUT
      // options.c_lflag |= (ICANON | ECHO | ECHOE);
      options.c_cflag |= (CLOCAL | CREAD);
      options.c_cflag &amp;= ~PARENB;
      options.c_cflag |= CSTOPB;
      options.c_cflag &amp;= ~CSIZE;
      options.c_cflag |= CS8;
      tcsetattr(SerialHandleSensors,TCSANOW,&amp;options);
  
      usleep(3000000);
  cout&lt;&lt;"Fine Setup"&lt;&lt;endl&lt;&lt;flush;
  }
  
  
  
  int stimulationController::Dario( )
  {
  
      {
      cout&lt;&lt;" NUOVA FUNZIONE"&lt;&lt;endl;
  
  
  unsigned char bytes[4];
      bytes[0]=0x45;  // 'E'
  
      int tempWrite=write(SerialHandleSensors,bytes,1);
      if(tempWrite==-1)
      {
          cout&lt;&lt;"......ERROR: PROBLEM WRITING TO ROBOTIC HAND"&lt;&lt;endl;
          failure=true;
          return 0; 
  
      }
  
      int value=0;
  
  
      int tempRead=read(SerialHandleSensors,buffer,28);
      if(tempRead==-1)
      {
          cout&lt;&lt;"......ERROR: PROBLEM READING FROM ROBOTIC HAND"&lt;&lt;endl;
          failure=true;
          return 0;
      }
  
  
  int j=0;
  for( int i=0; i&lt;28; i=i+2){
      value= buffer[i] | ((int)buffer[i+1]&lt;&lt;8);
      //ensorDataFoot.push_back(value);  //Aggiunge un elemento
      j=j+1;
          cout&lt;&lt;"Dato "&lt;&lt;j  &lt;&lt;"vale: "&lt;&lt;value&lt;&lt;endl&lt;&lt;flush;
      }
  
      return value;
  
  }
  }
  </code></pre>
  
  <p>The problem is that or the raspberry side some time it prints the right values, some time (the most of the times, actually) it doesn't. Data seems to be repeated or (2500 insted of 2000 for example).</p>
  
  <p>I can't figure it out the problem.Could it be a matter of timing between the request of sending data and the reading? if so, there is a way to get rid of it?</p>
  
  <p>I've added a do-while cycle in order to be sure that all the 28 bytes are read</p>
  
  <pre><code>int TotByte=28;
  int ByteRead=0;
  int TempRead=0;
  
  do{ TempRead= read(SerialHandleSensors, buffer, (TotByte-ReadByte));
  ReadByte= ReadByte+TempRead;
  cout&lt;&lt;"ReadByte is: "&lt;&lt;ReadByte&lt;&lt;endl&lt;&lt;flush;
  }while(ByteRead&lt;TotByte);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>ReadByte is: 5
  ReadByte is: 15
  ReadByte is: 25
  </code></pre>
  
  <p>and then it stay like that without doing anything</p>