From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,29fe9a340e0d180d X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: Depending on passing mechanism Date: 1997/10/21 Message-ID: #1/1 X-Deja-AN: 284947246 References: Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-10-21T00:00:00+00:00 List-Id: In article , hbaker@netcom.com (Henry Baker) wrote: >(Sorry about the free-association, but it is important to understand that >technical issues are now completely irrelevant in the choice of computer >languages or their features.) I'm going to tell you a story, Henry. It's called, What I Did At Work Today. We have a piece of external hardware, to which we communicate via an RS-422 serial port. We send and receive messages of a certain format, as a byte stream. On this piece of hardware is a thermocouple, which measures the temperature of the hardware. We can configure the hardware to shut down when it gets too hot or too cold, and so we periodically ask for the temperature to see if we're getting close to the extremes. The temperature is measured in degrees Celsius, having a range from -55 C to 125 C. The temperature is a real type, 9 bits long, and with an LSB of 0.5 C. We recieve the temperature as 2 bytes worth of a larger message. The first byte contains the low order 8 bits of the temperature, and the LSB of the second byte contains the MSB of the temperature. That's 9 total bits of temperature, embedded within 2 bytes. Now, here's how I was able to convert this data using Ada. You can decide whether the Ada solution is "too complex." Temperature_In_Celsius_Delta : constant := 0.5; type Temperature_In_Celsius_Base is delta Temperature_In_Celsius_Delta range -128.0 .. 127.5; for Temperature_in_Celsius_Base'Small use Temperature_in_Celsius_Delta; subtype Temperature_in_Celsius is Temperature_In_Celsius_Base range -55.0 .. 125.0; type Temperature_As_Record is record Temperature : Temperature_in_Celsius_Base; end record; for Temperature_As_Record use record Temperature at 0 range 7 .. 15; end record; for Temperature_As_Record'Size use 16; subtype Temperature_As_Byte_Array is Byte_Array (1 .. 2); function To_Temperature_As_Record is new Unchecked_Conversion (Temperature_As_Byte_Array, Temperature_As_Record); function Get_Temperature (Message : Message_Type) return Temperature_In_Celsius is Temperature_Message : Byte_Array renames Message.Temperature; Temperature_Bytes : constant Temperature_As_Byte_Array := (1 => Temperature_Message (2), 2 => Temperature_Messsage (1)); Temperature_Record : constant Temperature_As_Record := To_Temperature_As_Record (Temperature_Bytes); Temperature : Temperature_In_Celsius_Base renames Temperature_Record.Temperature; begin if Temperature not in Temperature_in_Celsius then raise Temperature_Out_Of_Range; end if; return Temperature; end Get_Temperature; Now, that was a real problem at my real job. Do you feel that that was too difficult? Dijkstra admonished us 20 years ago that we need to "minimize the intellectual distance" between a problem and the solution to that problem. Doesn't this solution capture the qualities of the problem directly? o I can directly declare a real number type that exactly matches the accuracy and size of the hardware, and comprising all possible values the can come from the hardware. type Temperature_In_Celsius_Base is delta 0.5 range -128.0 .. 127.5; for Temperature_In_Celsius_Base'Size use 9; This seems pretty clear to me. Is there some easier way? o I can directly declare another type that denotes the exact range of values - fewer in number than what is theoretically possible - that the thermocouple advertises it can return. I can check that the hardware documentation isn't lying to me, or that perhaps the hardware is just flaky today, by using a simple membership test: subtype Temperature_in_Celsius is Temperature_In_Celsius_Base range -55.0 .. 125.0; if Temperature not in Temperature_In_Celsius then raise Out_of_Range; end if; I don't understand how you could be more clear than that. o I was able to assemble the bits, spread out over 2 bytes in reverse order, into a 9 bit temperature type, without much trouble, using our old friend Unchecked_Conversion. I'm an open-minded guy, and if you've got some better way to translate the data from this thermocouple, using another language besides Ada, then I'd love to hear about it. Despite the "horrible experiment" that was Ada, a "baroque language" the product of a "flawed design process," I'm still able to solve nasty problems interfacing to hardware with relative ease, using Ada solutions that have, dare I say (gasp!), a certain elegance. I think the technical issues definately do affect my choice of language. For the work I do (embedded, real-time systems), Ada's advantages seem to outweigh its putative flaws. -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271