Results 1 to 6 of 6

Thread: RESOLVED: Tutorials for Serial ports / threading???

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Resolved RESOLVED: Tutorials for Serial ports / threading???

    I'm looking for any tutorials on serial ports. I'm working on a project to read a serial port, and this is my current method:
    Code:
    SerialPort1.Close()
            SerialPort1.BaudRate = 9600
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.DataBits = 8
            SerialPort1.StopBits = IO.Ports.StopBits.One
            SerialPort1.Open()
            SerialPort1.ReadExisting()
            Dim input As String = _SerialPort1.ReadExisting
            TextBox1.Text = input
            TextBox1.Refresh()
    I get no build errors or other errors, but on running it, I get no text in textbox1.

    I do have a serial port control on form1.

    To test, I have connected two computers and sent serial data from hyperterminal to hyperterminal successfully. This also confirms that baud/parity/stop/data are set correctly.

    Clearly, there's something missing, but the books I have don't talk about serial, so I'm hoping that there is a tutorial out there somewhere than makes it clear as mud.

    I'd also love to see a tutorial on threading in VB.net, if anyone has one lying around.

    Thanks in advance.
    Last edited by Og_ofthejungle; Aug 1st, 2011 at 12:59 PM. Reason: To Resolve

  2. #2
    Addicted Member
    Join Date
    Mar 2007
    Posts
    163

    Re: Tutorials for Serial ports / threading???

    Have you tried using the "DataReceived" event of your serial port?
    Read existing will happen once there in your code but if there is nothing there then it will read nothing...

    Have a look here its the data received event page on msdn that will give you a point to start.

    As for threading I would suggest again starting at the msdn page for the thread class and then take it from there.

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

    Re: Tutorials for Serial ports / threading???

    This should get you started. Not tested so there might be errors.

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button1.Click
            'open
            Button1.Enabled = False
            Try
                If SerialPort1.IsOpen Then SerialPort1.Close()
                With SerialPort1
                    .PortName = "COM1"
                    .DtrEnable = True
                    .BaudRate = 9600
                    .Parity = IO.Ports.Parity.None
                    .DataBits = 8
                    .StopBits = IO.Ports.StopBits.One
                    .Open()
                End With
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
                Button1.Enabled = True
            End Try
        End Sub
    
        Private Sub SerialPort1_DataReceived(sender As Object, _
                                             e As System.IO.Ports.SerialDataReceivedEventArgs) _
                                         Handles SerialPort1.DataReceived
            Dim input As String = SerialPort1.ReadExisting
            UpdateTextBox(input)
        End Sub
    
        Delegate Sub UpdateTextBoxDel(s As String)
    
        Private Sub UpdateTextBox(s As String)
            If Me.InvokeRequired Then 'check to see if running on UI thread
                'no, do so now
                'http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
                Dim foo As New UpdateTextBoxDel(AddressOf UpdateTextBox)
                Me.Invoke(foo, s)
                Exit Sub
            Else
                RichTextBox1.AppendText(s) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            End If
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, _
                                  e As System.EventArgs) Handles Button2.Click
            'send
            If SerialPort1.IsOpen AndAlso TextBox2.Text <> "" Then
                SerialPort1.Write(TextBox2.Text)
            End If
        End Sub
    End Class
    Last edited by dbasnett; Jul 29th, 2011 at 03:13 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

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    34

    Re: Tutorials for Serial ports / threading???

    @ Stavris, Thanks for replying. I have not tried the DataReceived event. I was concerned that this might force me to use a handshake, and the data stream I wish to test may or may not use a handshake. I will read the MSDN pages you linked to (hopefully better than most MS "Help" information).

    @ DBasnett, this looks good, and I'll try it. A question if you don't mind: I see that you've separated the two functions, setting port data and looking for data rec'd. In my application, I'm actually testing for the port data (i.e. at which setting do I get legible information from the remote system). Aside from keeping the code better organized, is there a reason not to have these two functions in the same code block?

    Also, I'm a little stumped on the TextBox update code block, but I'll look up the commands and hopefully get a clue on that.

    Thank you both, and I'll let you know how it works.

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

    Re: Tutorials for Serial ports / threading???

    Quote Originally Posted by Og_ofthejungle View Post
    ...@ DBasnett, this looks good, and I'll try it. A question if you don't mind: I see that you've separated the two functions, setting port data and looking for data rec'd. In my application, I'm actually testing for the port data (i.e. at which setting do I get legible information from the remote system). Aside from keeping the code better organized, is there a reason not to have these two functions in the same code block?

    Also, I'm a little stumped on the TextBox update code block, but I'll look up the commands and hopefully get a clue on that.

    Thank you both, and I'll let you know how it works.
    I made a change to the original post to use a RichTextBox for the output.

    When the SerialPort1.DataReceived fires data is available.
    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
    Member
    Join Date
    Feb 2009
    Posts
    34

    Resolved Re: Tutorials for Serial ports / threading???

    Thanks, DBasnett, it looks like this points me in the right direction. I still have some fine tuning to do and some debugging ahead, but I am passing data to and from, at least.

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