I am trying to figure out how to get information from my Arduino Mega 2560 through the USB port to a C program running in Xubuntu.
I'm trying to put together the simplest possible example to be used as a starting point.
The Arduino IDE comes with some nice basic examples, one of which is Graph, which simply prints the value from a potentiometer or other analog sensor:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(2);
}
No problems there, but as for the program that will be running on my computer and receiving the data, here's what I have so far(updated after reading this and doing some more digging):
/* prints the output from an Arduino running the "Graph" example.
*/
#include <stdio.h>
#include <sys/fcntl.h>
#include <termios.h>
int main(void)
{
int fd = 0;
char buffer[32];
int n;
fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY);
while(1)
{
read(fd, buffer, sizeof(buffer));
n = atoi(buffer);
printf("%d\n", n);
}
close(fd);
}
This almost works, but it appears to be missing more messages than it's getting and a lot of the messages are incomplete.
There are several options and flags that can be set along the way and I'm having trouble determining which ones to set.
Ultimately, I plan on using various sensors running through my Arduino to control SDL-based applications running on my computer, but as soon as I can just get that string then I should be back in familiar territory.