|
-
Sep 22nd, 2011, 11:08 AM
#1
Fanatic Member
Re: Listening to the Serial Port - Need help
It would be simpler if you simply dropped a SerialPort control from the toolbox onto the design surface. When you do that, the event handler is built for you. If you want to do this in code, the correct declaration would be
Code:
Private WithEvents SerialPort As New System.IO.Ports.SerialPort
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With SerialPort
.PortName = "Com2" 'for example
.BaudRate = 9600
.RtsEnable = True
.Open()
'etc.
End With
End Sub
Private Sub SerialPort_DataReceived(sender As Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
Dim DataFromPort As String = SerialPort.ReadExisting
Debug.Print(DataFromPort)
End Sub
Now, this is pretty simple-minded, and is only a starting point. If you go to my website, www.hardandsoftware.net and download Enhanced SerialPort (this is free), I have example code that is more complete. The example code can be used with the built-in SerialPort object, as long as you do not call any of the Enhanced SerialPort extensions -- however, you might as well use Enhanced SerialPort, since it is just a wrapper for SerialPort, and does not remove any functions.
Dick
Richard Grier, Consultant, Hard & Software
Microsoft MVP (Visual Basic)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|