Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translating data received over serial Encoding = System.Text.Encoding.GetEncoding(1252) 1

Status
Not open for further replies.

cdck

Programmer
Nov 25, 2003
281
US
I'm communicating over a serial port to a piece of equipment which needs to receive the data in Hex format. Recently, a requirement came up to not only send the commands, but receive responses and parse them. I have been successfully sending commands for some time, and I am able to capture the response, but cannot decode it to something which can be used further.

Here is the code currently in use:
Code:
 Public Function SendCom(ByVal strCommand As String, ByVal COMport As String)
        Dim RetVal
        Dim BS As New IO.Ports.SerialPort
        Try
            BS.PortName = COMport 'sets the COM port based on the setting on the main form
            BS.BaudRate = 9600
            BS.StopBits = IO.Ports.StopBits.One
            BS.DataBits = 8
            BS.Parity = IO.Ports.Parity.None
            BS.Encoding = System.Text.Encoding.GetEncoding(1252)
            BS.Handshake = Ports.Handshake.RequestToSendXOnXOff

            If BS.IsOpen = False Then
                BS.Open()
            End If

            BS.Write(strCommand)
            Threading.Thread.Sleep(20)
            BS.ReadTimeout = 100
            RetVal = BS.ReadExisting
            BS.Close()

            RetVal = HexToStr(RetVal)
            Return RetVal

        Catch ex As TimeoutException
            MsgBox(ex.Message)
            Return "FAIL"
        End Try
    End Function

    Function HexToStr(ByVal hex As String) As String
        Dim text As New System.Text.StringBuilder(hex.Length \ 2)
        For i As Integer = 0 To hex.Length - 2 Step 2
            text.Append(Chr(Convert.ToByte(hex.Substring(i, 2), 16)))
        Next
        Return text.ToString
    End Function

In the HexToStr function, the first time it gets to the text.append line, it throws an error that it "Could not find any recognizable digits."

How can I translate the data back into a string I can parse?

Cheryl dc Kern
 

Is the response actually in hexadecimal? What does the documentation for the device say? Without knowing what is being returned, there's not much else I can tell you.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
That gave me a thought. When I first programmed the code to send the data to the device, I had difficulty getting it to send correctly; although the string was hex according to vb formatting, it wasn't hex according to the receiving device. That's why I had to use the special encoding on the port.

When the string comes back across the port, it is still in the port-encoded format. I added a line to translate from a string, as opposed to from hex, and now it functions correctly.

New code:
Code:
    Public Function SendCom(ByVal strCommand As String, ByVal COMport As String)
        Dim RetVal
        Dim BS As New IO.Ports.SerialPort
        Try
            BS.PortName = COMport 'sets the COM port based on the setting on the main form
            BS.BaudRate = 9600
            BS.StopBits = IO.Ports.StopBits.One
            BS.DataBits = 8
            BS.Parity = IO.Ports.Parity.None
            BS.Encoding = System.Text.Encoding.GetEncoding(1252)
            BS.Handshake = Ports.Handshake.RequestToSendXOnXOff

            If BS.IsOpen = False Then
                BS.Open()
            End If

            BS.Write(strCommand)
            Threading.Thread.Sleep(20)
            BS.ReadTimeout = 100
            RetVal = BS.ReadExisting
            BS.Close()
            MsgBox(RetVal)
            RetVal = StrToHex(RetVal) '<-- new line
            Return RetVal

        Catch ex As TimeoutException
            MsgBox(ex.Message)
            Return "FAIL"
        End Try
    End Function

    'The function called by the new line:
    Public Function StrToHex(ByRef Data As String) As String
        Dim sVal As String
        Dim sHex As String = ""
        While Data.Length > 0
            sVal = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()))
            Data = Data.Substring(1, Data.Length - 1)
            sHex = sHex & " &H" & sVal
        End While
        Return sHex
    End Function


Cheryl dc Kern
 

Glad to help! Sometimes all you need is for someone to ask the right question.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top