TheGoat Level: Trainee
 Registered: 13-12-2007 Posts: 1
|
Re: Sending Hex on serial port
I know this thread is a bit old but just to avoid any misunderstanding.....
Hexadecimal (Hex) is a notation, not a data type.
ie Hex is a representation of an 8 bit binary value in Base 16.
For example, suppose you've got one of those TVs that you can connect to your serial port and control it from your computer. Let's also assume that in the Manual, it states that to turn it on you have to send it the following (Hexadecimal) codes: 02 03 64 60 28 03
In other words you have to send the following binary data:
0000 0010 (02)
0000 0011 (03)
0110 0100 (64)
0110 0000 (60)
0010 1000 (28)
0000 0011 (03)
There are 2 ways you can do this, either by using the Character representation of the binary value:
Dim strCommand As String
strCommand = Chr$(&H2) & Chr$(&H3) & Chr$(&H64) & Chr$(&H60) & Chr$(&H28) & Chr$(&H3)
MSComm1.Output = strCommand
|
or you could use a Byte Array:
Dim bytCommand(5) As Byte
bytCommand(0) = &H02
bytCommand(1) = &H03
bytCommand(2) = &H64
bytCommand(3) = &H60
bytCommand(4) = &H28
bytCommand(5) = &H03
MSComm1.Output = bytCommand
|
Note the difference between Hex() and &H.
Hex(255) will return the characters "FF" ie 2 bytes which will be:
0100 0110 0100 0110 (hex 46 46)
&H(FF) will return one byte, which will be:
1111 1111 (hex FF)
So, if you want to send Hex FF you'd use &HFF not Hex(255)
[Edited by TheGoat on 14-12-2007 at 06:10 AM GMT]
|