Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to write hex value to serial port?

Posted on 2009-07-15
8
826 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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