1

我想读取 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,但显然它们都不能正常工作......

谢谢!

4

1 回答 1

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

于 2012-11-21T06:28:45.520 回答