|
-
Jun 10th, 2022, 03:38 AM
#1
Thread Starter
Hyperactive Member
[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:
Public Class Form1
Private Buffer As Byte() 'I'M NOT SURE IT IS THE BEST WAY TO DECLARE A BYTE ARRAY WITH UNKNOWN/CHANGABLE LENGTH OR NOT
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False 'FOR ERRORS FROM NOWHERE(?)
Try
If SerialPort1.IsOpen = False Then
SerialPort1.Open()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Application.Exit()
End Try
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
If BackgroundWorker1.IsBusy = False Then
BackgroundWorker1.RunWorkerAsync() 'IT'S GOING TO BE A VERY CROWDY CODE. LET THE BACKGROUNDWORKER DO THE LOOP THING FOR ME
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim byteCount = SerialPort1.BytesToRead 'GET INPUT BUFFER SIZE
If SerialPort1.BytesToRead > 8 Then 'IGNORE BLANK AND NOT REQUEST COMMANDS (GET ONLY ANSWERS FROM SLAVES. REQUEST COMMANDS ARE 8 BYTES)
buffer = New Byte(byteCount) {} 'EXTEND VARIABLE/ARRAY AS IT IS
SerialPort1.Read(Buffer, 0, byteCount) 'READ SERIAL PORT FUNCTION
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
If Buffer(1) = 3 And Buffer(2) = 192 Then 'CORRECT PACKET(S) INCOMING
If Buffer(0) = 2 Then 'FIRST PARAMETER
For i = 0 To 29 Step 2 'ARRANGING/ASSIGNING VALUES IN THEIR PLACES FOR 14 USERCONTROLS
UserControl(i/2).TextBox1.Text = (Val(Buffer(i+3)*256) + Val(Buffer(i+4)) 'COMBINE 2 BYTES => INTEGER
Next
End If
'If Buffer(0) = 3 Then 'SECOND PARAMETR
''SAME AS ABOVE
'End If
'.
'.
'.
'If Buffer(0) = n Then 'LAST PARAMETR
''SAME AS ABOVE
'End If
End If
'POSSIBLE 'BUFFER' DISPOSE/CLEAR CODE GOES HERE WHICH I DON'T KNOW HOW TO DO IT
End If
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|