Writing to Serial Port

  • Thread starter Thread starter kronecker
  • Start date Start date
K

kronecker

The write command is something like this

Serial1.Port.Write("command")

where command is a string.

How do I write a Hex number? ie something like

A2FF

Thanks

K.
 
The write command is something like this

Serial1.Port.Write("command")

where command is a string.

How do I write a Hex number? ie something like

A2FF

There are no Hex numbers. Only Hex Strings. Do you want to send two bytes
which' values are A2 and FF (=162 and 255)? If yes, use the other overloaded
method, like

Dim b As Byte() = {&HA2, &HFF}

serial1.write(b, 0, 2)


Armin
 
Hello Kronecker,
There are no Hex numbers. Only Hex Strings. Do you want to send two
bytes which' values are A2 and FF (=162 and 255)? If yes, use the other
overloaded method, like

Dim b As Byte() = {&HA2, &HFF}

serial1.write(b, 0, 2)

In addition to what Armin wrote and my previous posting for the
conversion of a number to hex string and binary string:

Hex as well as Octal are just representations in a different format.

For example the decimal number 25:
In binary it is 00011001,
in octal it is 31 (00 011 001),
in hex it is 19 (0001 1001).

Note that the binary value has never changed, but only the way to
represent it.

So, these variables will always contain the same value:

Dim OctalValue As Byte = &O31
Dim DecimalValue As Byte = 25
Dim HexValue As Byte = &H19

If OctalValue = DecimalValue And OctalValue = HexValue Then
Debug.Print("Values are identical.")
Else
Debug.Print("Values are different.")
End If

So regarding your question, you should consider if you want to send a
string (Dim HexString As String = Hex(25)) or if it is that you want to
send a numeric value (Dim HexValue As Byte = &H19) over the serial port.

Best regards,

Martin
 
IMO, sending byte-by-byte is not the best approach. You should send an
array of bytes for best performance. Even if you only transmit a single
byte, there is no real downside to using an array.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
There are no Hex numbers. Only Hex Strings. Do you want to send two bytes
which' values are A2 and FF (=162 and 255)? If yes, use the other overloaded
method, like

Dim b As Byte() = {&HA2, &HFF}

serial1.write(b, 0, 2)

Armin

Great - what's the format of (b,0,2)? The 2 is two elements of the
array I suppose - what's the zero?

Will this work


Dim objPort As New SerialNET.Port

objPort.BaudRate = 4800
objPort.ComPort = 2
objPort.Enabled = True ' Starts serial port.

Dim binary_data As Byte() = {&H81, &H82}

' Write binary data
objPort.Write(SerialNET.Port.ByteArrayToString(binary_data))
 
Great - what's the format of (b,0,2)? The 2 is two elements of the
array I suppose - what's the zero?

You can always press F1, or hover the mouse over the method call, or look in
the object browser. .... Zero is the start index in the array.
Will this work


I don't know.
Dim objPort As New SerialNET.Port

objPort.BaudRate = 4800
objPort.ComPort = 2
objPort.Enabled = True ' Starts serial port.

Dim binary_data As Byte() = {&H81, &H82}

' Write binary data
objPort.Write(SerialNET.Port.ByteArrayToString(binary_data))


Why ByteArrayToString? What do you want to send? Two bytes? Or "A2FF"? Or
"¢ÿ"? (which are the characters with character codes &HA2 and &HFF) If you
want to send character codes, which Encoding/codepage has to be used when
converted to an array of bytes?

What is SerialNet.Port? I only know System.IO.Ports.SerialPort.


Armin
 
You can always press F1, or hover the mouse over the method call, or look in
the object browser. .... Zero is the start index in the array.



I don't know.



Why ByteArrayToString? What do you want to send? Two bytes? Or "A2FF"? Or
"¢ÿ"? (which are the characters with character codes &HA2 and &HFF) If you
want to send character codes, which Encoding/codepage has to be used when
converted to an array of bytes?

What is SerialNet.Port? I only know System.IO.Ports.SerialPort.


Armin

SerialNet.Port seems to be connected with TCP ports.

Mike
 
Back
Top