Linux – Read /dev/ttyUSB0 with a c program on LINUX

clinuxserial-portxbee

I want to read data frames sent by a GPS XBee protocol. The USB XStick receives the following data:

CHARS : 15931    SENTENCES = 0    CHECKSUM : 58
Heading : 55    Tilt: -46    Roll:2
CHARS : ....

and so on … I can read by typing in the terminal control :

$ screen /dev/ttyUSB0

I'd like to see these details in the same way, but with a program written in C. Here's what I do :

#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include "serial_port.h"

void read_Serial_Port(const char* DEVICE_PORT)
{
    int file;
    struct termios options;
    char message[100];
    unsigned int nCountMax = 60;
    bool b;

    file = open(DEVICE_PORT, O_RDONLY | O_NOCTTY | O_NDELAY);

    if(file == -1){perror("Unable to open the serial port\n");}
    printf("Serial port open successful\n");

    tcgetattr(file, &options);          
    cfsetispeed(&options, B9600);                   
    cfsetospeed(&options, B9600);                   
    options.c_cflag |= (CLOCAL | CREAD);              
    options.c_cflag |= PARENB;                      //No parity                 
    options.c_cflag |= PARODD;                      
    options.c_cflag &= ~CSTOPB;                     
    options.c_cflag &= ~CSIZE;                      
    options.c_cflag |= CS8;                         //8 bits                    
    options.c_iflag |= (INPCK | ISTRIP);            
    tcsetattr(file, TCSANOW, &options);          
    fcntl(file, F_SETFL, FNDELAY);          

    printf("Reading serial port ...\n\n"); 
    b = readMessage(file, message, nCountMax);
    if (b == 0){printf("Error while reading serial port\n");}
    else printf("Serial port read successful\n");
    close(file);
    printf("Serial port closed\n");
};

bool readMessage(int file, char *message, unsigned int nCountMax)
{
    int nbCharToRead;
    char data[100];
    int i;

    if (file != 0)
    {
        i = 0;  
        while (i<nCountMax && data != ".")
        {
            if (read(file,&data,1) == -1)
            {
                printf("reception error\n");
                return false;
            }
            else
            {   
                message[i] = *data;
                printf("%c", message[i]);
                i++;
            }
        }
        message[i] = 0;
        return true;
    }
}

But it does not work, I get "reception error", corresponding to:

read(file,&data,1) == -1

Where am I going wrong?


My program is the following:

bool readMessage(int file, unsigned int nCountMax)
{
    int i;
    size_t nbytes;
    ssize_t bytes_read;

    if (file != -1)
    {
        i = 0;  
        char message[100];
        char data[100];
        while (i<nCountMax && data != ".")
        {
            if (read(file, data, 1) == -1)
            {
                printf("reception error\n");
                printf("code errno = %d\n", errno);
                return false;
            }
            else
            {   
                nbytes = sizeof(data);
                bytes_read = read(file, data, nbytes);
                message[i] = *data;
                printf("%c", message[i]);
                i++;
            }
        }
        message[i] = 0;
        return true;
    }
}

This time there are no more errors, but the characters shown are wrong:

$$$$QUC
U$C
$$$$JQMJ'   J$Cz(HSQ'Q'y
UKUNiQUMJ

the dollar signs $$$$ represent numbers in groups of four… I repeat that what I would like to have is

CHARS : 15931    SENTENCES = 0    CHECKSUM : 58
Heading : 55    Tilt: -46    Roll:2
CHARS : .....

I have tried using %c, %d, %x in the format string, but obviously none of them worked properly …

Thank you!

Best Answer

In my code, I only change c_cflag, as follows:

// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

// Set 8-bit mode
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

I'll post more code tomorrow, but give that a shot (and don't modify c_iflag).