Hey everyone, I'm not a complete noob, but I am when it comes to serial programming. So, here is my simple C function that will get a value from serial:
int arduinoValue(int fd, int length){
char valBuf[length];
char b[1];
int k = 0;
for(k = 0; k <= length; k++){
int n = read(fd, b,1);
if(n == -1) exit(1);
valBuf[k] = b[0];
}
valBuf[k] = '\0';
tcflush(fd,TCIOFLUSH);
int retval = atoi(valBuf);
return retval;
}
And this DOES work. Mostly. For instance, If i run this 5 times, 3 of those times Ill get the right value, other times it seems the bits are getting mashed. For instance, my potentiometer is set to "586". Here are 5 runs of my function:
$ 586
$ 586
$ 58686
$ 5
$ 586
Is there away to get a more consistent reading? I've tried, as you see above, to force it to read a fixed number of bits. Any help would be awesome. BTW, I've looked around for about an hour and couldn't find anything. And yes, I did see the link about interfacing with C via the arduino site, his code is sketchy and never worked. So, this does work, its just a bit sketchy itself. Thanks for reading and hopefully this leads to a solid arduino/serial interface for C.
--ac