Results 1 to 8 of 8

Thread: [RESOLVED] Serial Port Read Index Out Of Range Exception

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [RESOLVED] Serial Port Read Index Out Of Range Exception

    Well, Hello. It's me again.
    Consider a half-duplex modbus RTU serial communication between two devices which we want to only listen. A repeater.
    Without any CRC/Checksum complexity, I stuck at the beginning. What is the best approach to do such thing?

    Here's the code so far but making 2 bytes to an integer applies for first 8 bytes and then an exception will occur for line #30 which is comparing it is a HoldingRegister or not (Correct received package). I mean using Try/Catch will do the matter for first 8 bytes and continues the loop. I need the whole data. Any clues is precious. Thanks in advanced.
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Buffer As Byte()    'I'M NOT SURE IT IS THE BEST WAY TO DECLARE A BYTE ARRAY WITH UNKNOWN/CHANGABLE LENGTH OR NOT
    4.    
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         Control.CheckForIllegalCrossThreadCalls = False 'FOR ERRORS FROM NOWHERE(?)
    7.         Try
    8.             If SerialPort1.IsOpen = False Then
    9.             SerialPort1.Open()
    10.             End If
    11.         Catch ex As Exception
    12.             MsgBox(ex.Message, MsgBoxStyle.Critical)
    13.             Application.Exit()
    14.         End Try
    15.     End Sub
    16.  
    17.     Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    18.         If BackgroundWorker1.IsBusy = False Then
    19.             BackgroundWorker1.RunWorkerAsync()  'IT'S GOING TO BE A VERY CROWDY CODE. LET THE BACKGROUNDWORKER DO THE LOOP THING FOR ME
    20.         End If
    21.     End Sub
    22.    
    23.     Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    24.         Dim byteCount = SerialPort1.BytesToRead 'GET INPUT BUFFER SIZE
    25.         If SerialPort1.BytesToRead > 8 Then 'IGNORE BLANK AND NOT REQUEST COMMANDS (GET ONLY ANSWERS FROM SLAVES. REQUEST COMMANDS ARE 8 BYTES)
    26.             buffer = New Byte(byteCount) {}     'EXTEND VARIABLE/ARRAY AS IT IS
    27.             SerialPort1.Read(Buffer, 0, byteCount)  'READ SERIAL PORT FUNCTION
    28.             Dim UserControl as UserControl() = {UserControl1, UserControl2, UserControl3, UserControl4, UserControl5, UserControl6, UserControl7, UserControl8, UserControl9, UserControl10, UserControl11, UserControl12, UserControl13, UserControl14}    'THERE ARE 14 USERCONTROLS WHICH SORTING VALUES IN THEM IS REQUIRED
    29.            
    30.             If Buffer(1) = 3 And Buffer(2) = 192 Then 'CORRECT PACKET(S) INCOMING
    31.            
    32.                 If Buffer(0) = 2 Then   'FIRST PARAMETER              
    33.                     For i = 0 To 29 Step 2  'ARRANGING/ASSIGNING VALUES IN THEIR PLACES FOR 14 USERCONTROLS
    34.                     UserControl(i/2).TextBox1.Text = (Val(Buffer(i+3)*256) + Val(Buffer(i+4))   'COMBINE 2 BYTES => INTEGER
    35.                     Next               
    36.                 End If
    37.                
    38.                 'If Buffer(0) = 3 Then  'SECOND PARAMETR
    39.                 ''SAME AS ABOVE
    40.                 'End If
    41.                 '.
    42.                 '.
    43.                 '.
    44.                 'If Buffer(0) = n Then  'LAST PARAMETR
    45.                 ''SAME AS ABOVE
    46.                 'End If
    47.             End If
    48.     'POSSIBLE 'BUFFER' DISPOSE/CLEAR CODE GOES HERE WHICH I DON'T KNOW HOW TO DO IT
    49.         End If
    50.     End Sub
    51. End Class

    Exception full message:
    IndexOutOfRangeException was unhandled by user code: Index was outside the bounds of the array.
    Last edited by pourkascheff; Jun 10th, 2022 at 03:43 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width