lundi 29 avril 2019

How to handle many different input binary message types

I have a device that I am communicating with by sending and receiving a particular binary packet structure. The device has a somewhat well defined API, but there are over 100 possible message types that it can return. What is a good design to use to handle the processing of these different message types?

Here is an example in pseudocode, I am ignoring framing, and checksum bytes to make it clearer.

// I receive this message, where 0x00 indicates the device status,
//and each other byte is a particular error or status
message = [0x00, 0x01, 0x01, 0x04]
// The next message I receive, where 0x10 indicates system time, 
// and the rest of the fields are the integer clock seconds of the device.
message = [0x10, 0x00, 0x32, 0xFF, 0x8E]
// 100 other message types....

As you can see, each message I receive needs to be processed slightly different, has different meanings. I originally was going to use a gigantic switch statement case 0x00: process_errors() case 0x10: process_time() but I was curious if there a better design that I could use to increase flexibility of adding new message types, better useability, etc.

Aucun commentaire:

Enregistrer un commentaire