|
-
Jul 8th, 2008, 04:48 AM
#1
Thread Starter
Lively Member
[RESOLVED] [2008] Array.IndexOf always returns -1
I am having trouble with the following piece of code, the searchIndex always returns -1. If I put a breakpoint after 'searchIndex = Array.IndexOf(buffer, &H90)' and look in the buffer array I can see 90h so it should return the index but it doesn't.
This code is in my serialport DataReceived sub
Any help would be great!
Thanks
Code:
Dim searchIndex As Integer
Dim buffer() As Byte = New Byte() {}
Dim bytesRead As Integer
bytesRead = SerialPort.BytesToRead ' Get how many bytes available.
Array.Resize(buffer, bytesRead) ' Set the buffer to the number of bytes available.
bytesRead = SerialPort.Read(buffer, 0, bytesRead) ' Read the bytes
If bytesRead > 0 Then
searchIndex = Array.IndexOf(buffer, &H90)
If searchIndex <> -1 AndAlso buffer(searchIndex + 1) = &H90 Then
If (searchIndex + 18) <= buffer.Length Then
For x As Integer = searchIndex To (searchIndex + 18)
Debug.WriteLine(buffer(x).ToString)
Next
End If
End If
End If
Last edited by Adrian26; Jul 8th, 2008 at 05:57 AM.
-
Jul 8th, 2008, 05:40 AM
#2
Re: [2008] Array.IndexOf always returns -1
Code:
Dim buffer() As Byte = New Byte() {1, 2, 10, 11}
Dim lk4 As Byte = &HA
Dim iof As Integer = Array.IndexOf(buffer, lk4)
your code looks like something i have seen b4.
-
Jul 8th, 2008, 05:49 AM
#3
Thread Starter
Lively Member
Re: [2008] Array.IndexOf always returns -1
Yes I think you may have 
searchIndex now returns correctly, thanks
Last edited by Adrian26; Jul 8th, 2008 at 05:57 AM.
-
Jul 8th, 2008, 05:52 AM
#4
Re: [2008] Array.IndexOf always returns -1
remember to mark thread resolved, etc.
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
|