K
Kite
Hello, everyone!
I write a program with serial port, error happend--The I/O operation has been aborted because of either a thread exit or an application request. I have no idea with it.
This is my source code.
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.IO
Imports System.IO.Ports
Public Class Form1
Dim WithEvents port As New IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
port.ReceivedBytesThreshold = 1
If port.IsOpen = True Then
port.Close()
End If
port.Open()
lblError.Text = "COM1 port connected!"
lblError.ForeColor = Color.Green
TextBox1.Text = "0.000kg"
Catch ex As Exception
lblError.Text = ex.Message
End Try
End Sub
Private Sub port_DataReceived1(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
Try
TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
Catch ex As System.IO.IOException
MsgBox(ex.Message)
End Try
End Sub
'------------------------------------------------------
' Delegate and subroutine to update the Textbox control
'------------------------------------------------------
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
Dim tmpStr As String
Dim pos As Integer
tmpStr = Trim(port.ReadLine)
pos = InStr(tmpStr, "GS")
If pos > 0 Then
tmpStr = Trim(Mid(tmpStr, pos + 2))
If tmpStr <> Trim(TextBox1.Text) Then
With TextBox1
.Text = tmpStr
'.ScrollToCaret()
End With
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
port.Close()
Me.Close()
End Sub
End Class
I write a program with serial port, error happend--The I/O operation has been aborted because of either a thread exit or an application request. I have no idea with it.
This is my source code.
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.IO
Imports System.IO.Ports
Public Class Form1
Dim WithEvents port As New IO.Ports.SerialPort("COM1", 9600, Parity.None, 8, StopBits.One)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
port.ReceivedBytesThreshold = 1
If port.IsOpen = True Then
port.Close()
End If
port.Open()
lblError.Text = "COM1 port connected!"
lblError.ForeColor = Color.Green
TextBox1.Text = "0.000kg"
Catch ex As Exception
lblError.Text = ex.Message
End Try
End Sub
Private Sub port_DataReceived1(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
Try
TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
Catch ex As System.IO.IOException
MsgBox(ex.Message)
End Try
End Sub
'------------------------------------------------------
' Delegate and subroutine to update the Textbox control
'------------------------------------------------------
Public Delegate Sub myDelegate()
Public Sub updateTextBox()
Dim tmpStr As String
Dim pos As Integer
tmpStr = Trim(port.ReadLine)
pos = InStr(tmpStr, "GS")
If pos > 0 Then
tmpStr = Trim(Mid(tmpStr, pos + 2))
If tmpStr <> Trim(TextBox1.Text) Then
With TextBox1
.Text = tmpStr
'.ScrollToCaret()
End With
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
port.Close()
Me.Close()
End Sub
End Class