Click here to Skip to main content
13,772,600 members
Rate this:
Please Sign up or sign in to vote.
See more:
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:

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

        RXCnt = 0

            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 6-Nov-17 18:10pm
Updated 6-Nov-17 22:34pm
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.181119.1 | Last Updated 7 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100