1

I am new to VB and I am trying to get a program to output text instead of hex. I found the code online, a program called maxiCOM. Here is the link http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm. The source code can be downloaded at the bottom of the page.

Unfortunately, the level of coding in the program is far above my level of understanding, and I don't really get how to change the output from hex to text. I would greatly appreciate it if someone could explain to me how to do this. The snippet of the code is

Public Class MaxiTester

Dim SpaceCount As Byte = 0
Dim LookUpTable As String = "0123456789ABCDEF"
Dim RXArray(2047) As Char ' Text buffer. Must be global to be accessible from more threads.
Dim RXCnt As Integer      ' Length of text buffer. Must be global too.

' Make a new System.IO.Ports.SerialPort instance, which is able to fire events.
Dim WithEvents COMPort As New SerialPort

Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPort.DataReceived
    Dim RXByte As Byte
    Do
        '----- Start of communication protocol handling -----------------------------------------------------------
        ' The code between the two lines does the communication protocol. In this case, it simply emties the
        '   receive buffer and converts it to text, but for all practical applications, you must replace this part
        '     with a code, which can collect one entire telegram by searching for the telegram
        '       delimiter/termination. In case of a simple ASCII protocol, you may just use ReadLine and receive
        '         in a global string instead of a byte array.
        ' Because this routine runs on a thread pool thread, it does not block the UI, so if you have any data
        '   convertion, encryption, expansion, error detection, error correction etc. to do, do it here.
        RXCnt = 0
        Do
            RXByte = COMPort.ReadByte
            RXArray(RXCnt) = LookUpTable(RXByte >> 4) ' Convert each byte to two hexadecimal characters
            RXCnt = RXCnt + 1
            RXArray(RXCnt) = LookUpTable(RXByte And 15)
            RXCnt = RXCnt + 1
            RXArray(RXCnt) = " "
            RXCnt = RXCnt + 1
            SpaceCount = (SpaceCount + 1) And 31      ' Insert spaces and CRLF for better readability
            If SpaceCount = 0 Then                    ' Insert CRLF after 32 numbers
                RXArray(RXCnt) = Chr(13) ' CR
                RXCnt = RXCnt + 1
                RXArray(RXCnt) = Chr(10) ' LF
                RXCnt = RXCnt + 1
            Else
                If (SpaceCount And 3) = 0 Then        ' Insert two extra spaces for each 4 numbers
                    RXArray(RXCnt) = " "
                    RXCnt = RXCnt + 1
                    RXArray(RXCnt) = " "
                    RXCnt = RXCnt + 1
                End If
            End If
        Loop Until (COMPort.BytesToRead = 0)
        '----- End of communication protocol handling -------------------------------------------------------------
        Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
    Loop Until (COMPort.BytesToRead = 0)  ' Don't return if more bytes have become available in the meantime
End Sub

' Text display routine, which appends the received string to any text in the Received TextBox.

Private Sub Display()
    Received.AppendText(New String(RXArray, 0, RXCnt))
End Sub

2 Answers 2

2
System.Text.Encoding.Unicode.GetString(bytes) 'bytes is your byte array'
Sign up to request clarification or add additional context in comments.

3 Comments

You should explain how this one-liner fits in OP's situation.
@dotNET Chances are they didn't read the question, just the title.
Upvoted as this is the way Microsoft recommends; may not be relevant to the question but still generally relevant learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/…
1

Notice the first line after Do in your inner loop:

RXByte = COMPort.ReadByte

This is the only point in your code where you have the actual byte value read from the COM port. After this point the code converts the byte into two hex values using bit-shifting. You should create a global variable of string type and append the read byte value to this string before it is lost.

Add the following line immediately after the above line:

YourString &= Convert.ToChar(RXByte).ToString()

where YourString is your global string variable.

Note that you can attain some efficiency by using StringBuilder instead of string, but I'm leaving that for you as exercise.

1 Comment

Thanks for the help! I got it to work after following your instructions and also modifying some of the display routine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.