How to read serial port control line states with .net
I'开发者_开发技巧m working on a project where I need to trigger an event when a switch is closed. It is my understanding that it is possible to use the control lines on a serial port and read whether the switch is closed or not. I've found a schematic of how to connect the switches to the pins, but I have not been able to find any example code on how you can read the state of the switches.
The schematic is located here: http://irtel.uni-mannheim.de/pxlab/doc/manual/Devices.html
Anyone have any ideas how you could use vb.net or C# to read the state of the switch on one of these control lines?
A pin changed event handler might help
Dim WithEvents sp As New IO.Ports.SerialPort
Private Sub sp_PinChanged(sender As Object, _
e As System.IO.Ports.SerialPinChangedEventArgs) Handles sp.PinChanged
'look at e.EventType or check states
Select Case True
Case sp.CDHolding
Case sp.CtsHolding
Case sp.DsrHolding
Case sp.RtsEnable
Case e.EventType = IO.Ports.SerialPinChange.Ring
End Select
End Sub
This assumes that the port is open.
edit: To detect ring you have to use e.EventType.
This should help you get started: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx. There are good serial tutorials there too.
精彩评论