Home > Back-end >  A design scheme
A design scheme

Time:10-31

Now computer needs and a device for communication, and then the computer needs and communication equipment, communication way is to send different messages via TCP, the structure of each message is different, such as CPU request equipment problems can send message
 
//temperature feedback
Struct temperatureCtrl {
Int msgID;
Unsgined char type;//0: query 1: set the
Unsgined char [3] space;
};

While device after receiving the message will reply
 
Struct templaertureDev {
Int msgID;
Unsgined char type;//0: query 1: set the
Unsigned short value;
Unsgined char space;
}


Agree, if you need to set up the equipment of the time, the request and response messages respectively
 
Struct timeCtrl {
Int msgID;
Unsigned char type;//0: query 1: set the
Unsgined char [3] space;
Unsigned short year;
Unsigned short month;
Unsigned short day.
Unsigned short hour;
The unsigned short min;
Unsigned short SEC;
}
Struct timeDev {
Int msgID;
Unsigned char type;//0: query 1: set the
Unsgined char [3] space;
Unsgined char [8] deviceNumber;
Unsigned short year;
Unsigned short month;
Unsigned short day.
Unsigned short hour;
The unsigned short min;
Unsigned short SEC;
}


Now the problem is the computer software and the device connected to, but this piece of equipment interface has hundreds, each interface is similar to the above such excuses, each structure fields are the basic c + + data types, but each structure fields are different

Now what the problem is that I in order to realize these interfaces to define several hundred structure... I want to ask is there a simple way to deal with the hundreds of different structure

CodePudding user response:

If not considering traffic packet size, you can use json key - value such as the form of structure, so that you don't have to define the structure, if want to consider flow, can be classified to you these structures, and inheritance, such as this
 
Struct MsgHead
{
Int msgID;
Unsigned char type;//0: query 1: set the
};
Struct temperatureCtrl: public MsgHead
{
Unsigned char space [3].
};

Struct timeCtrl: public MsgHead
{
Unsigned char space [3].
Unsigned short year;
Unsigned short month;
Unsigned short day.
Unsigned short hour;
The unsigned short min;
Unsigned short SEC;
}

CodePudding user response:

But can't seem to reduce the number of struct...
  • Related