Hope I helped a bit!
22 Replies - 3134 Views - Last Post: 20 December 2011 - 05:02 PM
#16
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!
Hope I helped a bit!
#17
Re: Display the serial port string in a label or text box.
Posted 19 December 2011 - 10:10 PM
#18
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!
If I helped please click the little green plus to acknowledge my efforts!
#19
Re: Display the serial port string in a label or text box.
Posted 20 December 2011 - 11:06 AM
jimmyBo, on 20 December 2011 - 12:12 AM, said:
Glad! 
If I helped please click the little green plus to acknowledge my efforts!
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?
#20
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.
#21
Re: Display the serial port string in a label or text box.
Posted 20 December 2011 - 11:52 AM
demausdauth, 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(s)
#22
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.
#23
Re: Display the serial port string in a label or text box.
Posted 20 December 2011 - 05:02 PM
jimmyBo, 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:
|
|

New Topic/Question
Reply




MultiQuote



|