I have a GUI that i use to manage one mega 2560 wich then manages a couple of motors. On my GUI when i press one button it must pass "manual" to the Arduino, wich it does in:
I previously open the serial port (i leave it open always and close it when the system turns off). In manual mode it must let the user interact with the sistem(by acting some manual inputs that it has, setting the motors like the user wants).
The photograph, get_action, scan_action and apply refer to the user pressing buttons and those buttons be interpreted by the arduino to act the outputs.
My compare_input function:
char compare_input(char* This)
{
while (Serial.available() > 0)
{
if (index < 10)
{
inChar = Serial.read();
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
if (strcmp(inData, This) == 0)
{
for (int i = 0; i < 10; i++)
{
inData[i] = 0;
}
index = 0;
return (0);
}
else
return (1);
}
My problem is, i give the arduino the string "manual". It goes into manual mode. I perform the actions (photograph, etc) several times but sometime, somewhere it stops. When in my GUI i click stop it must pass "clear" to Arduino in order to, like it shows in the code, stop manual mode but when it gliches it stops working saying that the device connected to the program (GUI) is not working.
You read all waiting serial data. What if there is none? What if "man" has arrived. but the "ual" hasn't?
You REALLY, REALLY need to send some kind of end of packet marker. You need to collect the serial data, whenever it arrives, in an array. When the serial stream includes the end of packet marker, THEN you use the stored data.
for (int i = 0; i < 10; i++)
{
inData[i] = 0;
}
One STOP sign is enough. More than that makes it clear that you don't understand the concept of NULL terminated. Every time you add a character to the array, you should add a NULL in the next position.
Have look at the examples in serial input basics. Example 2 uses an end-marker and example 3 uses start- and end-markers. There is also a parse example.
There is no advantage sending "manual" to the Arduino - it does not convey any more information than a simple 'm'. Sending commands as single characters would make the Arduino code much simpler.
Thank you for your replies. i only had time to check it now.
I changed it to work with only one char. Now i have some trouble.
It works fine sometimes but then i get the same error. I was thinking it might be needing flow control. How can i assure that the data (my char. lets say "m") is sent and arrives at the exact same time in the instruction on the arduino. What can i do to guarantee it doesnt get lost because when it arrived at the Arduino he was doing something else?
xptandre7:
How can i assure that the data (my char. lets say "m") is sent and arrives at the exact same time in the instruction on the arduino.
There is no need to worry about exact time. If you send "" and use the 3rd example in serial input basics you should should have reliable communication. The < and > enable the Arduino to know when the data starts and when it ends - so it knows it has all the data.
If that does not work for you post your latest code.
What can i do to guarantee it doesnt get lost because when it arrived at the Arduino he was doing something else?
You can't guarantee anything about serial communications. Bytes get lost, occasionally. You can ensure that data doesn't get lost because the incoming buffer gets full by keeping the messages short AND reading incoming data as rapidly as possible. That means NO delay()s.