使用LINUX上的c程序读取/ dev / ttyUSB0

时间:2012-11-20 14:05:37

标签: c linux serial-port xbee

我想读取GPS XBee协议发送的数据帧。 USB XStick接收以下数据:

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

依旧......我可以通过输入终端控件来阅读:

$ screen /dev/ttyUSB0

我希望以相同的方式看到这些细节,但是用C编写的程序。这就是我的工作:

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

但它不起作用,我得到“接收错误”,对应于:

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

我哪里错了?


我的计划如下:

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

这次没有错误,但显示的字符是错误的:

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

美元符号$$$$代表四个一组的数字......我再说一遍,我想要的是

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

我尝试在格式字符串中使用%c,%d,%x,但显然它们都没有正常工作......

谢谢!

1 个答案:

答案 0 :(得分:1)

在我的代码中,我只更改c_cflag,如下所示:

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

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

我明天会发布更多代码,但请注意一下(不要修改c_iflag)。