1、VisualStudio2005 中 serialPort 控件访问串口实例点评:这个例子包括了这个控件几乎所有的操作,非常有参考价值.serialPort是在.net framework2.0 中才有的东西,感觉这个东西和 MSCOMM 很相似.这里给出的例子是基于 和 C-SHAPE的,相应的可以在 Cshape 和 c+中使用,基本上都是一样的.VisualStudio2005 中 serialPort 控件访问串口实例转载Imports System Imports System.IO.Ports Imports System.Threading Public Class PortC
2、hat Shared _continue As BooleanShared _serialPort As SerialPort Public Shared Sub Main() Dim name As StringDim message As StringDim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase Dim readThread As Thread = New Thread(AddressOf Read) Create a new SerialPort object with default setting
3、s._serialPort = New SerialPort() Allow the user to set the appropriate properties._serialPort.PortName = SetPortName(_serialPort.PortName) _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate) _serialPort.Parity = SetPortParity(_serialPort.Parity) _serialPort.DataBits = SetPortDataBits(_seria
4、lPort.DataBits) _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits) _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake) Set the read/write timeouts_serialPort.ReadTimeout = 500 _serialPort.WriteTimeout = 500 _serialPort.Open() _continue = TruereadThread.Start() Console.Write(“Na
5、me: “) name = Console.ReadLine() Console.WriteLine(“Type QUIT to exit“) While (_continue) message = Console.ReadLine() If sComparer.Equals(“quit“, message) Then_continue = FalseElse_serialPort.WriteLine( _ String.Format(“: 1“, name, message) End Ifend whilereadThread.Join() _serialPort.Close() End S
6、ubPublic Shared Sub Read() While (_continue) TryDim message As String = _serialPort.ReadLine() Console.WriteLine(message) Catch ex As TimeoutException Do nothingEnd TryEnd WhileEnd SubPublic Shared Function SetPortName(ByVal defaultPortName AsString) As StringDim newPortName As StringConsole.WriteLi
7、ne(“Available Ports:“) Dim s As StringFor Each s In SerialPort.GetPortNames() Console.WriteLine(“ 0“, s) Next s Console.Write(“COM port(0): “, defaultPortName) newPortName = Console.ReadLine() If newPortName = “ ThennewPortName = defaultPortName End IfReturn newPortName End FunctionPublic Shared Fun
8、ction SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As IntegerDim newBaudRate As StringConsole.Write(“Baud Rate(0): “, defaultPortBaudRate) newBaudRate = Console.ReadLine() If newBaudRate = “ ThennewBaudRate = defaultPortBaudRate.ToString() End IfReturn Integer.Parse(newBaudRate) End Functio
9、nPublic Shared Function SetPortParity(ByVal defaultPortParity As Parity) As Parity Dim newParity As StringConsole.WriteLine(“Available Parity options:“) Dim s As StringFor Each s In Enum.GetNames(GetType(Parity) Console.WriteLine(“ 0“, s) Next s Console.Write(“Parity(0):“, defaultPortParity.ToString
10、()newparity = Console.ReadLine() If newparity = “ Thennewparity = defaultPortParity.ToString() End IfReturn CType(Enum.Parse(GetType(Parity), newParity), Parity) End FunctionPublic Shared Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As IntegerDim newDataBits As StringConsole.Write(
11、“Data Bits(0): “, defaultPortDataBits) newDataBits = Console.ReadLine() If newDataBits = “ ThennewDataBits = defaultPortDataBits.ToString() End IfReturn Integer.Parse(newDataBits) End FunctionPublic Shared Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits Dim newStopBits As
12、 StringConsole.WriteLine(“Available Stop Bits options:“) Dim s As StringFor Each s In Enum.GetNames(GetType(StopBits) Console.WriteLine(“ 0“, s) Next s Console.Write(“Stop Bits(0):“, defaultPortStopBits.ToString() newStopBits = Console.ReadLine() If newStopBits = “ ThennewStopBits = defaultPortStopB
13、its.ToString() End IfReturn CType(Enum.Parse(GetType(StopBits), newStopBits), StopBits) End FunctionPublic Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake Dim newHandshake As StringConsole.WriteLine(“Available Handshake options:“) Dim s As StringFor Each s In E
14、num.GetNames(GetType(Handshake) Console.WriteLine(“ 0“, s) Next s Console.Write(“Stop Bits(0):“, defaultPortHandshake.ToString() newHandshake = Console.ReadLine() If newHandshake = “ ThennewHandshake = defaultPortHandshake.ToString() End IfReturn CType(Enum.Parse(GetType(Handshake), newHandshake), Handshake) End Function End Class