|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Simple Example of How to Implement SerialPort ClassHello all,
Does anyone have an example on how to implement the new SerialPort Class in VB.NET? I have been able to create the class, send (.WriteLine) to the port and Read from the port but cannot figure out how to use the Events to tell my program that data is available in the serial port buffer. I have searched the internet and MSDN and there doesn't appear to be any full example on how to do this simple task. Any help would be greatly appreciated! Ben Kim See
http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/vb05legacyhardware.asp Show quoteHide quote "Ben Kim" <bkim@NOSPAMemergitech.com> wrote in message news:egp2OVuEGHA.3608@TK2MSFTNGP10.phx.gbl... > Hello all, > > Does anyone have an example on how to implement the new SerialPort Class > in VB.NET? I have been able to create the class, send (.WriteLine) to the > port and Read from the port but cannot figure out how to use the Events to > tell my program that data is available in the serial port buffer. > > I have searched the internet and MSDN and there doesn't appear to be any > full example on how to do this simple task. > > Any help would be greatly appreciated! > > Ben Kim > Thank you Chuck. Exactly what I was looking for. However this has brought
up a different issue. Here is the code I created based on the example you gave. However I get an exception error whenever the remote port responds with text: "InvalidOperationException was unhandled", Cross-thread operation not valid: Control 'txtOutput' accessed from a thread other than the thread it was created on. Code: -------------------------------- Imports System.IO.Ports Public Class Form1 Dim WithEvents mySerialPort As SerialPort = New System.IO.Ports.SerialPort Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click With mySerialPort .PortName = "COM" & txtPortNum.Text .BaudRate = 57600 .Parity = Parity.None .DataBits = 8 .StopBits = StopBits.One End With mySerialPort.Open() End Sub Private Sub btnSendText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendText.Click mySerialPort.Write(txtSendText.Text) End Sub Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosePort.Click mySerialPort.Close() End Sub Private Sub mySerialPort_DataReceived(ByVal sender As Object, _ ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _ Handles mySerialPort.DataReceived txtOutput.Text = txtOutput.Text + mySerialPort.ReadExisting() 'Console.Write(mySerialPort.ReadExisting()) End Sub End Class -------------------------------- After reading the help about this specific error I am not sure I understand why mySerialPort is running on another thread since it is part of my form class. Any ideas? Ben Kim "Chuck Gantz" <cgantz2***@yahoo.com> wrote in message news:dDTvf.386$sa4.134@trnddc07...Show quoteHide quote > See > http://msdn.microsoft.com/vbasic/default.aspx?pull=/library/en-us/dnvs05/html/vb05legacyhardware.asp > > > "Ben Kim" <bkim@NOSPAMemergitech.com> wrote in message > news:egp2OVuEGHA.3608@TK2MSFTNGP10.phx.gbl... >> Hello all, >> >> Does anyone have an example on how to implement the new SerialPort Class >> in VB.NET? I have been able to create the class, send (.WriteLine) to >> the port and Read from the port but cannot figure out how to use the >> Events to tell my program that data is available in the serial port >> buffer. >> >> I have searched the internet and MSDN and there doesn't appear to be any >> full example on how to do this simple task. >> >> Any help would be greatly appreciated! >> >> Ben Kim >> > > OK got it to work. Seems like a lot of work for something that should be
inherently thread safe already (Window form controls). You have to create a delegate that enables asynchronous calls to the window control in this case txtOutput. Here is the code for all interested in how the new SerialPort object class works. Imports System Imports System.ComponentModel Imports System.Threading Imports System.IO.Ports Public Class Form1 Dim WithEvents mySerialPort As SerialPort = New System.IO.Ports.SerialPort ' This delegate enables asynchronous calls for setting ' the text property on a TextBox control. Delegate Sub SetTextCallback(ByVal [text] As String) Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click With mySerialPort .PortName = "COM" & txtPortNum.Text .BaudRate = 57600 .Parity = Parity.None .DataBits = 8 .StopBits = StopBits.One End With mySerialPort.Open() End Sub Private Sub btnSendText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendText.Click mySerialPort.Write(txtSendText.Text) End Sub Private Sub btnClosePort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClosePort.Click mySerialPort.Close() End Sub Private Sub mySerialPort_DataReceived(ByVal sender As Object, _ ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _ Handles mySerialPort.DataReceived SetText(mySerialPort.ReadExisting()) 'Console.Write(mySerialPort.ReadExisting()) End Sub Private Sub SetText(ByVal [text] As String) ' InvokeRequired required compares the thread ID of the ' calling thread to the thread ID of the creating thread. ' If these threads are different, it returns true. If Me.txtOutput.InvokeRequired Then Dim d As New SetTextCallback(AddressOf SetText) Me.Invoke(d, New Object() {[text]}) Else Me.txtOutput.Text &= [text] End If End Sub End Class
SetWindowsHookEx and VB.NET 2005
A Framework for Datadriven Forms for VB Dot Net XML Comments File Generated Byte Array, Datagrid CreateObject( )... :( Problem with setting focus math wrong? DataSet Problems ISO Date Functions I was looking for the "Immediate" window like in Classic VB but can't find it |
|||||||||||||||||||||||