分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
VB的MSCOMM控件虽然很好用,但是在没有装VB的机器上用该控件总觉得有些累赘,网上的VB API代码大部分都基于是同步方式,处理复杂的通信模式不是太理想,所以用了一些时间,把VC项目中的异步串口读写代码翻译为VB格式。
在VB新建一个类,把下面的代码复制后即可使用
' *************************************************************************
' **模 块 名:SerialPort
' **说 明:YFsoft 版权所有2006 - 2007(C)
' **创 建 人:叶帆
' **日 期:2006-08-17 14:32:29
' **修 改 人:
' **日 期:
' **描 述:串口异步读写(API)
' **版 本:V1.0.0
' *************************************************************************
Option Explicit
Private Type ComStat
fCtsHold As Long
fDsrHold As Long
fRlsdHold As Long
fXoffHold As Long
fXoffSent As Long
fEof As Long
fTxim As Long
fReserved As Long
cbInQue As Long
cbOutQue As Long
End Type
Private Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type
Private Type DCB
DCBlength As Long
BaudRate As Long
' DWORD DCBlength; /* sizeof(DCB) */
' DWORD BaudRate; /* Baudrate at which running */
' DWORD fBinary: 1; /* Binary Mode (skip EOF check) */
' DWORD fParity: 1; /* Enable parity checking */
' DWORD fOutxCtsFlow:1; /* CTS handshaking on output */
' DWORD fOutxDsrFlow:1; /* DSR handshaking on output */
' DWORD fDtrControl:2; /* DTR Flow control */
' DWORD fDsrSensitivity:1; /* DSR Sensitivity */
' DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */
' DWORD fOutX: 1; /* Enable output X-ON/X-OFF */
' DWORD fInX: 1; /* Enable input X-ON/X-OFF */
' DWORD fErrorChar: 1; /* Enable Err Replacement */
' DWORD fNull: 1; /* Enable Null stripping */
' DWORD fRtsControl:2; /* Rts Flow control */
' DWORD fAbortOnError:1; /* Abort all reads and writes on Error */
' DWORD fDummy2:17; /* Reserved */
fBitFields As Long ' See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer ' Reserved; Do Not Use
End Type
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function CloseHandle Lib " kernel32 " ( ByVal hObject As Long ) As Long
Private Declare Function GetLastError Lib " kernel32 " () As Long
Private Declare Function ReadFile Lib " kernel32 " ( ByVal hFile As Long , lpBuffer As Any, ByVal nNumberOfBytesToRead As Long , lpNumberOfBytesRead As Long , lpOverlapped As OVERLAPPED) As Long
Private Declare Function WriteFile Lib " kernel32 " ( ByVal hFile As Long , lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long , lpNumberOfBytesWritten As Long , lpOverlapped As OVERLAPPED) As Long ' OVERLAPPED
Private Declare Function SetCommTimeouts Lib " kernel32 " ( ByVal hFile As Long , lpCommTimeouts As COMMTIMEOUTS) As Long
Private Declare Function GetCommTimeouts Lib " kernel32 " ( ByVal hFile As Long , lpCommTimeouts As COMMTIMEOUTS) As Long
Private Declare Function BuildCommDCB Lib " kernel32 " Alias " BuildCommDCBA " ( ByVal lpDef As String , lpDCB As DCB) As Long
Private Declare Function SetCommState Lib " kernel32 " ( ByVal hCommDev As Long , lpDCB As DCB) As Long
Private Declare Function GetCommState Lib " kernel32 " ( ByVal nCid As Long , lpDCB As DCB) As Long
Private Declare Function CreateFile Lib " kernel32 " Alias " CreateFileA " ( ByVal lpFileName As String , ByVal dwDesiredAccess As Long , ByVal dwShareMode As Long , ByVal lpSecurityAttributes As Long , ByVal dwCreationDisposition As Long , ByVal dwFlagsAndAttributes As Long , ByVal hTemplateFile As Long ) As Long
Private Declare Function FlushFileBuffers Lib " kernel32 " ( ByVal hFile As Long ) As Long
Private Declare Function CreateEvent Lib " kernel32 " Alias " CreateEventA " (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long , ByVal bInitialState As Long , ByVal lpName As String ) As Long
Private Declare Function SetCommMask Lib " kernel32 " ( ByVal hFile As Long , ByVal dwEvtMask As Long ) As Long
Private Declare Function SetEvent Lib " kernel32 " ( ByVal hEvent As Long ) As Long
Private Declare Function PurgeComm Lib " kernel32 " ( ByVal hFile As Long , ByVal dwFlags As Long ) As
' **模 块 名:SerialPort
' **说 明:YFsoft 版权所有2006 - 2007(C)
' **创 建 人:叶帆
' **日 期:2006-08-17 14:32:29
' **修 改 人:
' **日 期:
' **描 述:串口异步读写(API)
' **版 本:V1.0.0
' *************************************************************************
Option Explicit
Private Type ComStat
fCtsHold As Long
fDsrHold As Long
fRlsdHold As Long
fXoffHold As Long
fXoffSent As Long
fEof As Long
fTxim As Long
fReserved As Long
cbInQue As Long
cbOutQue As Long
End Type
Private Type COMMTIMEOUTS
ReadIntervalTimeout As Long
ReadTotalTimeoutMultiplier As Long
ReadTotalTimeoutConstant As Long
WriteTotalTimeoutMultiplier As Long
WriteTotalTimeoutConstant As Long
End Type
Private Type DCB
DCBlength As Long
BaudRate As Long
' DWORD DCBlength; /* sizeof(DCB) */
' DWORD BaudRate; /* Baudrate at which running */
' DWORD fBinary: 1; /* Binary Mode (skip EOF check) */
' DWORD fParity: 1; /* Enable parity checking */
' DWORD fOutxCtsFlow:1; /* CTS handshaking on output */
' DWORD fOutxDsrFlow:1; /* DSR handshaking on output */
' DWORD fDtrControl:2; /* DTR Flow control */
' DWORD fDsrSensitivity:1; /* DSR Sensitivity */
' DWORD fTXContinueOnXoff: 1; /* Continue TX when Xoff sent */
' DWORD fOutX: 1; /* Enable output X-ON/X-OFF */
' DWORD fInX: 1; /* Enable input X-ON/X-OFF */
' DWORD fErrorChar: 1; /* Enable Err Replacement */
' DWORD fNull: 1; /* Enable Null stripping */
' DWORD fRtsControl:2; /* Rts Flow control */
' DWORD fAbortOnError:1; /* Abort all reads and writes on Error */
' DWORD fDummy2:17; /* Reserved */
fBitFields As Long ' See Comments in Win32API.Txt
wReserved As Integer
XonLim As Integer
XoffLim As Integer
ByteSize As Byte
Parity As Byte
StopBits As Byte
XonChar As Byte
XoffChar As Byte
ErrorChar As Byte
EofChar As Byte
EvtChar As Byte
wReserved1 As Integer ' Reserved; Do Not Use
End Type
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Private Declare Function CloseHandle Lib " kernel32 " ( ByVal hObject As Long ) As Long
Private Declare Function GetLastError Lib " kernel32 " () As Long
Private Declare Function ReadFile Lib " kernel32 " ( ByVal hFile As Long , lpBuffer As Any, ByVal nNumberOfBytesToRead As Long , lpNumberOfBytesRead As Long , lpOverlapped As OVERLAPPED) As Long
Private Declare Function WriteFile Lib " kernel32 " ( ByVal hFile As Long , lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long , lpNumberOfBytesWritten As Long , lpOverlapped As OVERLAPPED) As Long ' OVERLAPPED
Private Declare Function SetCommTimeouts Lib " kernel32 " ( ByVal hFile As Long , lpCommTimeouts As COMMTIMEOUTS) As Long
Private Declare Function GetCommTimeouts Lib " kernel32 " ( ByVal hFile As Long , lpCommTimeouts As COMMTIMEOUTS) As Long
Private Declare Function BuildCommDCB Lib " kernel32 " Alias " BuildCommDCBA " ( ByVal lpDef As String , lpDCB As DCB) As Long
Private Declare Function SetCommState Lib " kernel32 " ( ByVal hCommDev As Long , lpDCB As DCB) As Long
Private Declare Function GetCommState Lib " kernel32 " ( ByVal nCid As Long , lpDCB As DCB) As Long
Private Declare Function CreateFile Lib " kernel32 " Alias " CreateFileA " ( ByVal lpFileName As String , ByVal dwDesiredAccess As Long , ByVal dwShareMode As Long , ByVal lpSecurityAttributes As Long , ByVal dwCreationDisposition As Long , ByVal dwFlagsAndAttributes As Long , ByVal hTemplateFile As Long ) As Long
Private Declare Function FlushFileBuffers Lib " kernel32 " ( ByVal hFile As Long ) As Long
Private Declare Function CreateEvent Lib " kernel32 " Alias " CreateEventA " (lpEventAttributes As SECURITY_ATTRIBUTES, ByVal bManualReset As Long , ByVal bInitialState As Long , ByVal lpName As String ) As Long
Private Declare Function SetCommMask Lib " kernel32 " ( ByVal hFile As Long , ByVal dwEvtMask As Long ) As Long
Private Declare Function SetEvent Lib " kernel32 " ( ByVal hEvent As Long ) As Long
Private Declare Function PurgeComm Lib " kernel32 " ( ByVal hFile As Long , ByVal dwFlags As Long ) As