Results 1 to 8 of 8

Thread: Serialport Receiving Data

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    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

  2. #2
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: Serialport Receiving Data

    I believe this will trim off the zeros.
    vb Code:
    1. Trim(strString)

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Serialport Receiving Data

    Quote Originally Posted by Zero2Cool
    I believe this will trim off the zeros.
    vb Code:
    1. Trim(strString)
    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?
    Last edited by chris128; Jul 8th, 2008 at 09:37 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    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.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    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 , the decoding of the data shouldn't be a problem, I hope

    Thanks
    Last edited by Adrian26; Jul 8th, 2008 at 04:27 PM.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2002
    Location
    UK
    Posts
    123

    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

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    Last edited by dbasnett; Jul 9th, 2008 at 01:52 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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