这是我常用的头文件。
/*Type of data being sent to HMI*/
enum HMIDataType{
Climate = 0x12
};
#define MIN_DATA_CLIMATE 0x00000200u
#define MAX_DATA_CLIMATE 0x000002ffu
/*Types of Climate Data*/
enum enumClimateData
{
//! @brief Driver temperature
//! Type : float
DP_DriverTemperature_Float = MIN_DATA_CLIMATE
};
/*Climate Data to be sent to HMI*/
struct ClimateData{
enumClimateData type;
struct{
double ddata;
int idata;
bool bdata;
}body;
};
/*HMI data to be sent*/
struct HMIData{
HMIDataType type;
struct{
ClimateData climatedata;
}body;
};
发送方应用程序是一个通过 TCP 套接字发送数据的非 QT TCP 客户端
/* Creating Data */
HMIData data;
data.type = Climate;
data.body.climatedata.type = DP_DriverTemperature_Float;
data.body.climatedata.body.ddata = 12.23;
SendDataToHMI(&data);
发送它:
void SendDataToHMI(HMIData * data)
{
/* send the message line to the server */
size_t n = write(sockfd, (char*)data, sizeof(HMIData));
if (n < 0)
printf("ERROR writing to socket");
}
在我的基于 QT 的 TCP 服务器中但是我无法取回数据结构。
不起作用:
HMIData* m= new HMIData();
// get the information
qint64 Data = socket->read((char*)m, sizeof(HMIData));
qDebug() << "Type: " << (uint)m->body.climatedata.type << "Data: " << m->body.climatedata.body.ddata;
不起作用:
QByteArray Data = socket->readAll();
QDataStream dstream (Data);
HMIData m;
while(!dstream.atEnd())
{
dstream >> m;
}