Click here to Skip to main content
15,884,080 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
dim ECUData(27) as integer
dim RXArray(27) as integer


it shows me error on index 12th of ECUdata and Rxarray both

I am at the start and I am really trying to learn things for programming.

What I have tried:

VB
Private Sub Receiver(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles COMPorts.DataReceived
        If COMPorts.IsOpen = False Then
            COMPorts.Open()
        End If
        Dim RXByte As Byte

        RXCnt = 0
        Do

            RXByte = COMPorts.ReadByte

            RXArray(RXCnt) = Chr(RXByte) ' Convert each byte to two hexadecimal characters
            RXCnt = RXCnt + 1
            ECUData(RXCnt) = RXByte  <---------Getting Error in This Line
        Loop Until (COMPorts.BytesToRead = 0) ' Don't return if more bytes have become available in the meantime
        '----- End of communication protocol handling -------------------------------------------------------------
        Me.Invoke(New MethodInvoker(AddressOf Display)) ' Start "Display" on the UI thread
    End Sub
Posted
Updated 6-Nov-17 21:34pm
v2
Comments
Dave Kreskowiak 6-Nov-17 23:47pm    
Did you look at the value of RXCnt? I'll bet that its 27 when the error throws. The elements in the array you allocated are numbered 0 through 26, there is no 27.
Richard Deeming 7-Nov-17 13:38pm    
It's VB.NET, so the arrays actually have 28 elements from 0 to 27. :)
Dave Kreskowiak 7-Nov-17 14:14pm    
<headdesk>Haven't been in VB.NET for quite a while now.

Use the debugger.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

I suspect that because you don't reset RXCnt anywhere, it just overflows your array.
 
Share this answer
 
You know that is going to happen if
COMPorts.BytesToRead > 27

You either redim your arrays to make sure all the incoming data can fit or discard any data coming after those arrays are full.
 
Share this answer
 
Comments
Richard Deeming 7-Nov-17 13:40pm    
It's VB.NET, so the number in brackets is the upper-bound, not the length. It's the 29th byte that causes the problem. :)

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