📄 rs232.vb
字号:
Dim lpModemStatus As Integer
If Not GetCommModemStatus(mhRS, lpModemStatus) Then
Throw New ApplicationException("Unable to get modem status")
Else
Return CType(lpModemStatus, ModemStatusBits)
End If
End If
End Get
End Property
' This property gets or sets the Parity
Public Property Parity() As DataParity
Get
Return meParity
End Get
Set(ByVal Value As DataParity)
meParity = Value
End Set
End Property
' This property gets or sets the Port
Public Property Port() As Integer
Get
Return miPort
End Get
Set(ByVal Value As Integer)
miPort = Value
End Set
End Property
' This write-only property sets or resets the RTS line.
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
' This property gets or sets the StopBit
Public Property StopBit() As DataStopBit
Get
Return meStopBit
End Get
Set(ByVal Value As DataStopBit)
meStopBit = Value
End Set
End Property
' This property gets or sets the Timeout
Public Overridable Property Timeout() As Integer
Get
Return miTimeout
End Get
Set(ByVal Value As Integer)
miTimeout = CInt(IIf(Value = 0, 500, Value))
' If Port is open updates it on the fly
pSetTimeout()
End Set
End Property
' This property gets or sets the working mode to overlapped
' or non-overlapped.
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"
' The following functions are the required Win32 functions needed to
' make communication with the Comm Port possible.
<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 "Methods"
' This subroutine invokes a thread to perform an asynchronous read.
' This routine should not be called directly, but is used
' by the class.
Public Sub _R()
Dim iRet As Integer = Read(miTmpBytes2Read)
End Sub
' This subroutine invokes a thread to perform an asynchronous write.
' This routine should not be called directly, but is used
' by the class.
Public Sub _W()
Write(mabtTmpTxBuf)
End Sub
' This subroutine uses another thread to read from the Comm Port. It
' raises RxCompleted when done. It reads an integer.
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
' This subroutine uses another thread to write to the Comm Port. It
' raises TxCompleted when done. It writes an array of bytes.
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
' This subroutine uses another thread to write to the Comm Port. It
' raises TxCompleted when done. It writes a string.
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
' This function takes the ModemStatusBits and returns a boolean value
' signifying whether the Modem is active.
Public Function CheckLineStatus(ByVal Line As ModemStatusBits) As Boolean
Return Convert.ToBoolean(ModemStatus And Line)
End Function
' This subroutine clears the input buffer.
Public Sub ClearInputBuffer()
If Not mhRS = -1 Then
PurgeComm(mhRS, PURGE_RXCLEAR)
End If
End Sub
' This subroutine closes the Comm Port.
Public Sub Close()
If mhRS <> -1 Then
CloseHandle(mhRS)
mhRS = -1
End If
End Sub
' This subroutine opens and initializes the Comm Port
Public Overloads Sub Open()
' Get Dcb block,Update with current data
Dim uDcb As DCB, iRc As Integer
' Set working mode
Dim iMode As Integer = Convert.ToInt32(IIf(meMode = Mode.Overlapped, _
FILE_FLAG_OVERLAPPED, 0))
' Initializes Com Port
If miPort > 0 Then
Try
' Creates a COM Port stream handle
mhRS = CreateFile("COM" & miPort.ToString, _
GENERIC_READ Or GENERIC_WRITE, 0, 0, _
OPEN_EXISTING, iMode, 0)
If mhRS <> -1 Then
' Clear all comunication errors
Dim lpErrCode As Integer
iRc = ClearCommError(mhRS, lpErrCode, 0&)
' Clears I/O buffers
iRc = PurgeComm(mhRS, PurgeBuffers.RXClear Or _
PurgeBuffers.TxClear)
' Gets COM Settings
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -