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

ドル記号 $$$$ は 4 つのグループの数字を表します。

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 に答える