Display the serial port string in a label or text box.

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 7314 Views - Last Post: 20 December 2011 - 05:02 PM Rate Topic: -----

#16 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 10:08 PM

Sure man! :)
Hope I helped a bit!
Was This Post Helpful? 1
  • +
  • -

#17 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 10:10 PM

View PostjimmyBo, on 20 December 2011 - 12:08 AM, said:

Sure man! :)
Hope I helped a bit!

You did I've already learned a couple new things like I should find a book on this thats not so dumbed down and not over my head but just in the middle.
Was This Post Helpful? 0
  • +
  • -

#18 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 19 December 2011 - 10:12 PM

Glad! :)
If I helped please click the little green plus to acknowledge my efforts!
Was This Post Helpful? 0
  • +
  • -

#19 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 20 December 2011 - 11:06 AM

View PostjimmyBo, on 20 December 2011 - 12:12 AM, said:

Glad! :)
If I helped please click the little green plus to acknowledge my efforts!

Ok so i googled something diferant today and I actually found a somewhat tutorial from microsoft but it wasnt completely helpful. So i put this code in:
Function serialRead()

        Dim str As String = ""
        Do
            Dim Incoming As String = SerialPort1.ReadLine()
            If Incoming Is Nothing Then
                Exit Do
            Else
                str &= Incoming & vbCrLf
            End If
        Loop
        TextBox1.Text = str
        Return str
    End Function

But it still freezes when i press the button. so what should i do?
Was This Post Helpful? 0
  • +
  • -

#20 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 641
  • Joined: 03-February 10

Re: Display the serial port string in a label or text box.

Posted 20 December 2011 - 11:17 AM

It doesn't sound like it is freezing -- it is processing. Use a background worker and then you can show the progress by updating a label.
Was This Post Helpful? 1
  • +
  • -

#21 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 20 December 2011 - 11:52 AM

View Postdemausdauth, on 20 December 2011 - 01:17 PM, said:

It doesn't sound like it is freezing -- it is processing. Use a background worker and then you can show the progress by updating a label.

Ok now how would i set that up to run as soon as the form loads would i put it in the initialization or put it after the port is opened?

Ok i now get this error "The I/O operation has been aborted because of either a thread exit or an application request."
when i go to debug then cancel the background worker.

This is the whole code i have now:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Imports System.ComponentModel

Public Class Form1

    Dim incomingByte As String = ""

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Label1.BackColor = Color.Red
        Label1.Text = "The Port Is Closed!"
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub Main()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        With SerialPort1
            .Close()
            .PortName = "COM3"
            .BaudRate = 9600
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .DtrEnable = True
            .RtsEnable = True
            .ReceivedBytesThreshold = 1
        End With
        TextBox1.Text = SerialPort1.ReadExisting()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SerialPort1.Open()
        Label1.BackColor = Color.ForestGreen
        Label1.Text = "The Port Is Open!"
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        SerialPort1.Write("A")
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        SerialPort1.Write("B")
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        SerialPort1.Write("C")
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        SerialPort1.Write("D")
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        SerialPort1.Write("E")
    End Sub

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        SerialPort1.Write("F")
    End Sub

    Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
        SerialPort1.Write("G")
    End Sub

    Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
        SerialPort1.Write("H")
    End Sub

    Function serialRead()

        Dim str As String = ""
        Do
            Dim Incoming As String = SerialPort1.ReadLine()
            If Incoming Is Nothing Then
                Exit Do
            Else
                str &= Incoming & vbCrLf
            End If
        Loop
        TextBox1.Text = str
        Return str
    End Function

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Dim str As String = ""
        Do
            Dim Incoming As String = SerialPort1.ReadLine()
            If Incoming Is Nothing Then
                Exit Do
            Else
                str &= Incoming & vbCrLf
            End If
        Loop
        TextBox1.Text = str
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        TextBox1.Text = SerialPort1.ReadLine()

    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        TextBox1.Text = SerialPort1.ReadLine()
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted

    End Sub

    Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
        BackgroundWorker1.CancelAsync()
        SerialPort1.Close()
        Label1.Text = "The Port Is Closed!"
        Label1.BackColor = Color.Red
    End Sub

End Class

I've also adde pictures of a couple problems
Attached Image

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#22 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Display the serial port string in a label or text box.

Posted 20 December 2011 - 01:52 PM

It's because you cancelled the background worker while it was still running and doing a task. It is just telling you that an error occurred because a thread was cancelled - which it did when you cancelled the BackgroundWorker.
Was This Post Helpful? 1
  • +
  • -

#23 hightekrednek2396  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-December 11

Re: Display the serial port string in a label or text box.

Posted 20 December 2011 - 05:02 PM

View PostjimmyBo, on 20 December 2011 - 03:52 PM, said:

It's because you cancelled the background worker while it was still running and doing a task. It is just telling you that an error occurred because a thread was cancelled - which it did when you cancelled the BackgroundWorker.

Ok thats what I was thinking but I wanted to make sure but I'm still having trouble getting it to receive a string or even an integer if it could just receive a single digit I could then just decode it later but I'm not sure bout that.

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Dim incomingByte As String = ""

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        Label1.BackColor = Color.Red
        Label1.Text = "The Port Is Closed!"
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub Main()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        With SerialPort1
            .Close()
            .PortName = "COM3"
            .BaudRate = 9600
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .DtrEnable = True
            .RtsEnable = True
            .ReceivedBytesThreshold = 1
        End With
        TextBox1.Text = SerialPort1.ReadExisting()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        SerialPort1.Open()
        Label1.BackColor = Color.ForestGreen
        Label1.Text = "The Port Is Open!"
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        SerialPort1.Write("A")
        serialRead()
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        SerialPort1.Write("B")
        serialRead()
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        SerialPort1.Write("C")
        serialRead()
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        SerialPort1.Write("D")
        serialRead()
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        SerialPort1.Write("E")
        serialRead()
    End Sub

    Private Sub Button7_Click(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        SerialPort1.Write("F")
        serialRead()
    End Sub

    Private Sub Button8_Click(sender As System.Object, e As System.EventArgs) Handles Button8.Click
        SerialPort1.Write("G")
        serialRead()
    End Sub

    Private Sub Button9_Click(sender As System.Object, e As System.EventArgs) Handles Button9.Click
        SerialPort1.Write("H")
        serialRead()
    End Sub

    Public Sub serialRead()
        TextBox1.Text = SerialPort1.ReadChar()
    End Sub

    Private Sub Button10_Click(sender As System.Object, e As System.EventArgs) Handles Button10.Click
        SerialPort1.Close()
        Label1.Text = "The Port Is Closed!"
        Label1.BackColor = Color.Red
    End Sub
End Class



ok I decided to just use the ascii codes for now and maybe later ill use the whole strings but for now there was the finished code.

and ill add the picture of my form:
Attached Image
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2