Serialport Receiving Data
Help required with receiving data using the serialport please
The receive data i'm interested in is 19 bytes long. The first two bytes are headers/qualifiers.
My problem is the hardware that replies with the above packet sends a load of zeros in front of it, these zeros can vary in length.
How would I go about filtering out the 19 bytes from this variable length data packet.
Thanks
Re: Serialport Receiving Data
I believe this will trim off the zeros.
Re: Serialport Receiving Data
Quote:
Originally Posted by Zero2Cool
I believe this will trim off the zeros.
I thought that only worked with strings... so you'd need to convert your byte array to a string to use that. I found this post that shows how to do that: http://bytes.com/forum/thread347655.html
Does this help at all?
Re: Serialport Receiving Data
I didn't really want to convert it to a string but I will give it a go!
Thanks for your help, I will let you know how I get on.
Re: Serialport Receiving Data
Code:
Dim buffer() As Byte = New Byte() {}
Dim theMess() As Byte = New Byte() {}
Dim byts2Rd As Integer = TheComPort.BytesToRead 'bytes available to be read
Array.Resize(buffer, byts2Rd) 'set buffer size
'read the bytes into the buffer
byts2Rd = TheComPort.Read(buffer, 0, buffer.Length) 'byts2Rd=number of bytes read
Dim idxOfhdr As Integer
Dim hdr1 As Byte = 3 'first header byte change to your value
idxOfhdr = Array.IndexOf(buffer, hdr1) 'is it in buffer
If idxOfhdr <> -1 Then 'if neq -1 then hdr1 is present
Array.Resize(theMess, buffer.Length - idxOfhdr) 'determine message length
Array.Copy(buffer, idxOfhdr, theMess, 0, theMess.Length) 'copy from hdr1 to end
End If
Re: Serialport Receiving Data
Big thanks dbasnett, unfortunately I can't try it until tomorrow but looks good to me.
As you seem to be serial comms guru can I please ask a few more questions. The hardware I'm working with requires me to request data before It sends me a response back. In the past(vb6) I have used a simple timer for the polling at an interval of 1 sec which worked well. Would the timer method be ok to use in .NET or can it be done more efficiently a loop maybe. The poll rate needs to be about every 30-50ms this time so it's quite a lot faster than I'm use to. If you have the time to help me create a reliable TX and RX routine that would be great:D , the decoding of the data shouldn't be a problem, I hope:)
Thanks
Re: Serialport Receiving Data
Ok I seem to be having some problems dbasnett, using the code above when it finds the first header a lot of the time the length of the packet(theMess) is too short, this I think is due to the datareceived handler firing several times and only having part of the correct packet in the buffer at that time of checking.
i.e. - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,h1,h2,d1,d2,d3,d4
As the receive packet length is variable is there a way to check for the headers only when all of the buffer has been read into the array.
Thanks
Re: Serialport Receiving Data
what you want to do is read 1 byte at a time till you get both headers, then read 17 more bytes.
pseudo code
hdr1f, hdr2f as bool
do while more bytesavail andalso not hdr1f andalso not hdr2f
read 1 byte
is byte hdr1f then hdr1f = true
is byte hdr2f then hdr2f = true
loop
if hdr1f andalso hdr2f andalso 17 bytes avail then
read 17 bytes
clear hdrf's
endif
try turning this into real code and post it if you have problems.
also, see link in my signature.