I am using this header: http://playground.arduino.cc/Interfacing/CPPWindows
My main:
#include <iostream>
#include <stdio.h>
#include <tchar.h>
#include "SerialClass.h"
#include <string>
int main(int argc, _TCHAR* argv[])
{
Serial* SP = new Serial("\\\\.\\COM4");
if (SP->IsConnected())
printf("We're connected\n");
std::string tryStr;
int value;
DWORD bytesSend;
/* Loop */
while(SP->IsConnected())
{
/* Take a value from user */
printf("Value: "); scanf("%d",&value);
/* Send Arduino */
BOOL retVal = WriteFile(SP->hSerial, &value, sizeof(value), &bytesSend, NULL);
/* I have no idea what is this for? (SP->status.cbInQue) */
ClearCommError(SP->hSerial, &SP->errors, &SP->status);
/* Read string from arduino */
DWORD bytesRead;
tryStr.assign("");
bool check = true;
char tempCharacter;
while (check==true){
ReadFile(SP->hSerial, &tempCharacter, sizeof(char), &bytesRead, NULL);
tryStr += tempCharacter;
if(tempCharacter == '$'){
check = false;
}
}
/* Print the string */
std::cout << tryStr << std::endl << std::endl ;
}
return 0;
}
My Arduino Code:
int LedPin= 10;
int value;
String incomingData = "hello$";
void setup (){
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
}
void loop (){
while(!(Serial.available() > 0)){
/*Wait until take data */
}
value=Serial.read();
if (value == 20)
{
digitalWrite (LedPin, HIGH);
incomingData.setCharAt(0,'o');
incomingData.setCharAt(1,'p');
incomingData.setCharAt(2,'e');
incomingData.setCharAt(3,'n');
incomingData.setCharAt(4,'n');
Serial.print(incomingData);
}
else if (value == 15)
{
digitalWrite (LedPin, LOW);
incomingData.setCharAt(0,'c');
incomingData.setCharAt(1,'l');
incomingData.setCharAt(2,'o');
incomingData.setCharAt(3,'s');
incomingData.setCharAt(4,'e');
Serial.print(incomingData);
}
}
This code works perfectly. Output:
We're connected Value: 20 openn$
Value: 15 close$
Value: 20 openn$
Value: 20 openn$
Value: 15 close$
Value: 15 close$
Value: 20 openn$
But now I want to add another condition in my Arduino code:
int LedPin= 10;
int value;
String incomingData = "hello$";
void setup (){
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
}
void loop (){
while(!(Serial.available() > 0)){
/*Wait until take data */
}
value=Serial.read();
if (value == 20)
{
digitalWrite (LedPin, HIGH);
incomingData.setCharAt(0,'o');
incomingData.setCharAt(1,'p');
incomingData.setCharAt(2,'e');
incomingData.setCharAt(3,'n');
incomingData.setCharAt(4,'n');
Serial.print(incomingData);
}
else if (value == 15)
{
digitalWrite (LedPin, LOW);
incomingData.setCharAt(0,'c');
incomingData.setCharAt(1,'l');
incomingData.setCharAt(2,'o');
incomingData.setCharAt(3,'s');
incomingData.setCharAt(4,'e');
Serial.print(incomingData);
}
else{ // new line added
// new line added
incomingData.setCharAt(0,'g'); // new line added
incomingData.setCharAt(1,'g'); // new line added
incomingData.setCharAt(2,'g'); // new line added
incomingData.setCharAt(3,'g'); // new line added
incomingData.setCharAt(4,'g'); // new line added
// new line added
Serial.print(incomingData); // new line added
} // new line added
}
But it didn't work. ScreenShot:
What went wrong? The led blinked correctly but the output string is wrong.