Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.arch.embedded > #28284
| From | pozz <pozzugno@gmail.com> |
|---|---|
| Newsgroups | comp.arch.embedded |
| Subject | Re: How to transmit the value of an enumeration |
| Date | 2018-10-24 23:20 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <pqqnma$pme$1@dont-email.me> (permalink) |
| References | <pqpkdn$ed3$1@dont-email.me> <846dnZ-IQIryLU3GnZ2dnUU7-bmdnZ2d@earthlink.com> |
Il 24/10/2018 19:48, Joe Chisolm ha scritto:
> On Wed, 24 Oct 2018 13:18:15 +0200, pozz wrote:
>
>> enum type_t {
>> TYPE_1,
>> TYPE_2,
>> ...
>> TYPE_100,
>> }
>>
>> void tx_byte(unsigned char c);
>>
>> enum type_t t;
>> ...
>> tx_byte((unsigned char)t);
>>
>>
>> If the maximum value in the enumeration is lower than 256, you can cast
>> to (unsigned char) and transmit it as a byte.
>>
>>
>> However the receiver must have the same enumeration. The transmitter
>> and the receiver must agreed on *the exact values* of the enumeration.
>>
>> What happens if the developer of the transmitter thinks that it is
>> better to swap some values during a future release? The enumeration
>> isn't used only for transmission, it is also used for general logic of
>> the software.
>>
>> Until now I didn't found a good solution. I only write some comments
>> before the enumeration declaration:
>>
>> /* WARNING!! DON'T CHANGE THE ORDER OF THE VALUES INSIDE ENUMERATION
>> * They are also used in the transmission */
>>
>> I sometimes prefer explicit the value transmitted:
>>
>> if (t == TYPE_1) tx_byte(0);
>> if (t == TYPE_2) tx_byte(1);
>> ...
>>
>>
>> What is your strategy?
>
> Your protocol document defines what is sent on the communications
channel.
> In your code you may have:
> enum type_t {
> TYPE_2,
> TYPE_1,
> ...
> TYPE_100
> }
>
> But in your protocol handler you have
> enum protocol_type_t {
> PTYPE_1,
> PTYPE_2,
> ...
> PTYPE_100
> }
> switch(t) {
> case TYPE_1:
> tx_byte(PTYPE_1);
>
> .....
>
> You convert your internal type_t to the external protocol protocol_type_t
> and protocol_type_t always (well should always) matches the spec.
Yes, it seems *THE* best solution in order to avoid problems. Even if
it is tedious, because you have two enumerations instead of one and you
have a long piece of translation code that is silly.
Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 13:18 +0200
Re: How to transmit the value of an enumeration Jack <jack4747@gmail.com> - 2018-10-24 04:45 -0700
Re: How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 15:18 +0200
Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-24 17:05 +0200
Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-24 15:10 +0200
Re: How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 15:27 +0200
Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-24 16:59 +0200
Re: How to transmit the value of an enumeration Clifford Heath <no.spam@please.net> - 2018-10-25 08:57 +1100
Re: How to transmit the value of an enumeration Joe Chisolm <jchisolm6@earthlink.net> - 2018-10-24 12:48 -0500
Re: How to transmit the value of an enumeration pozz <pozzugno@gmail.com> - 2018-10-24 23:20 +0200
Re: How to transmit the value of an enumeration Joe Chisolm <jchisolm6@earthlink.net> - 2018-10-24 22:21 -0500
Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-25 09:31 +0200
Re: How to transmit the value of an enumeration Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2018-10-25 09:58 -0700
Re: How to transmit the value of an enumeration Joe Chisolm <jchisolm6@earthlink.net> - 2018-10-25 18:38 -0500
Re: How to transmit the value of an enumeration David Brown <david.brown@hesbynett.no> - 2018-10-26 09:24 +0200
Re: How to transmit the value of an enumeration George Neuner <gneuner2@comcast.net> - 2018-10-24 15:19 -0400
Re: How to transmit the value of an enumeration Les Cargill <lcargill99@comcast.com> - 2018-10-26 20:30 -0500
csiph-web