title: Serial Port communication with Arduino and C++
body: <p>I am having a problem with a Serial Port communication between Arduino Nano and C++, even though the problem is in C++ side. Basically I want to send integers (or long,...) from the Arduino to a C++ program to be processed. </p>
  
  <p>First I did a test sending information from the Arduino to the computer using Matlab. The Arduino code is pretty simple:</p>
  
  <pre><code>int i = 0;
  
  void setup() {
  
     // start serial port at 9600 bps:
     Serial.begin(9600);
     establishContact(); 
  }
  
  void loop() {
    Serial.println(i);
    i=i+1;  
    delay(10);
  }
  
  void establishContact() {
     while (Serial.available() &lt;= 0) {
       Serial.println('A', BYTE);
       delay(10);
     }
  }
  </code></pre>
  
  <p>The Matlab side is also simple: </p>
  
  <pre><code>clc;
  clear all;
  numSec=2;
  t=[];
  v=[];
  
  s1 = serial('COM3');    % define serial port
  s1.BaudRate=9600;               % define baud rate
  set(s1, 'terminator', 'LF');    % define the terminator for println
  fopen(s1);
  
  try                             % use try catch to ensure fclose
                                  % signal the arduino to start collection
      w=fscanf(s1,'%s');              % must define the input % d or %s, etc.
      if (w=='A')
          display(['Collecting data']);
          fprintf(s1,'%s\n','A');     % establishContact just wants 
                                      % something in the buffer
      end
  
      i=0;
      t0=tic;
      while (toc(t0)&lt;=numSec)
          i=i+1;
          t(i)=toc(t0);
          t(i)=t(i)-t(1);
          v(i)=fscanf(s1,'%d');     
      end
  
      fclose(s1);
      plot(t,v,'*r')   
  
  catch me
      fclose(s1);                
  end       
  </code></pre>
  
  <p><strong>My goal is</strong>, with C++, do the same that is done in Matlab using fscanf(s1, '%d'). </p>
  
  <p>Here is the current code that I am using (C++ code):</p>
  
  <pre><code>void main()
  {
   HANDLE hSerial;
   hSerial = CreateFile(TEXT("COM3"), 
     GENERIC_READ | GENERIC_WRITE, 
     0,
     NULL, 
     OPEN_EXISTING,
     FILE_ATTRIBUTE_NORMAL,//FILE_FLAG_OVERLAPPED, 
     NULL);
  
  
  
  if ( hSerial == INVALID_HANDLE_VALUE)
   {
    printf("Error initializing handler");
   } 
   else 
   {
  
    // Set the parameters of the handler to the serial port.
    DCB dcb = {0};
  
    dcb.DCBlength = sizeof(dcb);
  
    if ( !GetCommState(hSerial, &amp;dcb) )
    {
     printf("Error setting parameters");
    }
  
    FillMemory(&amp;dcb, sizeof(dcb), 0);
    dcb.BaudRate = CBR_9600;
    dcb.ByteSize = 8;
    dcb.StopBits = ONESTOPBIT;
    dcb.Parity = NOPARITY;
  
    if ( !SetCommState(hSerial, &amp;dcb) )
    {
     // error setting serial port state.
    }
  
    // Tell the program not to wait for data to show up
    COMMTIMEOUTS timeouts = {0};
  
    timeouts.ReadIntervalTimeout = 0;//20;
    timeouts.ReadTotalTimeoutConstant = 0;//20;
    timeouts.ReadTotalTimeoutMultiplier = 0;//50;
    timeouts.WriteTotalTimeoutConstant = 0;//100;
    timeouts.WriteTotalTimeoutMultiplier = 0;//100;
  
    if ( !SetCommTimeouts(hSerial, &amp;timeouts) )
    {
     printf("Error setting the timeouts");
  
    }
  
    char szBuff[5] = "";
    DWORD dwBytesRead = 0;
    int i = 0;
    char test[] = "B\n";
    int maxSamples = 10;
    DWORD dwCommStatus;
  
    WriteFile(hSerial, test, 2, &amp;dwBytesRead, NULL);
  
    SetCommMask(hSerial,EV_RXCHAR);
  
    while (i &lt; maxSamples)
    {
     WaitCommEvent (hSerial, &amp;dwCommStatus, 0);
  
     if (dwCommStatus &amp; EV_RXCHAR) 
     {
      memset(szBuff,0,sizeof(szBuff));
      ReadFile(hSerial, LPVOID(szBuff), 4, &amp;dwBytesRead, NULL);
  
      cout&lt;&lt;szBuff;
      printf(" - %d - \n", atoi(szBuff));
     }
     i++;     
    }
  
    scanf("%d", &amp;i);
  
    CloseHandle(hSerial);
   }
      }
  </code></pre>
  
  <p>The goal of my code would be something like <code>num = ReadSerialCOM(hSerial, "%d");</code> </p>
  
  <p>My current C++ code reads the information from the buffer, but there is not an accepted end of line, which implies that my numbers (integers) are received cut. </p>
  
  <p>Eg:</p>
  
  <p>I send 8889 from the Arduino, which places it in the COM port. And the command <code>ReadFile</code> saves '88' into <code>szBuff</code>. At the next iteration '89\n' is saved into <code>sZBuff</code>. Basically I want to avoid to post-process <code>sZBuff</code> to concat '88' and '89\n'.</p>
  
  <p>Anyone? 
  Thanks!</p>