收藏 分销(赏)

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

上传人:pc****0 文档编号:7774061 上传时间:2025-01-16 格式:DOC 页数:5 大小:169.50KB 下载积分:10 金币
下载 相关 举报
VisualStudio2005中serialPort控件访问串口实例.doc_第1页
第1页 / 共5页
VisualStudio2005中serialPort控件访问串口实例.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
VisualStudio2005中serialPort控件访问串口实例 点评:这个例子包括了这个控件几乎所有的操作,非常有参考价值.serialPort是在.net framework2.0中才有的东西,感觉这个东西和MSCOMM很相似.这里给出的例子是基于和C-SHAPE 的,相应的可以在Cshape和c++中使用,基本上都是一样的. VisualStudio2005中serialPort控件访问串口实例[转载] Imports System  Imports System.IO.Ports  Imports System.Threading    Public Class PortChatClass PortChat       Shared _continue As Boolean       Shared _serialPort As SerialPort        Public Shared Sub Main()Sub Main()           Dim name As String           Dim message As String           Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase           Dim readThread As Thread = New Thread(AddressOf Read)            ' Create a new SerialPort object with default settings.           _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(_serialPort.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 = True           readThread.Start()            Console.Write("Name: ")           name = Console.ReadLine()            Console.WriteLine("Type QUIT to exit")            While (_continue)               message = Console.ReadLine()                If sComparer.Equals("quit", message) Then                   _continue = False               Else                   _serialPort.WriteLine( _                       String.Format("<{0}>: {1}", name, message))               End If           end while            readThread.Join()           _serialPort.Close()       End Sub        Public Shared Sub Read()Sub Read()           While (_continue)               Try                   Dim message As String = _serialPort.ReadLine()                   Console.WriteLine(message)               Catch ex As TimeoutException                   ' Do nothing               End Try           End While       End Sub        Public Shared Function SetPortName()Function SetPortName(ByVal defaultPortName As String) As String           Dim newPortName As String            Console.WriteLine("Available Ports:")           Dim s As String           For Each s In SerialPort.GetPortNames()               Console.WriteLine(" {0}", s)           Next s            Console.Write("COM port({0}): ", defaultPortName)           newPortName = Console.ReadLine()            If newPortName = "" Then               newPortName = defaultPortName           End If           Return newPortName       End Function        Public Shared Function SetPortBaudRate()Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer           Dim newBaudRate As String            Console.Write("Baud Rate({0}): ", defaultPortBaudRate)           newBaudRate = Console.ReadLine()            If newBaudRate = "" Then               newBaudRate = defaultPortBaudRate.ToString()           End If            Return Integer.Parse(newBaudRate)       End Function        Public Shared Function SetPortParity()Function SetPortParity(ByVal defaultPortParity As Parity) As Parity           Dim newParity As String            Console.WriteLine("Available Parity options:")           Dim s As String           For Each s In [Enum ]Enum].GetNames(GetType(Parity))               Console.WriteLine(" {0}", s)           Next s            Console.Write("Parity({0}):", defaultPortParity.ToString())           newparity = Console.ReadLine()            If newparity = "" Then               newparity = defaultPortParity.ToString()           End If            Return CType([Enum ]Enum].Parse(GetType(Parity), newParity), Parity)       End Function        Public Shared Function SetPortDataBits()Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer           Dim newDataBits As String            Console.Write("Data Bits({0}): ", defaultPortDataBits)           newDataBits = Console.ReadLine()            If newDataBits = "" Then               newDataBits = defaultPortDataBits.ToString()           End If            Return Integer.Parse(newDataBits)       End Function        Public Shared Function SetPortStopBits()Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits           Dim newStopBits As String            Console.WriteLine("Available Stop Bits options:")           Dim s As String           For Each s In [Enum ]Enum].GetNames(GetType(StopBits))               Console.WriteLine(" {0}", s)           Next s            Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString())           newStopBits = Console.ReadLine()            If newStopBits = "" Then               newStopBits = defaultPortStopBits.ToString()           End If            Return CType([Enum ]Enum].Parse(GetType(StopBits), newStopBits), StopBits)       End Function        Public Shared Function SetPortHandshake()Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake           Dim newHandshake As String            Console.WriteLine("Available Handshake options:")           Dim s As String           For Each s In [Enum ]Enum].GetNames(GetType(Handshake))               Console.WriteLine(" {0}", s)           Next s            Console.Write("Stop Bits({0}):", defaultPortHandshake.ToString())           newHandshake = Console.ReadLine()            If newHandshake = "" Then               newHandshake = defaultPortHandshake.ToString()           End If            Return CType([Enum ]Enum].Parse(GetType(Handshake), newHandshake), Handshake)       End Function  End Class
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服