Read 4 bytes from arduino with C code

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?

the function "Serial.write" will just write the data as binary. If you wanna see it on serial monitor you should use "print" instead.

void setup() {
Serial.begin(9600);
}

void loop() {
uint8_t one[4] = {1, 2, 3, 4};
for(uint8_t i=0; i<sizeof(one)/sizeof(uint8_t); i++){
Serial.print(one*);*

  • }*
  • delay(1000);*
    }

As you see in previous post i want read bits from C code that didn't works as I expected

    options.c_cflag &= ~CSTOPB;        // set stop bit = 1;

...is not the correct way to set a bit.

thymbahutymba:

...

options.c_cc[VMIN] = 4;            // wait for 4 bytes
   options.c_cc[VTIME] = 0; // no minimum time to wait before read returs
...




What i get is always 0 bytes read, how i can fix this?

Your claim does not match the comments. There is no exit path for anything less than four bytes.

if you see the code into arduino i want to send exactly 4 bytes and will be always thus

I followed some examples online, if you know what's wrong, tell me and I'll fix it and hope it works

thymbahutymba:
if you see the code into arduino i want to send exactly 4 bytes and will be always thus

Yup.

Reread my post. Try to put more thought and care into the effort.

I don't see anything in your C code to set "raw" mode, so that side may be sitting there waiting for a line terminator...
As an experiment, try "stty raw; ./myprog; stty cooked"
(that's what I usually do when I need a quick C program to read data from a microcontroller. Looking up all the (varied) tcsetattr/ioctl stuff is too much work...
(also: "stty raw; ./myprog </dev/tty.ACM0; stty cooked" saves even more work!)

I don't understand what you mean. Could you suggest me how i can fix it?

westfw:
I don't see anything in your C code to set "raw" mode, so that side may be sitting there waiting for a line terminator...
As an experiment, try "stty raw; ./myprog; stty cooked"
(that's what I usually do when I need a quick C program to read data from a microcontroller. Looking up all the (varied) tcsetattr/ioctl stuff is too much work...
(also: "stty raw; ./myprog </dev/tty.ACM0; stty cooked" saves even more work!)

I need that everythings works with only C code, so i have to read four bytes from arduino that in this case contains 1, 2, 3, 4.

Ok. So, see cfmakeraw()