收藏 分销(赏)

SerialPort控件的使用.doc

上传人:xrp****65 文档编号:7679197 上传时间:2025-01-12 格式:DOC 页数:38 大小:118.50KB 下载积分:10 金币
下载 相关 举报
SerialPort控件的使用.doc_第1页
第1页 / 共38页
SerialPort控件的使用.doc_第2页
第2页 / 共38页


点击查看更多>>
资源描述
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref8/html/T_System_IO_Ports_SerialPort_Members.htm//串口控件 一.概述 在Visual Studio 6.0中编写串口通讯程序,一般都使用Microsoft Communication Control(简称MSComm)的通讯控件,只要通 过对此控件的属性和事件进行相应编程操作,就可以轻松地实现串口通讯。但在Microsoft.Net技术广泛应用的今天,Visual Studio.Net没有将此控件加入控件库,所以人们采用了许多方法在Visual Studio.Net来编写串口通讯程序:第一种方法是通过采用Visual Studio 6.0中原来的MSComm控件这是最简单的,最方便的方法,但需要注册;第二种方法是采用微软在.NET推出了一个串口控件,基于.NET的P/Invoke调用方法实现;第三种方法是自己用API写串口通信,虽然难度高,但可以方便实现自己想要的各种功能。 现在微软推出了最新版本的Visual Studio 2005开发工具,可以不再采用第三方控件的方法来设计串口通讯程序。NET Framework 2.0 类库包含了SerialPort 类,方便地实现了所需要串口通讯的多种功能,为了使MSComm编程方法快速转换到以SerialPort类为核心的串口通讯的设计方法,本文着重讨论了Visual Studio 6.0的MSComm控件和SerialPort 类设计方法的异同点。   二.SerialPort常用属性、方法和事件 1.命名空间 System.IO.Ports命名空间包含了控制串口重要的SerialPort类,该类提供了同步 I/O 和事件驱动的 I/O、对管脚和中断状态的访问以及对串行驱动程序属性的访问,所以在程序代码起始位置需加入Using System.IO.Ports。 2.串口的通讯参数 串口通讯最常用的参数就是通讯端口号及通讯格式(波特率、数据位、停止位和校验位),在MSComm中相关的属性是CommPort和Settings。SerialPort类与MSComm有一些区别: a.通讯端口号 [PortName]属性获取或设置通信端口,包括但不限于所有可用的 COM 端口,请注意该属性返回类型为String,不是Mscomm.CommPort的short类型。通常情况下,PortName正常返回的值为COM1、COM2……,SerialPort类最大支持的端口数突破了CommPort控件中CommPort属性不能超过16的限止,大大方便了用户串口设备的配置。 b. 通讯格式 SerialPort类对分别用[BaudRate]、[Parity]、[DataBits]、[StopBits]属性设置通讯格式中的波特率、数据位、停止位和校验位,其中[Parity]和[StopBits]分别是枚举类型Parity、StopBits,Parity类型中枚举了Odd(奇)、Even(偶)、Mark、None、Space,Parity枚举了None、One、OnePointFive、Two。 SerialPort类提供了七个重载的构造函数,既可以对已经实例化的SerialPort对象设置上述相关属性的值,也可以使用指定的端口名称、波特率和奇偶校验位数据位和停止位直接初始化 SerialPort 类的新实例。 3.串口的打开和关闭 SerialPort类没有采用MSComm.PortOpen=True/False设置属性值打开关闭串口,相应的是调用类的Open()和Close()方法。 4. 数据的发送和读取 Serial类调用重载的Write和WriteLine方法发送数据,其中WriteLine可发送字符串并在字符串末尾加入换行符,读取串口缓冲区的方法有许多,其中除了ReadExisting和ReadTo,其余的方法都是同步调用,线程被阻塞直到缓冲区有相应的数据或大于ReadTimeOut属性设定的时间值后,引发ReadExisting异常。 5.DataReceived事件 该事件类似于MSComm控件中的OnComm事件,DataReceived事件在接收到了[ReceivedBytesThreshold]设置的字符个数或接收到了文件结束字符并将其放入了输入缓冲区时被触发。其中[ReceivedBytesThreshold]相当于MSComm控件的[Rthreshold]属性,该事件的用法与MsComm控件的OnComm事件在CommEvent为comEvSend和comEvEof时是一致的。     三.SerialPort的使用 对于熟悉MSComm控件的程序设计者,SerialPort类是相当容易上手的。在进行串口通讯时,一般的流程是设置通讯端口号及波特率、数据位、停止位和校验位,再打开端口连接,发送数据,接收数据,最后关闭端口连接这样几个步骤。 数据接收的设计方法在这里比较重要,采用轮询的方法比较浪费时间,在Visual Basic中的延时方法中一般会调用API并用DOEvents方法来处理,但程序不易控制,建议采用DataReceived事件触发的方法,合理的设置ReceivedBytesThreshold的值,若接收的是定长的数据,则将ReceivedBytesThreshold设为接收数据的长度,若接收数据的结尾是固定的字符或字符串则可采用ReadTo的方法或在DataReceived事件中判断接收的字符是否满足条件。 SerialPort类读取数据的许多方法是同步阻塞调用,尽量避免在主线程中调用,可以使用异步处理或线程间处理调用这些读取数据的方法。 由于DataReceived事件在辅线程被引发,当收到完整的一条数据,返回主线程处理或在窗体上显示时,请注意跨线程的处理,C#可采用控件异步委托的方法Control.BeginInvoke及同步委托的方法Invoke。   四.结束语 在.NET平台下熟练使用SerialPort 类,可以很好地开发出串口通讯类程序,对于过去使用MSComm控件设计了一些通讯程序,也可以将MSComm控件替换为SerialPort类,当然为了避免对以前的项目做大的改动,可以使用SerialPort类设计一些与MSComm控件具有相同接口的类,在今后工业控制中,SerialPort类将广泛地应用于串口通讯程序的设计中,发挥着与MSComm控件一样的作用。 C# SerialPort运行方式 SerialPort中串口数据的读取与写入有较大的不同。由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取。一、线程实时读串口;二、事件触发方式实现。 由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。 另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下: SerialPort spSend;  //spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据 SerialPort spReceive;  //spReceive接受数据 TextBox txtSend;  //发送区 TextBox txtReceive;  //接受区 Button btnSend;  //数据发送按钮 delegate void HandleInterfaceUpdateDelegate(string text);  //委托,此为重点 HandleInterfaceUpdateDelegate interfaceUpdateHandle; public void InitClient()  //窗体控件已在初始化 {   interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox);  //实例化委托对象   spSend.Open();  //SerialPort对象在程序结束前必须关闭,在此说明   spReceive.DataReceived += Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);   spReceive.ReceivedBytesThreshold = 1;   spReceive.Open(); } public void btnSend_Click(object sender,EventArgs e) {   spSend.WriteLine(txtSend.Text); } public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e) {   byte[] readBuffer = new byte[spReceive.ReadBufferSize];   spReceive.Read(readBuffer, 0, readBuffer.Length);   this.Invoke(interfaceUpdateHandle, new string[] { Encoding.Unicode.GetString(readBuffer) }); } private void UpdateTextBox(string text) {   txtReceive.Text = text; } 点评:这个例子包括了这个控件几乎所有的操作,非常有参考价值.serialPort是在.net framework2.0中才有的东西,感觉这个东西和MSCOMM很相似.这里给出的例子是基于和CSHAPE 的,相应的可以在Cshape和c++中使用,基本上都是一样的. Imports System Imports System.IO.Ports Imports System.Threading   Public Class PortChat      Shared _continue As Boolean       Shared _serialPort As SerialPort       Public Shared 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()          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(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(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(ByVal defaultPortParity As Parity) As Parity          Dim newParity As String            Console.WriteLine("Available Parity options:")          Dim s As String           For Each s In [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].Parse(GetType(Parity), newParity), Parity)      End Function        Public Shared 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(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].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].Parse(GetType(StopBits), newStopBits), StopBits)      End Function        Public Shared 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].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].Parse(GetType(Handshake), newHandshake), Handshake)      End Function End Class using System; using System.IO.Ports; using System.Threading;   public class PortChat {      static bool _continue;      static SerialPort _serialPort;       public static void Main()      {          string name;          string message;          StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;          Thread readThread = new Thread(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 (stringComparer.Equals("quit", message))              {                  _continue = false;              }              else               {                  _serialPort.WriteLine(                      String.Format("<{0}>: {1}", name, message) );              }          }           readThread.Join();          _serialPort.Close();      }       public static void Read()      {          while (_continue)          {              try               {                  string message = _serialPort.ReadLine();                  Console.WriteLine(message);              }              catch (TimeoutException) { }          }      }       public static string SetPortName(string defaultPortName)      {          string portName;           Console.WriteLine("Available Ports:");          foreach (string s in SerialPort.GetPortNames())          {              Console.WriteLine(" {0}", s);          }           Console.Write("COM port({0}): ", defaultPortName);          portName = Console.ReadLine();           if (portName == "")          {              portName = defaultPortName;          }          return portName;      }       public static int SetPortBaudRate(int defaultPortBaudRate)      {          string baudRate;           Console.Write("Baud Rate({0}): ", defaultPortBaudRate);          baudRate = Console.ReadLine();           if (baudRate == "")          {              baudRate = defaultPortBaudRate.ToString();          }           return int.Parse(baudRate);      }       public static Parity SetPortParity(Parity defaultPortParity)      {          string parity;           Console.WriteLine("Available Parity options:");          foreach (string s in Enum.GetNames(typeof(Parity)))          {              Console.WriteLine(" {0}", s);          }           Console.Write("Parity({0}):", defaultPortParity.ToString());          parity = Console.ReadLine();           if (parity == "")          {              parity = defaultPortParity.ToString();          }           return (Parity)Enum.Parse(typeof(Parity), parity);      }       public static int SetPortDataBits(int defaultPortDataBits)      {          string dataBits;           Console.Write("Data Bits({0}): ", defaultPortDataBits);          dataBits = Console.ReadLine();           if (dataBits == "")          {              dataBits = defaultPortDataBits.ToString();          }           return int.Parse(dataBits);      }            public static StopBits SetPortStopBits(StopBits defaultPortStopBits)      {          string stopBits;           Console.WriteLine("Available Stop Bits options:");          foreach (string s in Enum.GetNames(typeof(StopBits)))          {              Console.WriteLine(" {0}", s);          }           Console.Write("Stop Bits({0}):", defaultPortStopBits.ToString());          stopBits = Console.ReadLine();           if (stopBits == "")          {              stopBits = defaultPortStopBits.ToString();          }           return (StopBits)Enum.Parse(typeof(StopBits), stopBits);      }       public static Handshake SetPortHandshake(Handshake defaultPortHandshake)      {          string handshake;           Console.WriteLine("Available Handshake options:");          foreach (string s in Enum.GetNames(typeof(Handshake)))
展开阅读全文

开通  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 

客服