我正在尝试从 linux 上的端口读取数据,然后我想使用这些数据并打印出来它总是第一个字节打印整个数据,所有其他字节都是空的。有谁知道我如何将读取的每个字节存储在一个单元格中。代码写下来了,请告诉我我错过了什么?
int decode_gps() {
while(1){
unsinged int UBX_buffer[40];
if (read(fd,&UBX_buffer,1)>0) {
// cout<<UBX_buffer[0]<<endl;
switch(UBX_step) //we start from zero and increment as we go through the cases
{
case 0:
if(UBX_buffer[0]==0xB5) UBX_step++; break; // UBX sync char 1 //check for the first data packet and go to next byte
case 1: if(UBX_buffer[1]==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[2]; checksum(UBX_class); UBX_step++; break;
case 3: UBX_id=UBX_buffer[3]; checksum(UBX_id); UBX_step++; break;
case 4: UBX_payload_length_hi=UBX_buffer[4]; checksum(UBX_payload_length_hi); UBX_step++; break;
case 5: UBX_payload_length_lo=UBX_buffer[5]; 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
} //end if
} //end while loop
close(fd);
return(0);
}