Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LINUX上用c程序读取/dev/ttyUSB0_C_Linux_Serial Port_Xbee - Fatal编程技术网

在LINUX上用c程序读取/dev/ttyUSB0

在LINUX上用c程序读取/dev/ttyUSB0,c,linux,serial-port,xbee,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

我想读取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;
    }
}
我哪里做错了


我的计划如下:

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;
    }
}
美元符号$$$$表示四人一组的数字。。。我再说一遍,我想要的是

CHARS : 15931    SENTENCES = 0    CHECKSUM : 58
Heading : 55    Tilt: -46    Roll:2
CHARS : .....
我曾尝试在格式字符串中使用%c、%d、%x,但显然它们都不能正常工作


谢谢大家!

在我的代码中,我只更改了
c\u 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\u-iflag
)。

在我的代码中,我只更改
c\u-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;

明天我会发布更多的代码,但是试一试(不要修改
ciflag
)@A_Bouras,请验证问题是否正确。不要只说“接收错误”。出现错误后,请尝试打印此函数调用返回的字符串:
strerrr(errno)
和include
。这将给你一个真正的错误消息,并说明原因。我使用了XBee模块。但不是GPS之类的东西。我使用的XBee协议需要二进制传输,而不是文本传输。您可能必须为原始模式设置串行端口。也许我在这个答案中的代码可以适合你的应用程序:顺便说一句,你的“答案”应该是对原始问题的编辑。@A_Bouras请不要发布额外的代码作为答案,只需编辑你的问题(见其下的编辑链接)以添加其他信息。我已将这个问题从法语翻译过来(OP可能被Chrome中的Google Translate欺骗)。@A_Bouras,请验证问题是否正确。不要只说“接收错误”。在出现错误后,请尝试打印此函数调用返回的字符串:
strerrr(errno)
和include
。这会给你一个真正的错误消息,并说明原因。我使用了XBee模块,但不是GPS的东西。我使用的XBee协议需要二进制传输,而不是文本传输。你可能必须为原始模式设置串行端口。也许我在这个答案中的代码可以适用于你的应用程序:顺便说一句,你的“答案”应该是对原始问题的编辑。@A\u Bouras请不要将其他代码作为答案,只需编辑您的问题(请参见其下的编辑链接)以添加其他信息。
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

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