BryanG
Member
When you use the way I showed the Manipulate drop down is available, it lets you reverse Bits, Bytes or Words
This.When you use the way I showed the Manipulate drop down is available, it lets you reverse Bits, Bytes or Words
tmp = PortRead(port, 0) << 16;
tmp |= PortRead(port, 0) << 24;
tmp |= PortRead(port, 0) << 0;
tmp |= PortRead(port, 0) << 8;
// StartFloat = 6.189;
// used IEEE-754 Floating Point Converter
// to convert that to HEX Integers
Tag1 = 0x4A; // 01001010
Tag1_1 = 0x0C; // 00001100
Tag1_2 = 0xC6; // 11000110
Tag1_3 = 0x40; // 01000000
// Moved the HEX Integers in to EndInteger
EndInteger = (Tag1);
EndInteger |= (Tag1_1) << 8;
EndInteger |= (Tag1_2) << 16;
EndInteger |= (Tag1_3) << 24;
// And used the method a couple of posts above to use that as the source for EndFloat
// EndFloat = 6.189
Yep I proved the method works as well, great stuff!I decided to experiment, maybe I have too much time on my hands
Tag1 to Tag1_3 are Integers in Crimson, as is End Integer.
Then I used the method below:
[...] @BryanG's translation method with two sets of data works - Good! Using the translation method, I do not need to set any Manipulation at all, I just set the raw tag as Integer and the "slave" tag as Floating point and it converts correct.
None of the above solutions is very elegant, could there be a better way out there?
You are reading it a byte at a time, correct?My current tag reading the Int16 displays 65, but should be presented as 0000000001000001 - Any ideas?
bits[0] = '00000000';
bits[1] = '00000001';
bits[2] = '00000010';
bits[3] = '00000011';
bits[4] = '00000100';
...
bits[255] := '11111111';
status = bits[PortRead(port, 0)];
status = bits[PortRead(port, 0)] + status;
You can set a Crimson integer numeric tag format to display the value as binary and even group the digits if you like....
Next question is how to get the binary bits decoded correctly... it is two bytes that should decode into 16 binary bits.
My current tag reading the Int16 displays 65, but should be presented as 0000000001000001 - Any ideas?
Thanks for all help, priceless
// DECODING INT32
tmp = PortRead(port, 0) << 0;
tmp |= PortRead(port, 0) << 8;
tmp |= PortRead(port, 0) << 16;
tmp |= PortRead(port, 0) << 24;
ROV_NAV.Lon = tmp*headingScale;
// DECODING INT16
tmp = PortRead(port, 0) << 0;
tmp |= PortRead(port, 0) << 8;
if (tmp > 32767) {tmp = tmp - 65536;}
ROV_NAV.Pitch = tmp*pitchScale;
void readInt16()
{
tmp = PortRead(port, 0) << 0;
tmp |= PortRead(port, 0) << 8;
if (tmp > 32767) {tmp = tmp - 65536;}
return tmp;
}
ROV_NAV.Pitch = readInt16()*pitchScale;
When you create a program there is a Prototype section at the top, edit that and you can send up to 6 Parameters and get back one result. A new program can be created which is essentially a Function which can be called by other Programs.In additon, is it possible to make function in the Crimson scripting? I'd like to do some functions to read Int32, Int16 etc, to clean up the code a bit... I cant get the below or anything similar to work - Do you guys know?
Functions declared as a void types cannot return a value. We need to declare the function as an int data type; see above.In additon, is it possible to make function in the Crimson scripting? I'd like to do some functions to read Int32, Int16 etc, to clean up the code a bit... I cant get the below or anything similar to work - Do you guys know?
C#:int readInt16(portarg) { int lcltmp; lcltmp = PortRead(portarg, 0) << 0; lcltmp |= PortRead(portarg, 0) << 8; if (lcltmp > 32767) {lcltmp -= 65536;} return lcltmp; } ROV_NAV.Pitch = readInt16()*pitchScale;
Hi @OkiePC, thanks for this - I've implemented this and it seems like a great way to handle the Bits!
Unfortunately, another problem just appeared in the testing phase. It seems like RedLion decodes my 4 byte integers as signed but my 2 byte integers as unsigned?
I do have test data which looks like this:
INT32: 43 DE 67 FF >> -9970109
INT16: EB FF >> -21
The INT16 above does instead decode as 65515, which is true if treated as Unsigned Integer - But not what I want. How come the 4 byte integer is treated as signed but not the 2 byte?
To come arount this, I need to apply a check if the value is higher than the max of a Signed 2 byte integer, if so, substract the max of a Unsigned 2 byte integer to get the correct negative value, see below.
I'm sure you have a more elegant bit magic way of handling this?
C#:// DECODING INT32 tmp = PortRead(port, 0) << 0; tmp |= PortRead(port, 0) << 8; tmp |= PortRead(port, 0) << 16; tmp |= PortRead(port, 0) << 24; ROV_NAV.Lon = tmp*headingScale; // DECODING INT16 tmp = PortRead(port, 0) << 0; tmp |= PortRead(port, 0) << 8; if (tmp > 32767) {tmp = tmp - 65536;} ROV_NAV.Pitch = tmp*pitchScale;
tmp = PortRead(port,0) << 16;
tmp |= PortRead(port,0) << 24; /* Shift bit 7 of this second PortRead(...) to the INT32 sign bit */
tmp /= 65536; /* Use division by 0x10000 as a proxy for right-shift back 16 bits with sign extension */
That worked great, thanks, all tidy nowWhen you create a program there is a Prototype section at the top, edit that and you can send up to 6 Parameters and get back one result. A new program can be created which is essentially a Function which can be called by other Programs.
No, this is the right way to handle this using unsigned INT32s; the issue is that the left-shift (<<) operator shifts all of the bits, and since the PortRead(...) results are always less than 256, their high 24 bits are always 0, so the sign bit (bit 7) of the << 8-shifted PortRead(...) result ends up in bit 15 of the INT32, which is 16 bit positions short of the INT32 sign bit (bit 31).
If you could declare a signed INT16 variable named tmp16, and shift two PortRead(...) results into that, then the << 8 operation would shift bit 7 of the PortRead(...) result into the sign bit (bit 15) of the INT16.
Another solution would be this:
Code:tmp = PortRead(port,0) << 16; tmp |= PortRead(port,0) << 24; /* Shift bit 7 of this second PortRead(...) to the INT32 sign bit */ tmp /= 65536; /* Use division by 0x10000 as a proxy for right-shift back 16 bits with sign extension */
but I don't know if you would consider that more elegant or more obfuscated.