Click here to Skip to main content
15,307,395 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am receiveing a Control string of 48 bytes, using serial port in VB.net. I am able to receive string correctly, using delegate, and display it correctly on a textbox. but whenever i use this string for processing to a subroutine it gives error "ArgumentOutOfRangeException was unhandelled"- "Index and length must refer to a location within the string. Parameter name: length"
DataReceived code is
VB
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
        strReceivedControlword = SerialPort1.ReadLine()
        Me.Invoke(UpdateFormDelegate1)
    End Sub
    Private Sub UpdateDisplay()
	'txtControlWord.Text = CStr(strReceivedControlword)
        ProcessControlWord(CStr(strReceivedControlword))
    End Sub

ProcessControlWord block changes colors of some buttons depending upon received string. ProcessControlWord block is ..
VB
Public Sub ProcessControlWord(ByVal strControlword As String)
        Dim ControlWordArray(5) As UShort
        Dim strRelayRequiredStatus As String = ""
        Dim str As String = ""
        Dim i As Integer

        For i = 0 To strControlword.Length - 1 Step 4
            ' Take 2 Bytes in Hex and put in Array (Decimal )
            ControlWordArray(i / 4) = Convert.ToUInt16(strControlword.Substring(i, 4), 16)
        Next
        'START OF STRING CHECK
        If ControlWordArray(0) = 35 Then
            Dim CRC As Integer
            'CRC CHECK
            For i = 0 To 3
                CRC = CRC Xor ControlWordArray(i)
            Next 'i = 0
            'Relay Code 0000-0001-1011-0110-1101-1011-0110-1101
            'CRC - DBAE
            If CRC = ControlWordArray(4) Then
                'lblDownLinkCode.Text = String.Concat(lblDownLinkCode.Text, "Command ID is           : ", vbTab, ControlWordArray(1), vbCrLf)
                str = String.Concat(Convert.ToString(ControlWordArray(2), 2).PadLeft(16, "0"c), _
                                    Convert.ToString(ControlWordArray(3), 2).PadLeft(16, "0"c))
                For i = 0 To 24 Step 1
                    booRelayStatus(i) = str.Substring(31 - i, 1)
                    If Val(str.Substring(31 - i, 1)) = 1 Then
                        btnArray(i).BackColor = Color.Red
                        btnArray(i).Text = "ON"
                    Else
                        btnArray(i).BackColor = Color.Green
                        btnArray(i).Text = "OFF"
                    End If
                Next
            Else
                For i = 0 To 24 Step 1
                    btnArray(i).Text = "CRC Mismatch"
                Next
            End If
        End If
    End Sub
<pre lang="vb">

Pl help
Posted
Comments
   
In what line?
—SA
mairothia 13-Jun-15 5:34am
   
It is at
Me.Invoke(UpdateFormDelegate1)
Ralf Meier 13-Jun-15 5:45am
   
Yes, of course ...because this method is responsible for the working with the string (indirect).
Set a Breakpoint a "me.invoke(..)" and look what your content in "strReceivedControlword" is at that moment ...
Ralf Meier 12-Jun-15 2:52am
   
Have you ever checked if the received string has the length that you await ?
Have you checked your strControlword to it's Content ?
I suppose that the error occures by the use of the .substring-method.
mairothia 13-Jun-15 5:38am
   
I checked length is correct.
Same code is working fine with tcpclient
mairothia 14-Jun-15 13:40pm
   
Thanks to all.
My problem is solved considering your comments.
It was showing 48 bytes send and 48 bytes received. I was transmiting 48 bytes with
serialport1.write
statement and reading with
serialport1.readline

when I wrote with serialport1.writeline problem is solved.

Thanks to all

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900