本人在工作中经常要编程控制仪器,所以就要经常会用到串口通信,GPIB通信,网线接口通讯,和PCI总线通讯方面的知识。自己在刚开始学习摸索时走了很多弯路,所以今天把自己写的一个串口通信类放在这里,我知道,在.net中实现串口通讯已经非常简单了,只需把控件放在窗体上,然后进行相关设置就可以进行基本的读写操作了。我只是稍微进行封装了一下,写在自己常用的类库里,方便调用使用。希望对大家小有帮助!话不多说,看内容:
#Region "SerialPort NI Control" '串口控制 ''' <summary> ''' 初始化串口 ''' </summary> ''' <param name="PortName">串口名称</param> ''' <param name="BaudRate">波特率</param> ''' <param name="DataBitsLength">数据位的长度</param> ''' <param name="StopBits">停止位</param> ''' <param name="Handshake">握手协议</param> ''' <param name="Parity">优先级</param> ''' <param name="ReadTimeOut">连接超时时间</param> ''' <remarks>初始化串口</remarks> Public Sub SerialPortInit(ByVal PortName As String,ByVal BaudRate As Integer,ByVal DataBitsLength As Integer,ByVal StopBits As StopBits,ByVal Handshake As Handshake,ByVal Parity As Parity,Optional ByVal ReadTimeOut As Integer = 1000) SerialPort = New SerialPort SerialPort.PortName = PortName '串口名称 SerialPort.BaudRate = BaudRate '波特率 SerialPort.DataBits = DataBitsLength '数据位的长度 SerialPort.StopBits = StopBits '停止位 SerialPort.Handshake = Handshake '握手协议 SerialPort.Parity = Parity '优先级 SerialPort.ReadTimeout = ReadTimeOut End Sub ''' <summary> ''' 载入串口名称 ''' </summary> ''' <param name="ComBoxSerialPortName">串口容器</param> ''' <remarks>载入串口名称</remarks> Public Sub SerialPortLoadSerialPortPortName(ByVal ComBoxSerialPortName As ComboBox) Try ComBoxSerialPortName.Items.Clear() For Each PortName As String In IO.Ports.SerialPort.GetPortNames ComBoxSerialPortName.Items.Add(PortName) Next ComBoxSerialPortName.SelectedIndex = 0 Catch ex As Exception Throw New Exception("仪器串口与电脑串口为连接通:" & ex.Message) End Try End Sub ''' <summary> ''' 打开串口 ''' </summary> ''' <remarks>打开串口</remarks> Public Sub SerialPortOpenPort() Try If Not SerialPort.IsOpen = True Then SerialPort.Open() Else MsgBox("通讯端口打开错误或者通讯端口未打开",MsgBoxStyle.Information,"系统提示") End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub ''' <summary> ''' 关闭串口 ''' </summary> ''' <remarks>关闭串口</remarks> Public Sub SerialPortClosePort() Try If SerialPort Is Nothing OrElse Not SerialPort.IsOpen Then MsgBox("通讯端口还未打开","系统提示") Exit Sub Else SerialPort.Close() SerialPort = Nothing End If Catch ex As Exception MsgBox(ex.ToString) End Try End Sub ''' <summary> ''' 从缓冲区中读取制定的字节 ''' </summary> ''' <param name="Buffer">缓存字节</param> ''' <param name="Offset">偏移字节数量</param> ''' <param name="Count">读取数量</param> ''' <returns>返回从缓冲区中读取制定的字节</returns> ''' <remarks>从缓冲区中读取制定的字节</remarks> Public Overloads Function SerialPortRead(ByVal Buffer() As Byte,ByVal Offset As Integer,ByVal Count As Integer) As Integer Return SerialPort.Read(Buffer,Offset,Count) End Function ''' <summary> ''' 从缓冲区中读取制定的字符 ''' </summary> ''' <param name="Buffer">缓存字节数</param> ''' <param name="Offset">偏移量</param> ''' <param name="Count">读取字节个数</param> ''' <returns>返回从缓冲区中读取制定的字符</returns> ''' <remarks>从缓冲区中读取制定的字符</remarks> Public Overloads Function SerialPortRead(ByVal Buffer() As Char,Count) End Function ''' <summary> ''' 从缓冲区中异步读取一个字节 ''' </summary> ''' <returns>返回从缓冲区中异步读取一个字节</returns> ''' <remarks>从缓冲区中异步读取一个字节</remarks> Public Overloads Function SerialPortReadByte() As Integer Return SerialPort.ReadByte() End Function ''' <summary> ''' 从缓冲区中异步读取一个字符 ''' </summary> ''' <returns>返回从缓冲区中异步读取一个字符</returns> ''' <remarks>从缓冲区中异步读取一个字符</remarks> Public Overloads Function SerialPortReadChar() As Integer Return SerialPort.ReadChar() End Function ''' <summary> ''' 读取缓冲区中立即可用的字符 ''' </summary> ''' <returns>返回读取缓冲区中立即可用的字符</returns> ''' <remarks>读取缓冲区中立即可用的字符</remarks> Public Overloads Function SerialPortReadExisting() As String Return SerialPort.ReadExisting End Function ''' <summary> ''' 读取输入缓冲区NewLine之前的内容 ''' </summary> ''' <returns>返回读取输入缓冲区NewLine之前的内容</returns> ''' <remarks>读取输入缓冲区NewLine之前的内容</remarks> Public Overloads Function SerialPortReadLine() As String Return SerialPort.ReadLine End Function ''' <summary> ''' 写入命令字符串 ''' </summary> ''' <param name="SerialPortmandStr">命令内容</param> ''' <remarks>写入命令字符串</remarks> Public Overloads Sub SerialPortWrite(ByVal SerialPortmandStr As String) SerialPort.Write(SerialPortmandStr) End Sub ''' <summary> ''' 写入指定数量的命令字节到缓存 ''' </summary> ''' <param name="Buffer">缓存字节</param> ''' <param name="Offset">字节偏移量</param> ''' <param name="Count">写入字节个数</param> ''' <remarks>写入指定数量的命令字节到缓存</remarks> Public Overloads Sub SerialPortWrite(ByVal Buffer() As Byte,ByVal Count As Integer) SerialPort.Write(Buffer,Count) End Sub ''' <summary> ''' 写入指定数量的命令字符到缓存 ''' </summary> ''' <param name="Buffer">缓存字符</param> ''' <param name="Offset">字节偏移量</param> ''' <param name="Count">写入字符个数</param> ''' <remarks>写入指定数量的命令字符到缓存</remarks> Public Overloads Sub SerialPortWrite(ByVal Buffer() As Char,Count) End Sub #End Region