📄 rs232.txt
字号:
Throw New ApplicationException("Unable to get modem status")
Else
Return CType(lpModemStatus, ModemStatusBits)
End If
End If
End Get
End Property
' 此属性获取或设置奇偶校验
Public Property Parity() As DataParity
Get
Return meParity
End Get
Set(ByVal Value As DataParity)
meParity = Value
End Set
End Property
' 此属性获取或设置端口
Public Property Port() As Integer
Get
Return miPort
End Get
Set(ByVal Value As Integer)
miPort = Value
End Set
End Property
' 此只写属性获取或重置 RTS 行。
Public WriteOnly Property Rts() As Boolean
Set(ByVal Value As Boolean)
If Not mhRS = -1 Then
If Value Then
EscapeCommFunction(mhRS, Lines.SetRts)
Else
EscapeCommFunction(mhRS, Lines.ClearRts)
End If
End If
End Set
End Property
' 此属性获取或设置停止位
Public Property StopBit() As DataStopBit
Get
Return meStopBit
End Get
Set(ByVal Value As DataStopBit)
meStopBit = Value
End Set
End Property
' 此属性获取或设置超时值
Public Overridable Property Timeout() As Integer
Get
Return miTimeout
End Get
Set(ByVal Value As Integer)
miTimeout = CInt(IIf(Value = 0, 500, Value))
' 如果端口是打开的,则对其进行动态更新
pSetTimeout()
End Set
End Property
' 此属性获取工作模式或将其设置为重叠模式
' 或非重叠模式。
Public Property WorkingMode() As Mode
Get
Return meMode
End Get
Set(ByVal Value As Mode)
meMode = Value
End Set
End Property
#End Region
#Region "Win32API"
' 下面的函数是要使与 Comm 端口的通信成为可能所必需的 Win32
' 函数。
<DllImport("kernel32.dll")> Private Shared Function BuildCommDCB( _
ByVal lpDef As String, ByRef lpDCB As DCB) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function ClearCommError( _
ByVal hFile As Integer, ByVal lpErrors As Integer, _
ByVal l As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function CloseHandle( _
ByVal hObject As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function CreateEvent( _
ByVal lpEventAttributes As Integer, ByVal bManualReset As Integer, _
ByVal bInitialState As Integer, _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpName As String) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function CreateFile( _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As Integer, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function EscapeCommFunction( _
ByVal hFile As Integer, ByVal ifunc As Long) As Boolean
End Function
<DllImport("kernel32.dll")> Private Shared Function FormatMessage( _
ByVal dwFlags As Integer, ByVal lpSource As Integer, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpBuffer As String, _
ByVal nSize As Integer, ByVal Arguments As Integer) As Integer
End Function
Private Declare Function FormatMessage Lib "kernel32" Alias _
"FormatMessageA" (ByVal dwFlags As Integer, ByVal lpSource As Integer, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, _
ByVal lpBuffer As StringBuilder, ByVal nSize As Integer, _
ByVal Arguments As Integer) As Integer
<DllImport("kernel32.dll")> Public Shared Function GetCommModemStatus( _
ByVal hFile As Integer, ByRef lpModemStatus As Integer) As Boolean
End Function
<DllImport("kernel32.dll")> Private Shared Function GetCommState( _
ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function GetCommTimeouts( _
ByVal hFile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function GetLastError() As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function GetOverlappedResult( _
ByVal hFile As Integer, ByRef lpOverlapped As OVERLAPPED, _
ByRef lpNumberOfBytesTransferred As Integer, _
ByVal bWait As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function PurgeComm( _
ByVal hFile As Integer, ByVal dwFlags As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function ReadFile( _
ByVal hFile As Integer, ByVal Buffer As Byte(), _
ByVal nNumberOfBytesToRead As Integer, _
ByRef lpNumberOfBytesRead As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function SetCommTimeouts( _
ByVal hFile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function SetCommState( _
ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function SetupComm( _
ByVal hFile As Integer, ByVal dwInQueue As Integer, _
ByVal dwOutQueue As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function SetCommMask( _
ByVal hFile As Integer, ByVal lpEvtMask As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function WaitCommEvent( _
ByVal hFile As Integer, ByRef Mask As EventMasks, _
ByRef lpOverlap As OVERLAPPED) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function WaitForSingleObject( _
ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer
End Function
<DllImport("kernel32.dll")> Private Shared Function WriteFile( _
ByVal hFile As Integer, ByVal Buffer As Byte(), _
ByVal nNumberOfBytesToWrite As Integer, _
ByRef lpNumberOfBytesWritten As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
#End Region
#Region "方法"
' 此子例程调用一个线程以执行异步读取。
' 此例程不应直接调用,而是由该类
' 使用。
Public Sub R()
Dim iRet As Integer = Read(miTmpBytes2Read)
End Sub
' 此子例程调用一个线程以执行异步写入。
' 此例程不应直接调用,而是由该类
' 使用。
Public Sub W()
Write(mabtTmpTxBuf)
End Sub
' 此子例程使用另一个线程从 Comm 端口进行读取。它
' 在完成时引发 RxCompleted。它读取整数。
Public Overloads Sub AsyncRead(ByVal Bytes2Read As Integer)
If meMode <> Mode.Overlapped Then Throw New ApplicationException( _
"Async Methods allowed only when WorkingMode=Overlapped")
miTmpBytes2Read = Bytes2Read
moThreadTx = New Thread(AddressOf R)
moThreadTx.Start()
End Sub
' 此子例程使用另一个线程写入到 Comm 端口。它
' 在完成时引发 TxCompleted。它写入字节数组。
Public Overloads Sub AsyncWrite(ByVal Buffer() As Byte)
If meMode <> Mode.Overlapped Then Throw New ApplicationException( _
"Async Methods allowed only when WorkingMode=Overlapped")
If mbWaitOnWrite = True Then Throw New ApplicationException( _
"Unable to send message because of pending transmission.")
mabtTmpTxBuf = Buffer
moThreadTx = New Thread(AddressOf W)
moThreadTx.Start()
End Sub
' 此子例程使用另一个线程写入到 Comm 端口。它
' 在完成时引发 TxCompleted。它写入字符串。
Public Overloads Sub AsyncWrite(ByVal Buffer As String)
Dim oEncoder As New System.Text.ASCIIEncoding()
Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
Me.AsyncWrite(aByte)
End Sub
' 此函数采用 ModemStatusBits,并返回一个指示调制解调器是否
' 处于活动状态的布尔值。
Public Function CheckLineStatus(ByVal Line As ModemStatusBits) As Boolean
Return Convert.ToBoolean(ModemStatus And Line)
End Function
' 此子例程清除输入缓冲区。
Public Sub ClearInputBuffer()
If Not mhRS = -1 Then
PurgeComm(mhRS, PURGE_RXCLEAR)
End If
End Sub
' 此子例程关闭 Comm 端口。
Public Sub Close()
If mhRS <> -1 Then
CloseHandle(mhRS)
mhRS = -1
End If
End Sub
' 此子例程打开并初始化 Comm 端口
Public Overloads Sub Open()
' 获取 Dcb 块,使用当前数据进行更新
Dim uDcb As DCB, iRc As Integer
' 设置工作模式
Dim iMode As Integer = Convert.ToInt32(IIf(meMode = Mode.Overlapped, _
FILE_FLAG_OVERLAPPED, 0))
' 初始化 Com 端口
If miPort > 0 Then
Try
' 创建一个 COM 端口流句柄
mhRS = CreateFile("COM" & miPort.ToString, _
GENERIC_READ Or GENERIC_WRITE, 0, 0, _
OPEN_EXISTING, iMode, 0)
If mhRS <> -1 Then
' 清除所有通信错误
Dim lpErrCode As Integer
iRc = ClearCommError(mhRS, lpErrCode, 0&)
' 清除 I/O 缓冲区
iRc = PurgeComm(mhRS, PurgeBuffers.RXClear Or _
PurgeBuffers.TxClear)
' 获取 COM 设置
iRc = GetCommState(mhRS, uDcb)
' 更新 COM 设置
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -