0
votes

I use the SerialClass.h and Serial.cpp in this link: http://playground.arduino.cc/Interfacing/CPPWindows

My main.cpp:

#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h"    // Library described above
#include <string>

// application reads from the specified serial port and reports the collected data
int main(int argc, _TCHAR* argv[])
{

    printf("Welcome to the serial test app!\n\n");

    Serial* SP = new Serial("COM4");    // adjust as needed

    if (SP->IsConnected())
        printf("We're connected\n");

    char incomingData[256] = "hello";
    int dataLength = 255;
    int readResult = 0;

    while(SP->IsConnected())
    {
        readResult = SP->ReadData(incomingData,dataLength);
        incomingData[readResult] = 0;

        if(readResult != 0){
            printf("%s",incomingData);
             printf("---> %d\n",readResult);
        }
        Sleep(500);
    }
    return 0;
}

My arduino code:

int mySize = 5;
char incomingData[256] = "hello";

void setup (){
  Serial.begin(9600); // Seri haberleşmeyi kullanacağımızı bildirdik
  pinMode(LedPin, OUTPUT); //LedPini çıkış olarak tanımlıyoruz.
}

void loop (){

    incomingData[mySize] = 't';
    ++mySize;

    Serial.write(incomingData);

    delay(500);
}

Arduino writes character array and C++ reads it. The issue is sometimes cpp is missing the data. My output:

output

My First question is what can I do for this? How to do synchronization between Arduino and C++? C++ should wait, until arduino to finish writing. I think I should use lock system or something like that.

And other question. I want to my Arduino and C++ program are constantly communicating. I want to make that: "Arduino writes" After "C++ reads" after "C++ writes" after "Arduino reads" after again "Arduino writes". So, I don't use sleep and delay. My Second question is how can I do for this synchronization? I think answer is same like the answer of the first question.

1

1 Answers

1
votes

The C++ class you are using doesn't implements own internal buffers, it relies on hardware buffer and OS driver buffer. OS driver buffer could be increased (Device Manager -> Ports -> driver properties -> Port Settings)

enter image description here

There is Sleep(500) delay in your receiving code. Now imagine that during such a 500ms delay UART hardware and software driver buffers are filled. But your code is 'sleeping' and didn't read buffered data. Any data received during this period will be discarded. Since Windows is not real-time OS, from time to time your windows process didn't get enough time-slice (because there are many other processes) and during such extended inactivity data could be lost. So remove that Sleep(500).

To ensure reliable communication, receiving part must buffer the data as soon as it detects new data (usually in separate thread, could have higher priority). Main processing logic shall work with that buffered data.

Also you shall implement some kind of protocol, with at least following 2:

  • message format (starting, ending, size etc)
  • message integrity (is data received without corruption, could be simple checksum)

Also some kind of transmission control would be nice (timeouts, reply / acknowledge if any).

UPD: in Arduino code Serial.write(incomingData); make sure that incomingData is properly zero-terminated. And add upper bound check for mySize...