I am working on a project that involves reading data from an Arduino Uno over serial port. In Arduino IDE, I observe that I am successfully printing values over the serial port in the following format:
Serial.print(x, DEC);
Serial.print(' ');
Serial.print(y, DEC);
Serial.print(' ');
Serial.println(z, DEC);
e.g.:
2 4 -41
4 8 -32
10 5 -50
...etc.
Then, I have a program written in C using XCode to read these values as float data types. However, upon running the program in terminal, the program appears to be stuck (no values are read, and I must use ctrl+C to exit).
Any ideas what I might be doing wrong? Below is my code. As of now, I am just using a for loop to test whether I am actually reading in these values. Let me know if you need more information. Thanks for your help.
#include <stdio.h>
#include <ApplicationServices/ApplicationServices.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
// buffer for data
float buffer[100];
int main(int argc, const char* argv[])
{
// open serial port
int port;
port = open("/dev/tty.usbmodem1411", O_RDONLY);
if (port == -1)
{
printf("Unable to open serial port.");
}
// testing read of data
fcntl(port, F_SETFL, FNDELAY);
for (int i = 0; i < 10; i++)
{
read(port, buffer, 12);
printf("%.2f %.2f %.2f\n", buffer[3*i], buffer[3*i+1], buffer[3*i+2]);
}
// close serial port
close(port);
return 0;
}