Shop OBEX P1 Docs P2 Docs Learn Events
Serial Comms with Visual Basic — Parallax Forums

Serial Comms with Visual Basic

realolmanrealolman Posts: 65
edited 2009-02-06 16:32 in Propeller 1
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.

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-02-05 02:04
    Hi realolman, you are right about threads, when you added the DataReceived event to your program that created a second thread to your application. The way to call between threads is to use a Delegate, if you look at Chris Savages source for the RFID Reader in the completed projects forum it contains an example which will probably work for you. The three things you should search for are the declaration for mydelegate , the line where the delegate is TextBox1.Invoked and the sub which is the delegate updateTextBox().

    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.
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-02-05 08:44
    Now I know why so many have stuck with good old VB6 smile.gif

    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
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2009-02-05 09:56
    Cluso99,
    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.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2009-02-05 13:08
    I must confess I have not ever managed to get these trapping routines to work properly, either in vb6 nor in vb.net. Like you say, cross threads, or mysterious crashes after a few days of running code. The workaround that I've used in bot vb6 and vb.net is to check for inputs periodically with a timer. Eg once every 100 milliseconds. A few tricks there too - eg you have to turn the timer off while you check for inputs otherwise you get a recursive loop. Then keep reading the input till there are no more bytes. The code below is part of a terminal program and is called from timer1.

    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
  • ConeheadConehead Posts: 12
    edited 2009-02-05 13:46
    Try using the MSCOMM_oncomm event in VB6.··If you have to use VB.NET, I would suggest the Franson SerialNET component.
  • QuattroRS4QuattroRS4 Posts: 916
    edited 2009-02-05 14:15
    The following link discusses various aspects and methods ..

    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.
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2009-02-05 19:33
    Hi, Visual Basic is solid enough that I see more and more production lines being installed with VB as one of the major control components the latest being a 3 lane·eighty foot conveyor with 17 pick and place robots and a supervisory camera vision system. You have to see this to believe the speed. So if you need a Prop·to machine interface then VB has to be high on the list of consideration. There are also strong similarities between the syntax of Spin and VB for example Pub ReadTxt(stringptr) in Spin can be represented in VB with Public Sub ReadTxt(stringptr As String). I think the latest flavor of Visual Basic .net is the most user friendly and easiest to learn and a lot of this is due to available documentation. Again I see similarities , both Parallax and MS have great documentation and as with anything it is down to the user and how much time they are prepared to put in learning the subject. I started with Visual·Studio and PBasic a little over 2 years ago and though I still make mistakes I am pleased with what I am able to achieve , the small app I posted at this thread http://forums.parallax.com/showthread.php?p=780420 works great for me at 115200 baud without dropping a character (although I did find one flaw in my Spin code). Threads can get confusing but fortunately for the beginner VB .net has a background worker control which takes a lot of the thinking out of the problem. Aside from all of that I have had a Propstick sitting on a breadboard doing nothing for a very long time now and I have decide it is time to get familiar with this awsome chip , if anyone has any thoughts and ideas on using the Propstick and VB together I am always interested.

    Jeff T.
  • realolmanrealolman Posts: 65
    edited 2009-02-05 23:52
    Prop to machine interface is exactly what I have in mind..

    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 SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2009-02-06 16:32
    VB files can be zipped in order to post them. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Engineering
Sign In or Register to comment.