Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to write hex value to serial port?

Posted on 2009-07-15
8
Medium Priority
?
840 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 83

Accepted Solution

by:
CodeCruiser earned 2000 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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