|
-
Jun 28th, 2009, 12:58 AM
#1
Thread Starter
Member
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.
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
|