#Region "formFile" Public Class Form1 Dim sp As New serPort_cls Public Sub processRcvdData() 'do something useful here, msgbox() just for testing MsgBox("data rcvd") End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim h As Integer If Me.IsHandleCreated Then h = Handle ' yes, handle does exist, here it is End If 'Me.CreateHandle() sp.openCom() End Sub End Class #End Region #Region "classFile" Imports System.IO.Ports Public Class serPort_cls Private WithEvents serPort As New SerialPort Delegate Sub UIrcvdDataHandler() Public Sub openCom() serPort.PortName = "COM1" serPort.Open() serPort.BaudRate = 38400 serPort.ReceivedBytesThreshold = 10 ' set bigger than largest message expected, EOF will trigger DataReceived event End Sub Private Sub Receiver(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serPort.DataReceived ' invoke handler in UI thread Form1.BeginInvoke(New UIrcvdDataHandler(AddressOf Form1.processRcvdData)) 'Form1.beginInvoke(New MethodInvoker(AddressOf Form1.processRcvdData)) ' either of the invoke methods above produces the following error '"Invoke or BeginInvoke cannot be called on a control until the window handle has been created." End Sub End Class #End Region
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)