Search code examples
serial-portarduinoxbee

Reading from serial port in C


I'm trying to read from Arduino (who's sending char '4' constantly) with XBee.

I have tried writing from the PC to Arduino, and it works, so the connection is correct.

When I execute de following code, the terminal doesn't show anything and don't finish the program, so it gets stuck on reading.

#include <stdio.h>     // Standard input / output functions
#include <stdlib.h>
#include <string.h>     // String function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions
#define XBEE "/dev/ttyUSB0"
#define BAUDRATE B9600

int main(int argc,char** argv)
{
    struct termios tio;
    struct termios stdio;
    struct termios old_stdio;

    int tty_fd = open(XBEE , O_RDWR| O_NOCTTY);

    cfsetospeed(&tio,BAUDRATE);
    cfsetispeed(&tio,BAUDRATE);            // Baudrate is declared above
    tcsetattr(tty_fd,TCSANOW,&tio);
    // for(i;i<5;i++){
    // write(tty_fd,&c,1); //If new data is available on the console, send it to the serial port
    // write(tty_fd,&o,1); //If new data is available on the console, send it to the serial port

    // }
    int n=0;
    char buf = '\0';

    /* Whole response*/

    do
    {
       n = read( tty_fd, &buf, sizeof(char) );
    }
    while( n > 0);

    if (n < 0)
    {
        printf("ERROR READING");
    }
       else if (n == 0)
    {
        printf("Read nothing!");
    }
    else
    {
        printf("Response: %c",buf);
    }
    close(tty_fd);
    tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
    return EXIT_SUCCESS;
}

How do I solve this?

UPDATE

I tried this other code and receive a warning: turning off output flushing and then the terminal froze.

#include <stdio.h>      // Standard input / output functions
#include <stdlib.h>
#include <string.h>     // String function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions
#define XBEE "/dev/ttyUSB0"
#define BAUDRATE B9600

int main(int argc,char** argv)
{
    struct termios tio;
    struct termios stdio;
    struct termios old_stdio;
    struct termios options;

    int tty_fd = open(XBEE , O_RDWR | O_NOCTTY | O_NDELAY);

    cfsetospeed(&tio,BAUDRATE);
    cfsetispeed(&tio,BAUDRATE);            // Baudrate is declared above
    tcsetattr(tty_fd,TCSANOW,&tio);

    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    // for(i;i<5;i++){
    // write(tty_fd,&c,1);//if new data is available on the console, send it to serial port
    // write(tty_fd,&o,1);//if new data is available on the console, send it to serial port

    // }
    int n=0;
    char buf = '1';
    int i = 1;
    /* Whole response*/

    while(i==1){
        n = read( tty_fd, &buf, sizeof(char) );

        if (n < 0)
        {
            printf("ERROR READING");
        }
           else if (n == 0)
        {
            printf("Read nothing!");
        }
        else
        {
            printf("Response: %c",buf);
            close(tty_fd);
            break;
        }
    }
    tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);
    return EXIT_SUCCESS;
}

Solution

  • If you're going to be using C to communicate with XBee modules, you might want to check out this Open Source XBee Host library. You could just use the serial driver from it in your code, or take a look at the xbee_term sample program as a simple terminal.