vishalkbhatt 0 Newbie Poster

Hi everyone, I am working on an application which works with serial port communication with a PLC machine using modbus protocol. It is developed in VB.NET.
Now I am able to establish connection using hyperterminal but i m stuck at creating the modbus data frame and in that generation LRC. So please anyone who has any idea of creating a functions for modbus data frame and LRC generation then pls help me. posting the code that i have written but its not working, if anyone has a readymade function code then also it is highly appreciated.

Private Sub Transmitter(ByVal sender As Object, ByVal e As EventArgs) Handles SendButton.Click
        Dim TextString As String
        Dim TXArray(2047) As Byte
        Dim I, cnt As Integer
        Dim J As Integer = 0
        Dim Ascii As Boolean = False
        Dim Quote As Boolean = False
        Dim Temp As Boolean
        Dim Second As Boolean = False
        Dim TXByte As Byte = 0
        Dim CharByte As Byte
        If COMPort.IsOpen Then
   
            Dim hexNumbers As System.Text.StringBuilder = New System.Text.StringBuilder
            Dim lrc1 As String

            hexNumbers.Append("60")
            hexNumbers.Append(Long.Parse(Convert.ToString("03")).ToString("X2"))
            
            TextString = hexNumbers.ToString()
            MsgBox(TextString)
            lrc1 = GetLRC(TextString)

            hexNumbers.Append(lrc1)
            cnt = hexNumbers.Length
            COMPort.Write(hexNumbers.ToString, 0, cnt)
         Else
            MsgBox("COM port is closed. Please select a COM port")
        End If
        
    End Sub

    Private Function GetLRC(ByVal cmString As String) As String

        Dim lrc = 0, xlrc As Long
        Dim sum As String
        Dim parse As String
        Dim sb As System.Text.StringBuilder
        Dim i, j As Integer
        Dim b As Byte
        j = 0
        sum = 0
        MsgBox(cmString)
        For i = 0 To cmString.Length - 1 Step 2
            parse = cmString.Substring(i, 2)
            lrc = lrc + Long.Parse(parse, System.Globalization.NumberStyles.HexNumber)
        Next i

        lrc = (lrc Xor &HFFFFFFFFUI)

        lrc = lrc + 1
        xlrc = Convert.ToString(lrc, 16)
        lrc = Long.Parse(xlrc, System.Globalization.NumberStyles.HexNumber)

        b = Convert.ToByte(lrc)
       
        Return Long.Parse(Convert.ToString(b)).ToString("X2")

    End Function