Serial Comms with Visual Basic
realolman
Posts: 65
First let me apologize for coming back· (again ) with questions re: Visual Basic on a propeller forum... I am pretty close to what I want to be able to do with the propeller, and the serial interface is important to me, and I feel like I am so close...
I·have a form with which I can communicate with the propeller, I can write to the propeller like I want and I can read from the propeller if I use a button and· SerialPort1.Read Existing.
If I try to use the DataReceived event to put the data in a text box , I get a cross thread error.
I got the following from an old thread in an·MSDN·forum and I think I see what the guy is doing... sort of.
He created another Sub:·· DisplayText ... I think to put the received data into a text box avoiding the cross thread error.
I have both the data received event handler and the Display Text event here.· I have bolded the line that I don't understand.· I get a "type SetTextCallback is not defined" error.
I don't know what to do with this, and it was not explained in the forum.
I think if I could get this I could work the rest out.
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As······································ System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
· Dim strInData As String = SerialPort1.ReadExisting ' read the data
· DisplayText(strInData) ' use threadsafe way to write to StatusTextbox
End Sub
' This method demonstrates a pattern for making thread-safe TextBox Displays
Private Sub DisplayText(ByVal [noparse][[/noparse]text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.txtReceivedText.InvokeRequired Then
·· Dim d As New SetTextCallback(AddressOf DisplayText)·····
·· Me.Invoke(d, New Object() {[noparse][[/noparse]text]})
Else
·· Me.txtReceivedText.Text += [noparse][[/noparse]text]
·· End If
End Sub
thanks for your help (again )· and your patience.
I·have a form with which I can communicate with the propeller, I can write to the propeller like I want and I can read from the propeller if I use a button and· SerialPort1.Read Existing.
If I try to use the DataReceived event to put the data in a text box , I get a cross thread error.
I got the following from an old thread in an·MSDN·forum and I think I see what the guy is doing... sort of.
He created another Sub:·· DisplayText ... I think to put the received data into a text box avoiding the cross thread error.
I have both the data received event handler and the Display Text event here.· I have bolded the line that I don't understand.· I get a "type SetTextCallback is not defined" error.
I don't know what to do with this, and it was not explained in the forum.
I think if I could get this I could work the rest out.
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As······································ System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
· Dim strInData As String = SerialPort1.ReadExisting ' read the data
· DisplayText(strInData) ' use threadsafe way to write to StatusTextbox
End Sub
' This method demonstrates a pattern for making thread-safe TextBox Displays
Private Sub DisplayText(ByVal [noparse][[/noparse]text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.txtReceivedText.InvokeRequired Then
·· Dim d As New SetTextCallback(AddressOf DisplayText)·····
·· Me.Invoke(d, New Object() {[noparse][[/noparse]text]})
Else
·· Me.txtReceivedText.Text += [noparse][[/noparse]text]
·· End If
End Sub
thanks for your help (again )· and your patience.
Comments
As a note , in some circumstances where you are testing and want something quick and simple you can dispose of after testing adding the line CheckForIllegalCrossThreadCalls=False·in the Form load event will prevent the errors, just a temporary fix.
Jeff T.
MS have to use up the GB's of memory somehow !
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps (SixBladeProp)
· Prop Tools under Development or Completed (Index)
· Emulators (Micros eg Altair, and Terminals eg VT100) - index
· Search the Propeller forums (via Google)
My cruising website is: ·www.bluemagic.biz
You will find that it more efficient than vb6 MSComm controls. Once the initial curve is breached it becomes second nature.
Rgds,
John
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Necessity is the mother of invention'
Those who can, do.Those who can’t, teach.
Sub ReadSerialPort()
Dim BytesToRead As Integer ' get in batches
Dim i As Integer
Dim Character As String
Timer1.Enabled = False
Do
If SerialPort.BytesToRead = 0 Then Exit Do ' no more bytes
BytesToRead = SerialPort.BytesToRead
If BytesToRead > 2000 Then BytesToRead = 2000
SerialPort.Read(InPacket, 0, BytesToRead) ' read in a packet
For i = 1 To BytesToRead
Character = Strings.Chr(InPacket(i - 1))
Call DisplayCharacter(Character)
Next
Loop
Timer1.Enabled = True
End Sub
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/e36193cd-a708-42b3-86b7-adff82b19e5e/
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Necessity is the mother of invention'
Those who can, do.Those who can’t, teach.
Jeff T.
I would like to post a simple serial communication program written in Visual Basic Express, and a simple spin program to go along with it.
I got most all this stuff from others and I appreciate the help I got here.· My reason for posting this is kind of a pay it forward thing.
It's certainly not fancy, but it's simple enough that I think it might be useful to someone who is in the position I was in a few weeks ago ... trying to communicate with a prop using VB and not having much success.
It's in Word Pad because the attatchment manager would not let me upload the code from VB express.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Engineering