Hello,
im trying to figure out how to interface arduino to take commands from a serial port (win, putty serial mode)
i would like arduino to do smthing when i type the "specific word", and clear the buffer after i hit enter.
thanks,
xvjeko
void serialkey(char keycom[128]) //key combination i want to be pressed on a pc keyboard
{
int i;
int keylen;
int ipass=1;
int securevariable=0;
char input[128];
keylen=strlen(keycom);
Serial.flush();
while(securevariable == 0)
{
for(i=0;i<128;i++)
{
while(Serial.available()==0);
input[i] = Serial.read();
if(Serial.read()=='\r') Serial.flush();
}
for(i=0;i<keylen;i++)
{
if(input[i] == keycom[i]) ipass++;
if(ipass == keylen) securevariable = 1;
}
}
}
Then, read a character (removing it from the buffer) and storing it into the input array. Fair enough.
Then read another character and compare it to a carriage return. If it is, then clear the serial buffer.
Then throw it away.
You'll be reading every other character until you press return, at which point all the rest of your input is ignored. And then what? It just carries on for the rest of the requested 128 characters.
You need to read the serial character into a temporary char variable and then decide what to do with it.
Something more like:
int i=0;
char buffer[128];
bool complete = false;
char in;
while(complete==false)
{
if(Serial.available())
{
in = Serial.read();
switch(in)
{
case '\r':
complete = true;
break;
// add other character handling in here
default:
if(i<126)
{
buffer[i++] = in;
Serial.print(in);
}
break;
}
}
}
This will echo the characters back to you as you are typing, so you can see what you are doing. You can also add extra handling in there for other characters, like backspace, so you can make corrections as you type.