473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Writing to Serial Port

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.
Jun 27 '08 #1
6 17011
<kr*******@yahoo.co.ukschrieb
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

Jun 27 '08 #2
Hello Kronecker,
>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)
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
Jun 27 '08 #3
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.
Jun 27 '08 #4
On Jun 26, 10:01 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
<kronec...@yahoo.co.ukschrieb
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
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(bin ary_data))
Jun 27 '08 #5
<kr*******@yahoo.co.ukschrieb
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?
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(bin ary_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

Jun 27 '08 #6
On Thu, 26 Jun 2008 22:29:35 +0200, in
microsoft.public.dotnet.languages.vb "Armin Zingler"
<az*******@freenet.dewrote:
><kr*******@yahoo.co.ukschrieb
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?

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(bi nary_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
SerialNet.Port seems to be connected with TCP ports.

Mike

Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
1
by: lec | last post by:
Hi, I'm trying to write a program to read from the serial port & write whatever that is read to the X console (/dev/tty7). For X to recognize the characters sent, I believe you have to send...
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
13
by: jay.dow | last post by:
I want to write to the pins of an RS232 without using the serial protocol. The use would be every pin could act to complete a circuit in customized hardware. I could use python to communicate...
4
by: Frank | last post by:
Hello, how to get information about all serial ports in the PC? I use the following code, but i got only the data of the FIRST serial port. All other serial port information are not available...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
4
by: Petr Jakes | last post by:
I am trying to save data it is comming from the serial port continually for some period. (expect reading from serial port is 100% not a problem) Following is an example of the code I am trying to...
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
2
by: Nasif | last post by:
Currently I am writing a program which sends and receives messages through serial port to a device. I am using C# and Microsoft Visual studio 2005 for windows program. But my problem is when i try...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.