Hexadecimal Conversions
Sensiva
Posts: 211
Hi..
I am trying to write a program to control TACT amplifier, an excellent amplifier with a creepy rs232 protocol
my client wants to ramp the volume up and down using a bar graph, so i created a level event for this bar graph to store the last level value of it and make some calculations to convert this level to two hex words..using ITOHEX conversion keyword which returns an ascii string.
My problem is how to send this new hex value back to the device?
I am trying to write a program to control TACT amplifier, an excellent amplifier with a creepy rs232 protocol
my client wants to ramp the volume up and down using a bar graph, so i created a level event for this bar graph to store the last level value of it and make some calculations to convert this level to two hex words..using ITOHEX conversion keyword which returns an ascii string.
My problem is how to send this new hex value back to the device?
0
Comments
SET MASTER LEVEL
$08,$01,$80,$6D,$01,$xx1,$xx2,$xx3
LEVEL = GAIN x 10
GAIN 00.0 = 000
GAIN 00.1 = 001
GAIN 01.0 = 010
GAIN 01.3 = 013
CONVERT LEVEL TO 16BIT HEX
xx1= MOST 8BIT
xx2= LEAST 8BIT
xx3 = (SUM OF ALL SEGMENTS) AND FF
EXAMPLE: SET TO GAIN 62.2
LEVEL = 62.2 x 10 = 622
LEVEL(HEX)=026E
xx1=02 xx2=6E xx3=(8+1+80+6D+01+2+6E) AND FF=67
FINAL STRING $08,$01,$80,$6D,$01,$02,$6E,$67
EXAMPLE: SET TO GAIN 62.2
LEVEL = 62.2 x 10 = 622
// Answer
Thanks for your piece of code , i really appreciate it
but there are two questions out of my head now
1st:: You didn't convert the integer LEVEL into HEX, and then divided LEVEL by $100.... is this a valid method.. i mean how netlinx could do such a math dividing a decimal value by a hexa value.
2nd:: i can realize a new concept from your code.. which is in netlinx a character type variable can hold characters whatever it's representations?
Thanx again
Question 1 Answer:
Decimal, Hex and Binary are just three different way at viewing numerical values.
A computer always see the values as binary. Where a human can see the numerical value in any way that make most sence to him. With out talking about a string literal representation of numerics. Just talking straight numerics, we use the Value that make most sence to us.
Dec Hex Binary
622 == 26E == 0000001001101110 == no conversion needed.
Decemel is Base 10
Hex is Base 16
Binary Base 2
It is easier for me to translate from base 16 to base 2 then it is for me to translate from base 10 to base 2. Each Digit in a hexidecimal number represents four binary digits.
0 2 6 E
0000 0010 0110 1110
So....
Good reading in this link...
http://mathforum.org/dr.math/faq/faq.bases.html
Question 2 Answer:
Not all variable types can be used in this fashion. NetLinx allows integer variable to be placed in char arrays. NetLinx truncates the top byte in this mode. Thereby leaving only the lower byte. The math that I did made sure NetLinx used the information that I needed.
Nice.. but i am completely aware about based numeric systems...
when i have read about conversion keywords i thought that variable can hold the value in its assigned base, i mean when assigning $26E to VAR1 it means that i can't use it in Send_string commands, or why there is a keyword ITTOHEX or HEXTOI ???,it is little confusing... but i will do some practice to figure this out, and i appreciate your help
Now... assume that i have two TP buttons for Volup(21) and Voldn(20)
and these are the rs232 commands
$06,$01,$80,$6E,$01,$F6 // volup
$06,$01,$80,$6E,$00,$F5 // voldn
now i wanna stack the two butt events like this i don't want to use switch case for Button.input.channel
i wanna use the relation between the two commands (the bold segment)
by doing some math like But.inp.chan - 20 then convert to hex , build the string -i don't care about the Checksum calcs now - and send it!!
i hope you got what i wanna say
so...
No need to convert to hex, same reasons as above...
Thanx...
So.. At last the conclusion is as all devices communicate with each other in Binary code... so i don't have to care about conversion because when i notate $26E will tell the master that it has to convert from hex to binary
'26E' convert from ASCII to binary
26 convert from decimal to binary
and i can make one variable hold as many bases as i want
VAR="$26E,'AsciiString',26"
and when sending it , the master gonna do the job
Thanx Jeff... you made my day
but one more silly question: why do you terminate your line with this [size=+2];[/size]?
Since I am a programmer in many languages, I use the semi-colon at the end of the line bacause it is a habit. NetLinx allows it, so I do not have to change. It does help NetLinx find the end of line better, in some cases it is needed.
This code cause a syntax error, the compiler thinks the first square bracket belongs to the variable decleration, assuming it is an array.
The semi-colon tells the compiler where the end of line is, and fixes the syntax issue.
I consider this as one of the fabioulus things in Netlinx , in your case you do programming in many other languages, so when doing some Netlinx you will not suffer the confusion.
Cheers