Hi,
I finished with my arduino sketch that provides a set of commands for my Linux box to use to read sensors, run servo motors etc.
Commands have this format: @xxxx\n
For instance to read my Ping))) my Linux box issues (after opening the serial port as a file):
fprintf("@sp\n");
Where 's' means the query goes to the sensor array, and 'p' specifies the Ping))) sensor I have. This works correct now. One problem I dealt with was using:
Serial.print(pingOutput);
that made my Linux box to wait forever for a line termination to arrive! Then I changed it to:
Serial.println(pingOutput);
and world became a happy place
now there is a new problem I don't have any idea how to solve:
I decided to test if I can read my Ping))) 10 times with an interval of 1 second:
and the Linux box waits forever, as if for a line termination to arrive! I have for sure "Serial.println(pingOutput)" so what may be the reason please?
Assuming your MAXCMDLEN is large enough (couldn't find it in the source, but I'm sure it's > 3), the only possible problem I'm seeing so far is the following.
You are comparing an uninitialized variable:
char c;
int i = 0;
while( Serial.available()>0 && [glow]c != '\n'[/glow] && i < MAXCMDLEN) //MAXCMDLEN
You should not only define, but also assign c a value. Or you could change the while loop to look somewhat like this (untested)
while( Serial.available()>0)
{
c = Serial.read();
if(c == '\n' || i >= MAXCMDLEN) break;
cmdbuf[i++] = c;
}
Well I changed that, and now initialize the variable. It didn't work either. Then to exclude any possible effect of other parts of the code, I designed a new sketch from scratch...
Arduino new sketch:
char c;
char test[10];
int icnt;
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available()>0 && Serial.read()=='@')
{
delay(10);
c = Serial.read();
while( c!='\n' && icnt<10)
{
test[icnt++] = c;
c = Serial.read();
}
for(int ist = 0; ist < icnt; ist++)
Serial.print(test[ist]);
Serial.println();
icnt = 0;
}
}
Linux box new program:
char buffer[256];
//.....
//motorController file descriptor opened, and all other setup...
//....
while(1==1)
{
fprintf(motorController, "@testing\n" );
fscanf(motorController, "%s", buffer);
printf("Pinged: %s\n", buffer);
bzero(buffer,256);
sleep(1);
}
same as before:
if I exclude the while loop in the Linux box, it works fine, i.e. I sent "@testing\n", and I'll get back "testing". Then I return to my while and it works again one time, and then waits forever!
Do you please suggest how to get rid of the error?
running the code gives accurate output from Ultrasonic sensor: it continuously gives the right distance from the sensor.
next I remove the comments you see. the code continues to behave as if it is still commented! To force it to work, I have to close the file descriptor and reopen it before querying the encoder sensor. This is inefficient, as I'll have various sensors. Also, even if I close and reopen the file descriptor, code works for some time and then hangs!
Questions:
a) it seems that it is certainly not connected with Arduino sketch right?
b) any idea how to approach the solution?