📄 comm.vb
字号:
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading
Public Class msComm
#Region "定义变量或常数"
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 PURGE_TXCLEAR As Integer = 4
Private Const PURGE_RXCLEAR As Integer = 8
Private Const STOPBITS_10 As Byte = 1
Private Const STOPBITS_15 As Byte = 2
Private Const STOPBITS_20 As Byte = 4
Private Const DATABITS_5 As Byte = 1
Private Const DATABITS_6 As Byte = 2
Private Const DATABITS_7 As Byte = 4
Private Const DATABITS_8 As Byte = 8
Private Const EVENPARITY As Byte = 2
Private Const MARKPARITY As Byte = 3
Private Const NOPARITY As Byte = 0
Private Const ODDPARITY As Byte = 1
Private Const SPACEPARITY As Byte = 4
Private Const CBR_110 As Integer = 110
Private Const CBR_300 As Integer = 300
Private Const CBR_600 As Integer = 600
Private Const CBR_1200 As Integer = 1200
Private Const CBR_2400 As Integer = 2400
Private Const CBR_4800 As Integer = 4800
Private Const CBR_9600 As Integer = 9600
Private Const CBR_14400 As Integer = 14400
Private Const CBR_19200 As Integer = 19200
Private Const CBR_38400 As Integer = 38400
Private Const CBR_56000 As Integer = 56000
Private Const CBR_57600 As Integer = 57600
Private Const CBR_115200 As Integer = 115200
Private Const CBR_128000 As Integer = 128000
Private Const CBR_256000 As Integer = 256000
Private mintHandle As Integer = INVALID_HANDLE_VALUE ' 串口句柄
Private mintPort As Integer = 1 ' 串口号
Private mbytParity As Byte = NOPARITY ' 奇偶校验位
Private mbytDataBits As Byte = DATABITS_8 ' 数据位
Private mbytStopBits As Byte = STOPBITS_10 ' 停止位
Private mintBaudRate As Integer = CBR_9600 ' 波特率
Private mintTXBufferSize As Integer = 512 ' 默认输出缓冲区大小
Private mintRXBufferSize As Integer = 512 ' 默认输入缓冲区大小
Private mintTimeout As Integer = 1000 ' 串口超时
Private mbytTxBuffer As Byte() ' 发送缓冲区
Private mbytRxBuffer As Byte() ' 接收缓冲区
Public ComData() As Byte
Public ComLen As Integer
Private meParity As DataParity = 0
Private meStopBit As DataStopBit = 0
Private miPort As Integer = 1 ' Default is COM1
Private miDataBit As Integer = 8
Private miBufferSize As Integer = 512 ' Buffers size default to 512 bytes
#End Region
#Region "Structures"
<StructLayout(LayoutKind.Sequential)> 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
' This is the DCB structure used by the calls to the Windows API.
<StructLayout(LayoutKind.Sequential)> Private Structure DCB
Public DCBlength As Integer ' Specifies the DCB structure length, in bytes.
Public BaudRate As Integer ' Specifies the baud rate at which the communication device operates.
Public bits1 As Integer ' Specifies if binary mode is enabled.
Public wReserved As Int16 ' Not used set to zero.
Public XonLim As Int16 ' Specifies the minimum number of bytes accepted in the input buffer before the XON character is sent.
Public XoffLim As Int16 ' Specifies the maximum number of bytes accepted in the input buffer before the XOFF character is sent.
Public ByteSize As Byte ' Specifies the number of bits in the bytes transmitted and received.
Public Parity As Byte ' Specifies the parity scheme to be used.
Public StopBits As Byte ' Specifies the number of stop bits to be used.
Public XonChar As Byte ' Specifies the value of the XON character for both transmission and reception.
Public XoffChar As Byte ' Specifies the value of the XOFF character for both transmission and reception.
Public ErrorChar As Byte ' Specifies the value of the character used to replace bytes received with a parity error.
Public EofChar As Byte ' Specifies the value of the character used to signal the end of data.
Public EvtChar As Byte ' Specifies the value of the character used to signal an event.
Public wReserved1 As Int16 ' Reserved do not use.
End Structure
<StructLayout(LayoutKind.Sequential)> Private Structure DCB1
Public DCBlength As Integer ' Specifies the DCB structure length, in bytes.
Public BaudRate As Integer ' Specifies the baud rate at which the communication device operates.
Public fBinary As Integer ' Specifies if binary mode is enabled.
Public fParity As Integer ' Specifies if parity checking is enabled.
Public fOutxCtsFlow As Integer ' Specifies if the CTS (clear-to-send) signal is monitored for output flow control.
Public fOutxDsrFlow As Integer ' Specifies if the DSR (data-set-ready) signal is monitored for output flow control.
Public fDtrControl As Integer ' Specifies the DTR (data-terminal-ready) flow control.
Public fDsrSensitivity As Integer ' Specifies if the communications driver is sensitive to the state of the DSR signal.
Public fTXContinueOnXoff As Integer ' Specifies if transmission stops when the input buffer is full and the driver has transmitted the XoffChar character.
Public fOutX As Integer ' Specifies if XON/XOFF flow control is used during transmission.
Public fInX As Integer ' Specifies if XON/XOFF flow control is used during reception.
Public fErrorChar As Integer ' Specifies if bytes received with parity errors are replaced with the character specified by the ErrorChar member.
Public fNull As Integer ' Specifies if null bytes are discarded.
Public fRtsControl As Integer ' Specifies the RTS (request-to-send) flow control.
Public fAbortOnError As Integer ' Specifies if read and write operations are terminated if an error occurs.
Public fDummy2 As Integer ' Reserved do not use.
Public wReserved As Int16 ' Not used set to zero.
Public XonLim As Int16 ' Specifies the minimum number of bytes accepted in the input buffer before the XON character is sent.
Public XoffLim As Int16 ' Specifies the maximum number of bytes accepted in the input buffer before the XOFF character is sent.
Public ByteSize As Byte ' Specifies the number of bits in the bytes transmitted and received.
Public Parity As Byte ' Specifies the parity scheme to be used.
Public StopBits As Byte ' Specifies the number of stop bits to be used.
Public XonChar As Char ' Specifies the value of the XON character for both transmission and reception.
Public XoffChar As Char ' Specifies the value of the XOFF character for both transmission and reception.
Public ErrorChar As Char ' Specifies the value of the character used to replace bytes received with a parity error.
Public EofChar As Char ' Specifies the value of the character used to signal the end of data.
Public EvtChar As Char ' Specifies the value of the character used to signal an event.
Public wReserved1 As Int16 ' Reserved do not use.
End Structure
<StructLayout(LayoutKind.Sequential)> Private Structure DCB2
Public DCBlength As Int32 ' Specifies the DCB structure length, in bytes.
Public BaudRate As Int32 ' Specifies the baud rate at which the communication device operates.
Public fBinary As Byte ' Specifies if binary mode is enabled.
Public fParity As Byte ' Specifies if parity checking is enabled.
Public fOutxCtsFlow As Byte ' Specifies if the CTS (clear-to-send) signal is monitored for output flow control.
Public fOutxDsrFlow As Byte ' Specifies if the DSR (data-set-ready) signal is monitored for output flow control.
Public fDtrControl As Byte ' Specifies the DTR (data-terminal-ready) flow control.
Public fDsrSensitivity As Byte ' Specifies if the communications driver is sensitive to the state of the DSR signal.
Public fTXContinueOnXoff As Byte ' Specifies if transmission stops when the input buffer is full and the driver has transmitted the XoffChar character.
Public fOutX As Byte ' Specifies if XON/XOFF flow control is used during transmission.
Public fInX As Byte ' Specifies if XON/XOFF flow control is used during reception.
Public fErrorChar As Byte ' Specifies if bytes received with parity errors are replaced with the character specified by the ErrorChar member.
Public fNull As Byte ' Specifies if null bytes are discarded.
Public fRtsControl As Byte ' Specifies the RTS (request-to-send) flow control.
Public fAbortOnError As Byte ' Specifies if read and write operations are terminated if an error occurs.
Public fDummy2 As Byte ' Reserved do not use.
Public wReserved As Byte ' Not used set to zero.
Public XonLim As Byte ' Specifies the minimum number of bytes accepted in the input buffer before the XON character is sent.
Public XoffLim As Byte ' Specifies the maximum number of bytes accepted in the input buffer before the XOFF character is sent.
Public ByteSize As Byte ' Specifies the number of bits in the bytes transmitted and received.
Public Parity As Byte ' Specifies the parity scheme to be used.
Public StopBits As Byte ' Specifies the number of stop bits to be used.
Public XonChar As Byte ' Specifies the value of the XON character for both transmission and reception.
Public XoffChar As Byte ' Specifies the value of the XOFF character for both transmission and reception.
Public ErrorChar As Byte ' Specifies the value of the character used to replace bytes received with a parity error.
Public EofChar As Byte ' Specifies the value of the character used to signal the end of data.
Public EvtChar As Byte ' Specifies the value of the character used to signal an event.
Public wReserved1 As Int16 ' Reserved do not use.
End Structure
' This is the OverLapped structure used by the calls to the Windows API.
' 该结构包括异步输入输出操作的相关信息
' This is the OverLapped structure used by the calls to the Windows API.
<StructLayout(LayoutKind.Sequential)> 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 "Enum"
Public Enum DataStopBit
StopBit_1 = 1
StopBit_2
End Enum
Private Enum PurgeBuffers
RXAbort = &H2
RXClear = &H8
TxAbort = &H1
TxClear = &H4
End Enum
Public Enum DataParity
Parity_None = 0
Pariti_Odd
Parity_Even
Parity_Mark
End Enum
Private Enum Lines
SetRts = 3
ClearRts = 4
SetDtr = 5
ClearDtr = 6
ResetDev = 7 ' Reset device if possible
SetBreak = 8 ' Set the device break line.
ClearBreak = 9 ' Clear the device break line.
End Enum
#End Region
#Region "Win32API"
<DllImport("coredll.dll")> Private Shared Function BuildCommDCB( _
ByVal lpDef As String, ByRef lpDCB As DCB) As Boolean
End Function
<DllImport("coredll.dll")> Private Shared Function GetFileSize( _
ByVal hFile As Integer, ByRef lpFileSizeHigh As Integer) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function GetCommState( _
ByVal hCommDev As Integer, ByRef lpDCB As DCB) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function GetCommTimeouts( _
ByVal hFile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function EscapeCommFunction( _
ByVal hFile As Integer, ByVal ifunc As Long) As Boolean
End Function
<DllImport("coredll.dll")> Private Shared Function PurgeComm( _
ByVal hFile As Integer, ByVal dwFlags As Integer) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function SetCommState( _
ByVal hFile As Integer, ByRef lpDCB As DCB) As Integer
End Function 'hCommDev -->hFile
<DllImport("coredll.dll")> Private Shared Function ClearCommError( _
ByVal hFile As Integer, ByVal lpErrors As Integer, _
ByVal l As Integer) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function SetCommTimeouts( _
ByVal hFile As Integer, ByRef lpCommTimeouts As COMMTIMEOUTS) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function CloseHandle( _
ByVal hObject As Integer) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function SetupComm( _
ByVal hFile As Integer, ByVal dwInQueue As Integer, _
ByVal dwOutQueue As Integer) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function CreateFile( _
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("coredll.dll")> Private Shared Function ReadFile( _
ByVal hFile As Integer, ByVal lpBuffer As Byte(), _
ByVal nNumberOfBytesToRead As Integer, _
ByRef lpNumberOfBytesRead As Integer, _
ByRef lpOverlapped As OVERLAPPED) As Integer
End Function
<DllImport("coredll.dll")> Private Shared Function WriteFile( _
ByVal hFile As Integer, ByVal lpBuffer As Byte(), _
ByVal nNumberOfBytesToWrite As Integer, _
ByRef lpNumberOfBytesWritten As Integer, _
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -