本文介绍了从串口解析完整的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找的信息从以下地址开始:

  0xB5 0x62 0x02 0x13 

从这样的串口读取,就像这样

  while(running!= 0)
{

int n = read(fd,input_buffer,sizeof input_buffer); (int i = 0; i< BUFFER_SIZE; i ++)








if(input_buffer [ I] == 0xB5执行&安培;&安培; input_buffer [I + 1] == 0X62&安培;&安培; input_buffer [I + 2] == 0×02和安培;&安培; input_buffer [I + 3] == 0×13和安培;&安培; I< (BUFFER_SIZE-1))
{

//处理消息。
}

}

我遇到的问题是我需要得到一个完整的信息。一半的消息可能在缓冲区中一次迭代。另一半可能在下一次迭代中进入消息。

有人建议将缓冲区从完整的信息中解放出来。然后我将缓冲区中的其余数据移动到缓冲区的开始处。



如何执行该操作或其他任何方式以确保每次完成所选内容消息进来了吗?

编辑//



I想要一个特定的类和ID。但我也可以读取长度

解决方案

您可以将读取分为三部分。找到消息的开始。然后获得LENGTH。然后阅读其余的消息。

  //应该清除这些数据,以防从先前读取的
input_buffer [0] = input_buffer [1] = 0;

//首先确保第一个字符是0xB5
do {
n = read(fd,input_buffer,1);
} while(0xB5!= input_buffer [0]);

//检查第二个同步字符
n = read(fd,& input_buffer [1],1);

if(input_buffer [1]!= 0x62){
//错误
return;
}

//读取到LENGTH
n = read(fd,& input_buffer [2],4);

//分析长度
// int length = *((int *)& input_buffer [4]);
//因为我不知道你的系统上int大小是多少,所以这种方式更好一些。 (input_buffer [5]<< 8);

//读取消息的剩余部分
n = read(fd,& input_buffer [6],length);

// input_buffer现在应该有一个完整的消息

您应该添加错误检查......


I am trying to read complete messages from my GPS via serial port.

The message I am looking for starts with:

0xB5 0x62 0x02 0x13

So I read from the serial port like so

while (running !=0)
{

int n = read (fd, input_buffer, sizeof input_buffer); 


for (int i=0; i<BUFFER_SIZE; i++)
{   



if (input_buffer[i]==0xB5  && input_buffer[i+1]== 0x62 && input_buffer[i+2]== 0x02 && input_buffer[i+3]== 0x13    && i<(BUFFER_SIZE-1) )
     { 

            // process the message.
     }

}

The problem I am having is that I need to get a complete message. Half of a message could be in the buffer one iteration. And the other half could come into the message the next iteration.

Somebody suggested that free the buffer up from the complete message. And then I move the rest of data in the buffer to the beginning of the buffer.

How do I do that or any other way that make sure I get every complete selected message that comes in?

edit//

I want a particular class and ID. But I can also read in the length

解决方案

You can break the read into three parts. Find the start of a message. Then get the LENGTH. Then read the rest of the message.

// Should probably clear these in case data left over from a previous read
input_buffer[0] = input_buffer[1] = 0;

// First make sure first char is 0xB5
do {
    n = read(fd, input_buffer, 1); 
} while (0xB5 != input_buffer[0]);

// Check for 2nd sync char
n = read(fd, &input_buffer[1], 1);

if (input_buffer[1] != 0x62) {
     // Error
     return;
}

// Read up to LENGTH
n = read(fd, &input_buffer[2], 4); 

// Parse length
//int length = *((int *)&input_buffer[4]);
// Since I don't know what size an int is on your system, this way is better
int length = input_buffer[4] | (input_buffer[5] << 8);

// Read rest of message
n = read(fd, &input_buffer[6], length);

// input_buffer should now have a complete message

You should add error checking...

这篇关于从串口解析完整的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 00:31