简体   繁体   中英

Read arduino from serial in C

I have the following program that tries to read data from an arduino using the serial port, the thing is it mostly doesn't read anything, except sometimes it reads a piece of what I'm sending. The arduino code its just writing a single letter in a loop.

#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>

int main() {
    int serialfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
    if (serialfd == -1)
        perror("Error opening the serial port");
    else
        fcntl(serialfd, F_SETFL, 0);

    fprintf(stdout, "Device is open, attempting read \n");

    fcntl(serialfd, F_SETFL, 0);
    char buf[11] = {0};
    read(serialfd, buf, 10);
    fprintf(stdout, "Buffer: %s", buf);
    close(serialfd);
    return 0;
}

for example the output is like this

Device is open, attempting read 
Buffer: AAAAAAAAAAA⏎

If I try to run it again (several times) I just get the 0'd buffer

Device is open, attempting read 
Buffer: ⏎                

It sounds like a configuration issue, most likely the baudrate is not properly set. Also, as mentioned in the question's comments, you might be getting a full buffer with no '\\0' character at the end, thus fprintf does not behave properly.

Here I'll explain how to set the baudrate but you can use the wikibooks link I've put further down the answer to set other settings, also make sure to check the buffer.

Put simply on the arduino I like to use 115200 as my baudrate. There are a few more that are usually supported on other devices but this value does just fine so I'll use that for my example.

On the arduino this is most likely going to be the only thing you will have to configure (and if fact, it's the only thing I set when I want to use the serial port to talk to my computer).

Serial.begin(115200);

Then according to this wikibook you can set your baudrate via settings in the termios structure, as in the wikibook example I'll call it attribs .

struct termios attribs;

/* get the current settings */
tcgetattr(serialfd, &attribs);

/* set the baudrate */
cfsetospeed(&attribs, B115200); /* outut baudrate */
cfsetispeed(&attribs, B115200); /* input baudrate */

/* if there is need for it, set other settings here */

/* eventually apply everything for your serialfd descriptor */
tcsetattr(serialfd, TCSANOW, &attribs);

So yes technically you could have different speeds for input than for output but the arduino's UART only has one such setting and does not support different speeds for input/ouput , so you need to set it for both on the computer to the same value.

If you just need to receive binary data on PC side and maybe react to some certain values and send something back to Arduino and log data for example,

then you can use some more advanced RS232 terminal program such as http://docklight.de/

It does have much more option and scripting to basically make ant data processing, but I did not use scripting on it yet.

But in few minutes you can have data read and answering in ASCII or binary format.

Of course for some database connectivity or more you will need custom program, but to test operation during debugging stage docklight is great tool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM