?? module1.bas
字號:
Attribute VB_Name = "Module1"
Option Explicit
Public Const GENERIC_WRITE = &H40000000
Public Const GENERIC_READ = &H80000000
Public Const OPEN_EXISTING = 3
Public Const FILE_FLAG_OVERLAPPED = &H40000000
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const PURGE_RXABORT = &H2 ' Kill the pending/current reads to the comm port.
Public Const PURGE_RXCLEAR = &H8 ' Kill the typeahead buffer if there.
Public Const PURGE_TXCLEAR = &H4 ' Kill the transmit queue if there.
Public Const PURGE_TXABORT = &H1 ' Kill the pending/current writes to the comm port.
'奇偶校驗設置的值
Public Const EVENPARITY = 2
Public Const ODDPARITY = 1
Public Const NOPARITY = 0
Public Const MARKPARITY = 3
Public Const SPACEPARITY = 4
'串口通信的數據結構,設備控制DCB
Public Type DCB
DCBlength As Long 'DCB結構大小
BaudRate As Long '波特率
'本來在該結構中沒有fBitFields變量,而是另外14個變量
'這14個變量可以包含在fBitFields變量的4個字節里,對fBitFields變量
'執行邏輯運算(And或Or)操作可以獲取原先的效果
fBitFields As Long
wReserved As Integer '當前未用,必須置0
XonLim As Integer 'XON閾值
XoffLim As Integer 'XOFF閾值
ByteSize As Byte '字符位數,4-8
Parity As Byte '奇偶校驗位
StopBits As Byte '0,1,2分別為1,1.5,2
XonChar As Byte 'XON字符
XoffChar As Byte 'XOFF字符
ErrorChar As Byte '奇偶錯誤替代字符
EofChar As Byte '結束字符
EvtChar As Byte '事件字符
wReserved1 As Integer '保留,未用
End Type
'串口當前狀態的數據結構
Public Type COMSTAT
'本來在該結構中沒有fBitFields變量,而是另外8個變量
'這8個變量可以包含在fBitFields變量的4個字節里,對fBitFields變量
'執行邏輯運算(And或Or)操作可以獲取原先的效果
fBitFields As Long
cbInQue As Long '指出已經接收到但是沒有被ReadFile操作讀取的字節數
cbOutQue As Long '正在被所有寫操作傳送的用戶數據
End Type
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Public Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type
Public Type COMMCONFIG
dwSize As Long
wVersion As Integer
wReserved As Integer
dcbx As DCB
dwProviderSubType As Long
dwProviderOffset As Long
dwProviderSize As Long
wcProviderData As Byte
End Type
Public Type COMMPROP
wPacketLength As Integer
wPacketVersion As Integer
dwServiceMask As Long
dwReserved1 As Long
dwMaxTxQueue As Long
dwMaxRxQueue As Long
dwMaxBaud As Long
dwProvSubType As Long
dwProvCapabilities As Long
dwSettableParams As Long
dwSettableBaud As Long
wSettableData As Integer
wSettableStopParity As Integer
dwCurrentTxQueue As Long
dwCurrentRxQueue As Long
dwProvSpec1 As Long
dwProvSpec2 As Long
wcProvChar(1) As Integer
End Type
Public Declare Function CreateFile Lib "kernel32" Alias _
"CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess _
As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes _
As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes _
As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function ReadFile Lib "kernel32" _
(ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToRead _
As Long, ByRef lpNumberOfBytesRead As Long, _
ByRef lpOverlapped As Any) As Long 'OVERLAPPED) As Long
Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByRef lpOverlapped As Any) As Long 'OVERLAPPED) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function BuildCommDCB Lib "kernel32" Alias "BuildCommDCBA" (ByVal lpDef As String, lpDCB As DCB) As Long
Public Declare Function BuildCommDCBAndTimeouts Lib "kernel32" Alias "BuildCommDCBAndTimeoutsA" (ByVal lpDef As String, lpDCB As DCB, lpCommTimeouts As COMMTIMEOUTS) As Long
Public Declare Function ClearCommBreak Lib "kernel32" (ByVal nCid As Long) As Long
Public Declare Function ClearCommError Lib "kernel32" (ByVal hFile As Long, lpErrors As Long, lpStat As COMSTAT) As Long
Public Declare Function CommConfigDialog Lib "kernel32" Alias "CommConfigDialogA" (ByVal lpszName As String, ByVal hWnd As Long, lpcc As COMMCONFIG) As Long
Public Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
Public Declare Function EscapeCommFunction Lib "kernel32" (ByVal nCid As Long, ByVal nFunc As Long) As Long
Public Declare Function GetCommConfig Lib "kernel32" (ByVal hCommDev As Long, lpcc As COMMCONFIG, lpdwSize As Long) As Long
Public Declare Function GetCommMask Lib "kernel32" (ByVal hFile As Long, lpEvtMask As Long) As Long
Public Declare Function GetCommModemStatus Lib "kernel32" (ByVal hFile As Long, lpModemStat As Long) As Long
Public Declare Function GetCommProperties Lib "kernel32" (ByVal hFile As Long, lpCommProp As COMMPROP) As Long
Public Declare Function GetCommState Lib "kernel32" (ByVal nCid As Long, lpDCB As DCB) As Long
Public Declare Function GetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Public Declare Function GetDefaultCommConfig Lib "kernel32" Alias "GetDefaultCommConfigA" (ByVal lpszName As String, lpcc As COMMCONFIG, lpdwSize As Long) As Long
Public Declare Function PurgeComm Lib "kernel32" (ByVal hFile As Long, ByVal dwFlags As Long) As Long
Public Declare Function SetCommBreak Lib "kernel32" (ByVal nCid As Long) As Long
Public Declare Function SetCommConfig Lib "kernel32" (ByVal hCommDev As Long, lpcc As COMMCONFIG, ByVal dwSize As Long) As Long
Public Declare Function SetCommMask Lib "kernel32" (ByVal hFile As Long, ByVal dwEvtMask As Long) As Long
Public Declare Function SetCommState Lib "kernel32" (ByVal hCommDev As Long, lpDCB As DCB) As Long
Public Declare Function SetCommTimeouts Lib "kernel32" (ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long
Public Declare Function SetDefaultCommConfig Lib "kernel32" Alias "SetDefaultCommConfigA" (ByVal lpszName As String, lpcc As COMMCONFIG, ByVal dwSize As Long) As Long
Public Declare Function SetupComm Lib "kernel32" (ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long
Public Declare Function TransmitCommChar Lib "kernel32" (ByVal nCid As Long, ByVal cChar As Byte) As Long
Public Declare Function WaitCommEvent Lib "kernel32" (ByVal hFile As Long, lpEvtMask As Long, lpOverlapped As OVERLAPPED) As Long
Public Declare Function GetOverlappedResult Lib "kernel32" (ByVal hFile As Long, lpOverlapped As OVERLAPPED, lpNumberOfBytesTransferred As Long, ByVal bWait As Long) As Long
'該函數用來設置延遲,在利用事件自動讀取串口數據的時候使用
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Declare Sub MoveMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pDest As Any, _
pSource As Any, _
ByVal dwLength As Long)
'*******************************************************************
'功能: 將一個字符串拆分為單個字符的數組
'參數:
' strInput: String類型,用來傳遞要拆分的字符串
'輸出:
' strSplitChar: String類型,字符數組。
'*******************************************************************
Public Function strSplitChar(ByVal strInput As String) As String()
Dim sStr() As String
Dim nLen As Long '定義變量,用來存放參數字符串的長度
Dim i As Long
If strInput = vbNullString Then
MsgBox "該函數不操作空字符串!"
Exit Function
End If
nLen = Len(strInput)
'為sStr動態數組設置大小,其長度是字符串長度加1,因為要存放字符串Chr(0)
ReDim sStr(0 To nLen)
For i = 0 To nLen
If i <> nLen Then
'每次,提取字符的位置遞增1,提取的長度總是1,單個字符
sStr(i) = Mid$(strInput, i + 1, 1)
Else
sStr(i) = Chr$(0)
End If
Next i
strSplitChar = sStr()
End Function
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -