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:
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.