VisualStudio2005中serialPort控件访问串口实例.doc

上传人:sk****8 文档编号:3502583 上传时间:2019-06-01 格式:DOC 页数:5 大小:169.50KB
下载 相关 举报
VisualStudio2005中serialPort控件访问串口实例.doc_第1页
第1页 / 共5页
VisualStudio2005中serialPort控件访问串口实例.doc_第2页
第2页 / 共5页
VisualStudio2005中serialPort控件访问串口实例.doc_第3页
第3页 / 共5页
VisualStudio2005中serialPort控件访问串口实例.doc_第4页
第4页 / 共5页
VisualStudio2005中serialPort控件访问串口实例.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。