Hello community. Once again I'm up against the wall and I need your help. First I want to mention that I already read all the general serial communication tips, guides and the basic's page. Yes, I'm a beginer, this is only my second project.
Last year I did a motor control with VB.NET application and I had troubles reading the incoming serial data as String. Huge mistake, thanks to you I switched to char with only 1 symbol and it worked perfectly in timely manner.
This year I need the same, so I stepped on my previous project and I wanted simply to change the action after a command. It turned the communication is no longer working properly, again, using the very very simple 1 symbol char commands. It freezes after the first received command. As in later stage, I'd have a SIM900 chip connected and I'd need to send phone numbers, I decided directly to go for the multi-character solution with '\n', just like it's explained and written in Serial Basics tutorial. It didn't work so I simplified and simplified and simplified so right now all I have the exact copied program from the tutorial with the only addition of adding a diode so I can debug.
int gLed = 13;
int yLed = 12;
int rLed = 11;
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
ndx = 0;
newData = true;
}
}
}
void dataTest() {
if (newData == true && receivedChars == 'red') {
digitalWrite(rLed,HIGH);
delay(2000);
newData = false; }
else if (newData == true && receivedChars == 'green') {
digitalWrite(gLed,HIGH);
delay(2000);
newData = false; }
else {
digitalWrite(yLed,HIGH);
delay(2000); }
//newData = false; }
}
void setup() {
Serial.begin(9600);
//Serial.setTimeout(200);
pinMode(gLed, OUTPUT);
pinMode(yLed, OUTPUT);
pinMode(rLed, OUTPUT);
}
void loop() {
recvWithEndMarker();
dataTest();
}
My VB.NET commands are pretty simple as well:
spArduino.Open()
spArduino.WriteLine("red\n")
spArduino.Close()
I debugged recvWithEndMarker() and it's working. It looks like I do receive the data but I cannot read it.
I assume this communication represents at least half the issues you're dealing with here.