Results 1 to 8 of 8

Thread: Serial Data - How to make sure the correct data is received?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Serial Data - How to make sure the correct data is received?

    I am using Visual Basic express to create a serial interface to a multi-room audio controller. I have started by creating a form that can connect to the controller and I can receive data from the Controller and display Zone status, volume etc in corresponding texts box thaks to help from Stanav and Tassa in my previous thread.

    My next problem is to do with receiving lots of data from the serial port and my application getting out of sync. This is mainly caused by turning the volum control as each time it is turned a little bit the controller sends out the new status of the Zone being effected

    eg
    ****Turning Volume knob on Zone 1***
    Status returned from Controller:
    #Z01PWRON,SRC3,VOL05<CR>
    #Z01PWRON,SRC3,VOL10<CR>
    #Z01PWRON,SRC3,VOL15<CR>
    #Z01PWRON,SRC3,VOL20<CR>
    #Z01PWRON,SRC3,VOL25<CR>
    #Z01PWRON,SRC3,VOL30<CR>
    #Z01PWRON,SRC3,VOL35<CR>
    #Z01PWRON,SRC3,VOL40<CR>
    #Z01PWRON,SRC3,VOL45<CR>
    #Z01PWRON,SRC3,VOL55<CR>

    and so on.

    If I only receive the data every so often then it works fine IE can press the knob very quickly which changes the source, which in turn sends the data out but my application can keep up with this, it only seems to be when the volume is changed.

    Below is my code showing the DataRecieved part. Is there some sort of buffer I should be using?
    Code:
        '-------------------------------------------
        ' Event handler for the DataReceived
        '-------------------------------------------
        Private Sub DataReceived( _
           ByVal sender As Object, _
           ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
           Handles SerialPort.DataReceived
            Sleep(50)
            
            txtVol.Invoke(New  _
                          myDelegate1(AddressOf updateTextBox), _
                          New Object() {})
        End Sub
        '------------------------------------------------------------------------
        ' Delegate and subroutine to update the Volume Status Text Box control
        '------------------------------------------------------------------------
        Public Delegate Sub myDelegate1()
        Public Sub updateTextBox()
            Dim data As String = SerialPort.ReadExisting 'this is the data string you received from the controller
            Dim zone As String = String.Empty
            Dim status As String = String.Empty
            Dim source As String = String.Empty
            Dim volume As String = String.Empty
            'Split the data to an array
            Dim parts() As String = data.Split(","c)
            'Now work the data array to extract each piece of data
            For Each part As String In parts
                If part.StartsWith("#") Then
                    zone = part.Substring(2, 2)
                    status = part.Substring(7)
                ElseIf part.StartsWith("SRC") Then
                    source = part.Substring(3)
                ElseIf part.StartsWith("VOL") Then
                    volume = part.Substring(3)
                End If
            Next
            'MessageBox.Show(String.Format("Zone: {0}, Status: {1}, Source: {2}, Volume: {3}", zone, status, source, volume))
            With txtZone
                .Font = New Font("Garamond", 24.0!, FontStyle.Bold)
                .SelectionColor = Color.Red
                .AppendText(zone)
                .ScrollToCaret()
            End With
    
            With txtStatus
                .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
                .SelectionColor = Color.Red
                .AppendText(status)
                .ScrollToCaret()
            End With
    
            With txtVol
                .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
                .SelectionColor = Color.Red
                .AppendText(volume)
                .ScrollToCaret()
            End With
    
            With txtSource
                .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
                .SelectionColor = Color.Red
                .AppendText(source)
                .ScrollToCaret()
            End With
    
        End Sub
    I did read on a post on another forum about the application running faster than the serial port so that not all data is getting received.

    Am I correct in using the
    Code:
    Serialport.readexisting
    function?

    Any help is much appreciated. And yes I am very new to programming.

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Serial Data - How to make sure the correct data is received?

    You won't lose any information if the data has been correctly interpreted by the serial port. The problem is likely in your reception event.

    The issue is that the event may give you a single character or 1000. You have to determine when you have received enough characters to interpret the protocols message. Put it another way, the data received event does not match your protocol messages.

    For example: what does your program do if the message received is:

    #Z01PWRON,SRC3,VOL05<CR>#Z01PWRON,S

    Or:

    #Z01PWR

    So, yes, you'll have to put a buffer in somewhere.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Re: Serial Data - How to make sure the correct data is received?

    I have tried a basic way to fix this but it still does return funny results when the vol knob is turned fast, I added the following
    Code:
    DataLength = Len(data)
    
            If DataLength = "21" Then
    It is still not that good. Is there a "proper" way for using a buffer with serial data?

  4. #4
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Serial Data - How to make sure the correct data is received?

    that's quite true. That'll give you the same results: what do you do when your data length isn't 21?

    There are a hundred ways of creating a buffer. Personally, I always use a byte circular buffer (because that's what a serial port is) which can be used for any byte-based serial transmission protocol (tcp/ip, for example). You'd use read and write pointers to that array to write to and read from that buffer.

    An alternative is to simply store your bytes in a string builder and keep putting bytes there until you have enough, then raise an event or something indicating that a string needs interpreting, subsequently resetting the stringbuilder.

    This is a good time to create a 'wrapper' for the serial port to hide all the grubby mess of the serial port.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Re: Serial Data - How to make sure the correct data is received?

    Thanks for that, I will do some more searching on the topics you have suggested, there is a lot to learn.

    I tried two different things for else,. I tried leaving it blank, but obviously if it was not true then my software wouldn't be up to date, then I also tried putting a 500ms sleep delay, then sending a status query (*Z01STATUS<CR>) and running the whole code again but not ideal, obviously I am still in the very early learning stages.

    Thanks again, much appreciated.

    Edit: If you do know of any links to more info on the byte circular buffer or building a string with the bytes or the wrapper that you talked about, feel free to post. :-)

  6. #6
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Serial Data - How to make sure the correct data is received?

    what is this project you're working on (if i may ask).
    it sounds pretty neat. is it a home audio controller (like speakers in different rooms of the house?

    would love to learn this kind of stuff (ie: using pc to control things like audio, lighting etc)

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Re: Serial Data - How to make sure the correct data is received?

    Yip making an interface for HAI HIfi multi-room audio controller, it is an 8 zone (room), 6 source controller. We also have a 4x4 HDMI matrix switch that has a serial port so eventually the software would ideally be universally. But I am just taking baby steps. This is just a hobby for me, for my own satisfaction.

    There is lots of hardware out there that control lights and automation etc that you could write software for.

  8. #8
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: Serial Data - How to make sure the correct data is received?

    Consider "#" as a start char and "CR" as a stop char. After every event fill up a buffer. Check after every event if it contains a stop char and a start char. If so take that data and process it. Clear the buffer and set it ready to fill up again.

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
  •  



Click Here to Expand Forum to Full Width