我有数据,其中包括纬度和经度的 32 位缩放整数、xy 和 z 速度的 32 位浮点数以及一些 1 位标志。我还成功地通过 UDP 发送了至少一个 double,但它在另一端和 Wireshark 中似乎是垃圾。我需要按位访问以每秒通过 UDP 发送数据来切换/更改数据。消息格式很简单,每种数据类型都会占用它的大小并具有一定的起始位。似乎我需要序列化,但在另一端,不一定反序列化是可能的吗?我的第一个想法是创建一个包含所有数据的结构,然后通过 UDP 发送整个结构,但填充似乎是错误的。消息需要 64 字节长,并且每种数据类型都有一个起始位和格式长度。这段代码可以编译并且可以工作,但是在接收端有一个不可读的纬度。Lat、long 和 alt 每秒更新一次。有更多数据字段,但我试图从小处着手。格式的片段附有数据类型、字位置、该字中的起始位和长度(以位为单位),lat、long 和 alt 在消息中的其他位置。数据格式现在最好的想法是使用 Cereal ( http://uscilab.github.io/cereal/index.html ) 但不确定这是否是死胡同。
struct TSPIstruct //
{
double Latitude;
double Longitude;
double Altitude;
};
void SendDataUDP(double latitude, double longitude, double altitude)
{
TSPIstruct TSPI;
TSPI = CompileTSPI(latitude, longitude, altitude);
//printf("\n\r");
printf("Sending GPS over UDP at %d", LastSystemTime);
printf("\n\r");
printf("\n\r");
//send the message
if (sendto(s, (char*)&(TSPI.Latitude), sizeof(double) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d" , WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("UDP packet sent, latitude is %f", TSPI.Latitude);
printf("\n\r");
printf("\n\r");
}
TSPIstruct CompileTSPI(double latitude, double longitude, double altitude)
{
TSPIstruct TSPI;
TSPI.Latitude = latitude;
TSPI.Longitude = longitude;
TSPI.Altitude = altitude;
return TSPI;
}