|
-
Jun 2nd, 2022, 01:36 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] SrialPort readbyte to a Byte array failure
Hello all, following code will cause "Index was outside the bounds of the array." as exception:
Code:
Public Class MainWindow
Dim RxBuffer As Byte() = {}
Try
If SerialPort1.BytesToRead <> 0 Then
For index = 0 To SerialPort1.BytesToRead - 1
RxBuffer(index) = SerialPort1.ReadByte
Next
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
Application.Exit()
End Try
End Class
What I tried:
- Switching variable to other type(s).
- Adding/removing "()" and "{}".
- Adding/removing "index + 1" and "index - 1".
Adding "SerialPort1.ReadByte" in a textbox control once a loop works properly for whole data:
Code:
TextBox1.Text = TextBox1.Text & SerialPort1.ReadByte & " "
The goal is to store received bytes in an array to call/change in-application then clear it every time. The length is changeable every time.
Thanks for contribution in advanced.
-
Jun 2nd, 2022, 01:58 AM
#2
Re: SrialPort readbyte to a Byte array failure
If you expect to read N Bytes and assign them to the elements of an array then you need to have an array and you need that array to have at least N elements. Here:
vb.net Code:
Dim RxBuffer As Byte() = {}
you are creating an array with zero elements, so of course any index you specify will be out of range. The obvious thing to do is to create an array at the point you know how many elements it will need:
and:
vb.net Code:
Dim byteCount = SerialPort1.BytesToRead
If byteCount > 0 Then
buffer = New Byte(byteCount - 1) {}
For i = 0 To byteCount
buffer(i) = SerialPort.ReadByte()
Next
End If
Alternatively, use a List(Of Byte) and call Add, letting it grow as required. You can then call ToArray at the end if you specifically need an array.
Last edited by jmcilhinney; Jun 2nd, 2022 at 02:03 AM.
-
Jun 2nd, 2022, 02:03 AM
#3
Re: SrialPort readbyte to a Byte array failure
That said, why read the data Byte by Byte when the SerialPort has a Read method that will read multiple Bytes at a time? You can either create an array big enough to contain all the data and read it all in one go:
vb.net Code:
Dim byteCount = SerialPort1.BytesToRead
If byteCount > 0 Then
buffer = New Byte(byteCount - 1) {}
SerialPort.Read(buffer, 0 byteCount)
End If
or you can use a fixed-size buffer and read the data in chunks. That's how you generally read large data streams or data for which you don't know the size. I'll leave that to you as an exercise, noting that Read will return the number of Bytes read and that being less than the number of Bytes you specified to read means that there's no more data.
-
Jun 2nd, 2022, 11:24 AM
#4
Thread Starter
Hyperactive Member
Re: SrialPort readbyte to a Byte array failure
 Originally Posted by jmcilhinney
That said, why read the data Byte by Byte when the SerialPort has a Read method that will read multiple Bytes at a time?
What a fantastic idea! To be honest I didn't give a chance to try 'Read' method once. I'm using Serial Port in an external microcontroller low-level based stream. (Mostly Modbus RTU and creating a 16 bit values from (EvenByte*255)+OddByte)
Code:
Private buffer As Byte()
Rectifies the matter, sir.
I believe a comma is needed @ line #6 in last code 'Read' argument. 3:')
Since we got here, Please introduce the best way you know for an always ready to read port. I mean is there any "DataReceived" event or something to use cause I use a 1mS timer to do such thing (If byteCount > 0 Then communication is online and Rx is coming)
-
Jun 2nd, 2022, 11:43 AM
#5
Thread Starter
Hyperactive Member
Re: SrialPort readbyte to a Byte array failure
And two more questions:
1) How to get that "buffer" from "Private buffer As Byte()" length after all work? (.Length.toString and .GetLength doesn't work)
2) Didn't want to get it from SerialPort1.BytesToRead because I want to figure out what is happening due to exception which is happening when I stream a non-stop data with such message: "Index was outside the bounds of the array.". Is there a way to clear that "buffer" every cycle?
-
Jun 2nd, 2022, 11:55 AM
#6
Re: SrialPort readbyte to a Byte array failure
My approach to SerialPorts is different than most. I tend to have a small received event handler that accumulates bytes and a separate process that enforces protocol.
See: https://www.vbforums.com/showthread....=1#post5568506
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
|