Solved

How to write hex value to serial port?

Posted on 2009-07-15
8
811 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now