Go Down

Topic: Read 4 bytes from arduino with C code (Read 393 times) previous topic - next topic

thymbahutymba

Dec 07, 2018, 04:56 pm Last Edit: Dec 07, 2018, 04:57 pm by thymbahutymba
I'm tring to read data from arduino with simple C code without success.
The code is the following:

Code: [Select]

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

void loop() {
    uint8_t one[4] = {1, 2, 3, 4};
    int writed = Serial.write(one, 4);
    delay(1000);
}


Code: [Select]

#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?

matcanyn

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);
}

thymbahutymba

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

Coding Badly

Code: [Select]

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


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


Coding Badly

Code: [Select]

...
    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.


thymbahutymba

#5
Dec 07, 2018, 10:41 pm Last Edit: Dec 07, 2018, 10:43 pm by thymbahutymba
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

Code: [Select]

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


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

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

Coding Badly

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.


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!)



thymbahutymba

Yup.

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


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

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.

westfw


Go Up