.NET Framework - Send a Hex value to a serial port.

Asked By Aussie Rules on 19-Oct-09 03:33 AM
Hi,

I am needing to send a hex value, combined with a string value to a serial
device, and am not sure how to do this.

This is what is needed to be sent:

*Gxxy$M XO$X01001STR1$X02001STR2$X03010\x01\x02$ (+ CRLF at end)

The support person for the serial device has given me the following
information;

for hex 01 you have to send  ^A as a decimal character
for hex 02 you have to send  ^B

I would normally send data to the serial port via
seriald.writeling(stringvalue)

As this is in the end a string I am at a loss as to how to do the hex as
well...

Thanks for any advice....




Patrice replied on 19-Oct-09 07:13 AM
My understanding is that those characters should be sent as characters whose
code is 1 and 2. See the Chr function...

Another option (likely my preference) would be to write them as a byte array

Dim b() As Byte= {1,2}

sp.Write(MyString) ' Write the string
sp.Write(b,0,2) ' Then Chr(1), Chr(2)

--
Patrice


de discussion : PM00047644C3F4DA49@heath-dillons-macbook.Headland.local...
Armin Zingler replied on 19-Oct-09 07:30 AM
Aussie Rules schrieb:

Patrice already told you how to write the data.
In addition: Hex values are Strings. If you have a String containing
hexadecimally formatted values but you have to send the values themselves, you
have to convert the String into the values first. Unfortunatelly I do not know
how to interpret the String example you gave above. You must look at the
syntax description that you probably find in the manual of the device you are
communicating with.
Well, if the String given does not exist (at runtime) and it is only a part
of the description of the protocol, then no conversion is necessary. Though,
you still have to consult the manual how to interpret it.

--
Armin
DickGrier replied on 19-Oct-09 02:28 PM
Hi,

Assign each value to an array of type Byte, then use the Write method, which
has an overload for this data type.  Eg.

Dim Buffer(3) As Byte 'just for example - some binary values
Buffer(0) = &H1
Buffer(1) = &H2
Buffer(2) = &HFF
Buffer(3) = &HD      'Carriage Return character

SerialPort.Write(Buffer, 0, Buffer.Length)

Naturally, your "buffer" will have different content than my simple-minded
example.

I have lots of serial port example code in my book (see below), and
illustrate several protocols, though not specifically the one that you are
using.

Dick

--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.
James Hahn replied on 20-Oct-09 11:43 PM
The required statement is:

Dim Msg as String
Msg = "*Gxxy$M XO$X01001STR1$X02001STR2$X03010" & chr(1) & Chr(2) & "$" &
vbCrLf

except that I suppose you will be inserting the specific message identifier
instead of xxy.  Also, be wary of that space in your example - check that it
really should be there.  And I also wonder about STR1$ and STR2$ - are those
placeholders that you are supposed to replace with the actual values of some
parameter strings, or in fact are the parameters included in the string
already.  Eg, should it actually be :

Dim Msg as String
Dim Str1 as String = "Something"
Dim Str2 as String = "Something Else"
Msg = "*Gxxy$M XO$X01001" & STR1 & "X02001" & STR2 & "X03010" & chr(1) &
Chr(2) & "$" & vbCrLf

and if so does X0$ fall into the same category?
Aussie Rules replied on 21-Oct-09 12:28 AM
Hi Patrice,

Thanks for your reply.

I seem to be having some success with your code, but not sure how to go
about sending just one value such as 0x12.

How would I convert the code to be just for this single value. I changed the
Byte={1,2} to be ={11} but then the write(b,0,2) failed(changed 2 to 1, but
no luck)

Thanks
Patrice replied on 21-Oct-09 06:58 AM
Use &h12 in VB. VB prefix values expressed using the hexadecimal format with
&h rather than 0x


Should have worked (but if 11 is supposed to be an hexadecimal value it
should be &h11). You may want to explain what is the behavior you see.

--
Patrice