![]() |
|
|||||||
| Beta Driver Development Discussion of new drivers, finding someone to write a new driver, etc... |
![]() |
|
|
Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
I am writing a PDL driver for my energy analyzer. The data from the internal registers is returned in binary rather than ASCII format with two sequential bytes representing the MSB and LSB or a 16bit signed Integer. What is the easiest way to convert these to an INT4 field in the "Store=" structure?
So far the only thing that works is (for bytes 3 and 4 in the reply) Store= Register1 = Add(Mul(ToInt4(Extract(&Reply, Card1, 3)), 256) ,Extract(&Reply, Card1, 4)); EndStore; This seems long winded and I am not sure yet if it will work for negative numbers. Is there a better way? PJG |
|
#2
|
||||
|
||||
|
Probably the best way is to extract the LSB cast to card2, then shift left 8. Then extract the MSB, to card2, and OR it with the result. So you know have the 16 bit value in the correct format. You can then just cast that to an Int2 to get the correct handling of the two's complement format. Then you can store that. So, just by eye and I'm sure with various syntax errors, something like:
Code:
So, the innermost pulls the two values out, casts them to card2, shifts the first one left to make room for the next one and ORs them together to create the the raw 16 bit value. It then casts that to Int2 to covert to signed (it's important to do the Int2 so that the two's complement stuff is handled correctly.) We really need a byte swap expression, which would make things like this easier.
__________________
Dean Roddey Software Geek Extraordinaire |
![]() |
| Thread Tools | |
| Display Modes | |
|
|