Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

byte to decimal conversion

Hello, 

 

I'm having a problem to read data in a binary format from a an instrument. according to the manual  The instrument send data as a word - a two byte format - in the following order:

 

The MSB is sent first. The LSB is sent last. EOI is sent with the LSB. 

 

I read it as a string and can convert it to bytes array.

 

how do I turn it into a real number?

 

thanks

0 Kudos
Message 1 of 18
(5,386 Views)
String to Byte Array and then do a swap bytes. Typecast to a sgl.

There isn't an existing driver for the instrument?
Message 2 of 18
(5,383 Views)

thanks for the quick reply, 

 

there is no formal driver to my instrument. I wrote the driver myselp and read the data as decimal numbers. this is working slowly and I'm trying to improve that. 

 

I did as you suggested. 

 

what is sgl? where can I find such type to connect? 

 

I typecast it to double numeric and received unreseanable number like 1e245...

0 Kudos
Message 3 of 18
(5,376 Views)
It has to be typecast to i16 or u16
Sgl is an 8 byte floating point number
greetings from the Netherlands
0 Kudos
Message 4 of 18
(5,371 Views)

thanks for the help. 

 

I think its working but I don't understand how? or if it working correctly.

 

when I change the represnetation from sgl to I32 or other representation, I get different long numbers like -32564678 etc...

 

when I read the data in decimal format  I get numbers like -42.56.

 

The decimal readout is obiously correct - but why I do not get the same when I read a word? how do I know the readout is correct?

 

and another quesion:

 

I have indicators for the output bytes before and after the swap - the are the same.... how could that be?

 

 

0 Kudos
Message 5 of 18
(5,364 Views)

mrish wrote:  how do I know the readout is correct?

That's something you have to figure out.  If you don't know what the reading is supposed to be, there is no way to verify that you are getting the right value and doing the conversion correctly.

 

What is the instrument?  The manual should tell you exactly how to convert the data.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 18
(5,355 Views)

the instrument is an old spectrum analyzer HP71450.

 

what I wrote in the first post is taken from the manual regarding binary readout.

 

I've notice I get different numbers for different typecast (i.e. I16, I8, U32 etc.), and I only Identify what I think its suitable based on decimal readout.

 

from the typecast help function I understand its affect the interpretation.

 

I know the number I sould received are like -45.56  yet typecasting to double format produce unrealistic number.

0 Kudos
Message 7 of 18
(5,341 Views)

mrish wrote:

I know the number I sould received are like -45.56


What is the 16-bit word you are getting with that value?

 

 

Here's the thing with type casting: if you don't supply something large enough, it will use data that is directly after that data in memory.  Since a Double needs 8 bytes and you are just giving it 2 bytes, it will use the next 6 bytes in memory.  You have no clue what those extra bytes could be and therefore you get junk.

 

I still hold that there has to be a conversion mentioned somewhere in the manual.  Something like this will typically have a gain and an offset to apply to the raw value.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 18
(5,330 Views)

 

I've looked into the manual again. there is an example which I don't understand. maybe someone can help me with that.

 

The example shows how to read a value of +10.00 (in decimal) from the instrument.

 

the way I read the data is in a two bytes format with MSB first. in this way +10.00 is represented as:

 

byte 1: [3]

byte 2: [232-EOI]

 

the number represening 8 bit binary numbers.

 

Can someone help tp understand how the above represents +10.00?

 

 

 

 

 

 

 

 

0 Kudos
Message 9 of 18
(5,260 Views)

Byte 2 (LSB): decimal 232 = hex E8 = binary 11101000. Byte 1 (MSB): Decimal and hex 3 = binary 11. Combined: hex 3E8 = binary 1111101000 = decimal 1000. 

 

If 1000 = +10.00, then it is pretty easy to get positive numbers from 0.00 to +655.35. It is not at all clear how negative numbers would be represented.

 

Lynn

0 Kudos
Message 10 of 18
(5,243 Views)