📄 rs232.txt
字号:
' 版权所有 (C) Microsoft Corporation。保留所有权利。
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading
Public Class RS232
' 声明必需的类变量及其初始值。
Private mhRS As Integer = -1 ' Com 端口的句柄
Private miPort As Integer = 1 ' 默认值为 COM1
Private miTimeout As Integer = 70 ' 超时设置,以 ms 为单位
Private miBaudRate As Integer = 9600
Private meParity As DataParity = 0
Private meStopBit As DataStopBit = 0
Private miDataBit As Integer = 8
Private miBufferSize As Integer = 512 ' 缓冲区大小默认值为 512 字节
Private mabtRxBuf As Byte() ' 接收缓冲区
Private meMode As Mode ' 类工作模式
Private mbWaitOnRead As Boolean
Private mbWaitOnWrite As Boolean
Private mbWriteErr As Boolean
Private muOverlapped As OVERLAPPED
Private muOverlappedW As OVERLAPPED
Private muOverlappedE As OVERLAPPED
Private mabtTmpTxBuf As Byte() ' Async Tx 使用的临时缓冲区
Private moThreadTx As Thread
Private moThreadRx As Thread
Private miTmpBytes2Read As Integer
Private meMask As EventMasks
#Region "枚举"
' 此枚举提供数据奇偶校验值。
Public Enum DataParity
Parity_None = 0
Pariti_Odd
Parity_Even
Parity_Mark
End Enum
' 此枚举提供数据停止位数值。
' 它被设置为从一开始,因此枚举值
' 与实际值匹配。
Public Enum DataStopBit
StopBit_1 = 1
StopBit_2
End Enum
' 此枚举包含用于清除各缓冲区的值。
Private Enum PurgeBuffers
RXAbort = &H2
RXClear = &H8
TxAbort = &H1
TxClear = &H4
End Enum
' 此枚举为发送到 Comm 端口的行提供值
Private Enum Lines
SetRts = 3
ClearRts = 4
SetDtr = 5
ClearDtr = 6
ResetDev = 7 ' 可能时重置设备
SetBreak = 8 ' 设置设备分行符。
ClearBreak = 9 ' 清除设备分行符。
End Enum
' 此枚举为调制解调器状态提供值,因为
' 我们将主要与调制解调器进行通信。
' 注意,Flags() 属性设置为允许对值进行
' 按位组合。
<Flags()> Public Enum ModemStatusBits
ClearToSendOn = &H10
DataSetReadyOn = &H20
RingIndicatorOn = &H40
CarrierDetect = &H80
End Enum
' 此枚举为工作模式提供值
Public Enum Mode
NonOverlapped
Overlapped
End Enum
' 此枚举为使用的 Comm 掩码提供值。
' 注意,Flags() 属性设置为允许对值进行
' 按位组合。
<Flags()> Public Enum EventMasks
RxChar = &H1
RXFlag = &H2
TxBufferEmpty = &H4
ClearToSend = &H8
DataSetReady = &H10
ReceiveLine = &H20
Break = &H40
StatusError = &H80
Ring = &H100
End Enum
#End Region
#Region "结构"
' 这是对 Windows API 的调用使用的 DCB 结构。
<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure DCB
Public DCBlength As Integer
Public BaudRate As Integer
Public Bits1 As Integer
Public wReserved As Int16
Public XonLim As Int16
Public XoffLim As Int16
Public ByteSize As Byte
Public Parity As Byte
Public StopBits As Byte
Public XonChar As Byte
Public XoffChar As Byte
Public ErrorChar As Byte
Public EofChar As Byte
Public EvtChar As Byte
Public wReserved2 As Int16
End Structure
' 这是对 Windows API 的调用使用的 CommTimeOuts 结构。
<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure COMMTIMEOUTS
Public ReadIntervalTimeout As Integer
Public ReadTotalTimeoutMultiplier As Integer
Public ReadTotalTimeoutConstant As Integer
Public WriteTotalTimeoutMultiplier As Integer
Public WriteTotalTimeoutConstant As Integer
End Structure
' 这是对 Windows API 的调用使用的 CommConfig 结构。
<StructLayout(LayoutKind.Sequential, Pack:=1)> Private Structure COMMCONFIG
Public dwSize As Integer
Public wVersion As Int16
Public wReserved As Int16
Public dcbx As DCB
Public dwProviderSubType As Integer
Public dwProviderOffset As Integer
Public dwProviderSize As Integer
Public wcProviderData As Byte
End Structure
' 这是对 Windows API 的调用使用的 OverLapped 结构。
<StructLayout(LayoutKind.Sequential, Pack:=1)> Public Structure OVERLAPPED
Public Internal As Integer
Public InternalHigh As Integer
Public Offset As Integer
Public OffsetHigh As Integer
Public hEvent As Integer
End Structure
#End Region
#Region "异常"
' 此类定义一个自定义信道异常。此异常在
' 引发 NACK 时引发。
Public Class CIOChannelException : Inherits ApplicationException
Sub New(ByVal Message As String)
MyBase.New(Message)
End Sub
Sub New(ByVal Message As String, ByVal InnerException As Exception)
MyBase.New(Message, InnerException)
End Sub
End Class
' 此类定义一个自定义超时异常。
Public Class IOTimeoutException : Inherits CIOChannelException
Sub New(ByVal Message As String)
MyBase.New(Message)
End Sub
Sub New(ByVal Message As String, ByVal InnerException As Exception)
MyBase.New(Message, InnerException)
End Sub
End Class
#End Region
#Region "事件"
' 此事件允许程序使用此类对 Comm 端口事件
' 做出响应。
Public Event DataReceived(ByVal Source As RS232, ByVal DataBuffer() As Byte)
Public Event TxCompleted(ByVal Source As RS232)
Public Event CommEvent(ByVal Source As RS232, ByVal Mask As EventMasks)
#End Region
#Region "常量"
' 这些常量用于使代码更清楚。
Private Const PURGE_RXABORT As Integer = &H2
Private Const PURGE_RXCLEAR As Integer = &H8
Private Const PURGE_TXABORT As Integer = &H1
Private Const PURGE_TXCLEAR As Integer = &H4
Private Const GENERIC_READ As Integer = &H80000000
Private Const GENERIC_WRITE As Integer = &H40000000
Private Const OPEN_EXISTING As Integer = 3
Private Const INVALID_HANDLE_VALUE As Integer = -1
Private Const IO_BUFFER_SIZE As Integer = 1024
Private Const FILE_FLAG_OVERLAPPED As Integer = &H40000000
Private Const ERROR_IO_PENDING As Integer = 997
Private Const WAIT_OBJECT_0 As Integer = 0
Private Const ERROR_IO_INCOMPLETE As Integer = 996
Private Const WAIT_TIMEOUT As Integer = &H102&
Private Const INFINITE As Integer = &HFFFFFFFF
#End Region
#Region "属性"
' 此属性获取或设置 BaudRate
Public Property BaudRate() As Integer
Get
Return miBaudRate
End Get
Set(ByVal Value As Integer)
miBaudRate = Value
End Set
End Property
' 此属性获取或设置 BufferSize
Public Property BufferSize() As Integer
Get
Return miBufferSize
End Get
Set(ByVal Value As Integer)
miBufferSize = Value
End Set
End Property
' 此属性获取或设置 DataBit。
Public Property DataBit() As Integer
Get
Return miDataBit
End Get
Set(ByVal Value As Integer)
miDataBit = Value
End Set
End Property
' 此只写属性获取或重置 DTR 行。
Public WriteOnly Property Dtr() As Boolean
Set(ByVal Value As Boolean)
If Not mhRS = -1 Then
If Value Then
EscapeCommFunction(mhRS, Lines.SetDtr)
Else
EscapeCommFunction(mhRS, Lines.ClearDtr)
End If
End If
End Set
End Property
' 此只读属性返回表示进入 Comm 端口的输入的
' 字节数组。
Overridable ReadOnly Property InputStream() As Byte()
Get
Return mabtRxBuf
End Get
End Property
' 此只读属性返回表示进入 Comm 端口的数据的
' 字符串。
Overridable ReadOnly Property InputStreamString() As String
Get
Dim oEncoder As New System.Text.ASCIIEncoding()
Return oEncoder.GetString(Me.InputStream)
End Get
End Property
' 此属性返回 Comm 端口的打开状态。
ReadOnly Property IsOpen() As Boolean
Get
Return CBool(mhRS <> -1)
End Get
End Property
' 此只读属性返回调制解调器的状态。
Public ReadOnly Property ModemStatus() As ModemStatusBits
Get
If mhRS = -1 Then
Throw New ApplicationException("Please initialize and open " + _
"port before using this method")
Else
' 检索调制解调器状态
Dim lpModemStatus As Integer
If Not GetCommModemStatus(mhRS, lpModemStatus) Then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -