Solved

How to write hex value to serial port?

Posted on 2009-07-15
8
827 Views
Last Modified: 2013-11-26
How can I write individual bytes say (x7e) in hex  to a serial port ? I have about an array lsit of 82 value that i need to write to a com port
serialCOMDiscover.Write()

Open in new window

screen-shot.jpg
0
Comment
Question by:cmdolcet
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:raja_ind82
ID: 24866430
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24867728
Wow you have been given a variety of links already including those from Delphi, MFC, VB, and VB.NET. But also have a look at this tutorial

http://www.dreamincode.net/forums/showtopic37361.htm
0
 

Author Comment

by:cmdolcet
ID: 24868588
Ok when I look at this tutorial. I see they ahve a method of  Transmission in HEX when i look at the code and plug it into my code I get an error on the line

HextoByte(msg)  is not declared?

 Dim newMsg As Byte() = HexToByte(msg)

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24868639
If you go a bit further down the article, you would find the definition of this method



#Region "HexToByte"
''' <summary>
''' method to convert hex string into a byte array
''' </summary>
''' <param name="msg">string to convert</param>
''' <returns>a byte array</returns>
Private Function HexToByte(ByVal msg As String) As Byte()
    'Here we added an extra check to ensure the data
    'was the proper length for converting to byte
    If msg.Length Mod 2 = 0 Then
        'remove any spaces from the string
        _msg = msg
        _msg = msg.Replace(" ", "")
        'create a byte array the length of the
        'divided by 2 (Hex is 2 characters in length)
        Dim comBuffer As Byte() = New Byte(_msg.Length / 2 - 1) {}
        For i As Integer = 0 To _msg.Length - 1 Step 2
            comBuffer(i / 2) = CByte(Convert.ToByte(_msg.Substring(i, 2), 16))
        Next
        write = True
        'loop through the length of the provided string
        'convert each set of 2 characters to a byte
        'and add to the array
        'return the array
        Return comBuffer
    Else
        'Message wasnt the proper length
        'So we set the display message
        _msg = "Invalid format"
        _type = MessageType.Error
        ' DisplayData(_Type, _msg)
        'Set our boolean value to false
        write = False
        Return Nothing
    End If
End Function
#End Region

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 24868732
ok thanks for the post back so fast. I need to ask this one important question you see the screen shot of the arraylist that im putting all this stuff into . I have some being passed as string and some as bytes.....Whats the best way to do this?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24868768
I do not know the structure of your program but you need to make segments of the code in a way that you could use the functions in both situations. For example, you could have two functions

WriteBytes(bytestowrite)
and
WriteString(stringtowrite)

The WriteString function would convert the string to bytes and then call the WriteBytes function internally.
0
 

Author Comment

by:cmdolcet
ID: 24868817
ok but how could i write each char one by one to the com port

for intloop =0 to arraylist_szie-1
comport1.write(char(i))

end loop

How could i write something like that

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24868849
You need to study that article in detail and based on that you could write characters one by one. Or modify the code in the Write method to write one char at a time instead of a string.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question