Solved

How to write hex value to serial port?

Posted on 2009-07-15
8
817 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

919 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

19 Experts available now in Live!

Get 1:1 Help Now