我正在用C ++开发代码来处理我的GPS。 GPS通过UBX协议进行通信。我知道这个数据表中的消息格式。我有些怎么不能用C ++编写它。有人能告诉我一个如何用C ++编写消息/包的例子吗?
我希望从此代码获得的是纬度经度和海拔高度,但是存在一个问题,即代码不会进入切换条件,可能是因为数据不等于0xB5。有人可以告诉我我错过了什么吗?
int decode_gps() {
while(1) {
// write(fd,bytestosend,6);
// cout<<"bytes sent = "<<bytestosend<<endl;
read(fd,&UBX_buffer,40);
switch(UBX_step) //we start from zero and increment as we go through the cases
{
case 0:
if(UBX_buffer[1]==0xB5) UBX_step++; break; // UBX sync char 1 //check for the first data packet and go to next byte
case 1: if(UBX_buffer[2]==0x62) UBX_step++;// UBX sync char 2 //check for the second data packet and go to the next byte
else UBX_step=0; break; //if first and second packets are not correct then go back and check again
case 2: UBX_class=UBX_buffer[3]; checksum(UBX_class); UBX_step++; break;
case 3: UBX_id=UBX_buffer[4]; checksum(UBX_id); UBX_step++; break;
case 4: UBX_payload_length_hi=UBX_buffer[5]; checksum(UBX_payload_length_hi); UBX_step++; break;
case 5: UBX_payload_length_lo=UBX_buffer[6]; checksum(UBX_payload_length_lo); UBX_step++; break;
case 6: // Payload data read...
if (UBX_payload_counter < UBX_payload_length_hi) // We stay in this state until we reach the payload_length
{
UBX_buffer[UBX_payload_counter] = data;
checksum(data);
UBX_payload_counter++;
}
else
UBX_step++;
break;
case 7: ck_a=data; UBX_step++; break; // First checksum byte
case 8: ck_b=data; // Second checksum byte
// We end the GPS read...
if((ck_a= ck_a)&&(ck_b= ck_a)) // Verify the received checksum with the generated checksum..
parse_ubx_gps(); // Parse new GPS packet...
UBX_step=0;
UBX_payload_counter=0;
ck_a=0;
ck_b=0;
GPS_timer=0; //Restarting timer...
break;
} // End Switch
}
return(0);
}
void parse_ubx_gps(void)
{
int j;
if(UBX_class == 0x01)
{
switch(UBX_id)
{
case 0x02: // ID NAV - POSLLH
j = 4;
lon = (float)join_4_bytes(&UBX_buffer[j]); // lon * 10000000
j += 4;
lat = (float)join_4_bytes(&UBX_buffer[j]); // lat * 10000000
j += 4;
//Altitude = join_4_bytes(&UBX_buffer[j]); // elipsoid heigth mm
j += 4;
alt = (float)join_4_bytes(&UBX_buffer[j]); // MSL heigth mm
alt /= 10.;
} // close case
} // if
}