I'm tring to read data from arduino with simple C code without success.
The code is the following:
void setup() {
Serial.begin(9600);
}
void loop() {
uint8_t one[4] = {1, 2, 3, 4};
int writed = Serial.write(one, 4);
delay(1000);
}
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
int main() {
int port = open("/dev/ttyACM0", O_RDONLY | O_NOCTTY);
uint8_t buffer[4];
int n_read;
uint32_t tt;
struct termios options;
tcgetattr(port, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag &= ~PARENB; // cleare parity bit;
options.c_cflag &= ~CSTOPB; // set stop bit = 1;
options.c_cflag &= ~CSIZE; //
options.c_cflag |= CS8; // set data bit to 8
options.c_cflag &= ~CRTSCTS; // no hardware flow control
options.c_cflag |= CLOCAL | CREAD; // enable receiver ignore status lines
options.c_cc[VMIN] = 4; // wait for 4 bytes
options.c_cc[VTIME] = 0; // no minimum time to wait before read returs
tcsetattr(port, TCSANOW, &options); // commit the options
// flush serial buffer
tcflush(port, TCIFLUSH);
while (1) {
n_read = read(port, buffer, 4);
printf("%i %i\n", n_read, buffer[0]);
sleep(1);
}
}
What i get is always 0 bytes read, how i can fix this?