1、vba中使用api串口通信serialport英文-This VB module is a collection of routines to perform serial port I/O withoutusing the Microsoft Comm Control component. This module uses the Windows APIto perform the overlapped I/O operations necessary for serial communications.The routine can handle up to 4 serial ports
2、which are identified with aPort ID.All routines (with the exception of CommRead and CommWrite) return an errorcode or 0 if no error occurs. The routine CommGetError can be used to getthe complete error message.- Public Constants- Output Control Lines (CommSetLine)Const LINE_BREAK = 1Const LINE_DTR =
3、 2Const LINE_RTS = 3 Input Control Lines (CommGetLine)Const LINE_CTS = &H10&Const LINE_DSR = &H20&Const LINE_RING = &H40&Const LINE_RLSD = &H80&Const LINE_CD = &H80&- System Constants-Private Const ERROR_IO_INCOMPLETE = 996&Private Const ERROR_IO_PENDING = 997Private Const GENERIC_READ = &HPrivate C
4、onst GENERIC_WRITE = &HPrivate Const FILE_ATTRIBUTE_NORMAL = &H80Private Const FILE_FLAG_OVERLAPPED = &HPrivate Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000Private Const OPEN_EXISTING = 3 COMM FunctionsPrivate Const MS_CTS_ON = &H10&Private Const MS_DSR_ON = &H20&Private Const MS_RING_ON = &H40&Private
5、 Const MS_RLSD_ON = &H80&Private Const PURGE_RXABORT = &H2Private Const PURGE_RXCLEAR = &H8Private Const PURGE_TXABORT = &H1Private Const PURGE_TXCLEAR = &H4COMM Escape Functions Private Const CLRBREAK = 9 Private Const CLRDTR = 6Private Const CLRRTS = 4Private Const SETBREAK = 8Private Const SETDTR
6、 = 5Private Const SETRTS = 3- System Structures-Private Type COMSTATfBitFields As Long See Comment incbInQue As LongcbOutQue As LongEnd TypePrivate Type COMMTIMEOUTSReadIntervalTimeout As LongReadTotalTimeoutMultiplier As LongReadTotalTimeoutConstant As LongWriteTotalTimeoutMultiplier As LongWriteTo
7、talTimeoutConstant As LongEnd TypeThe DCB structure defines the control setting for a serialcommunications device.Private Type DCBDCBlength As LongBaudRate As LongfBitFields As Long See Comments inwReserved As IntegerXonLim As IntegerXoffLim As IntegerByteSize As ByteParity As ByteStopBits As ByteXo
8、nChar As ByteXoffChar As ByteErrorChar As ByteEofChar As ByteEvtChar As BytewReserved1 As Integer Reserved; Do Not UseEnd TypePrivate Type OVERLAPPEDInternal As LongInternalHigh As Longoffset As LongOffsetHigh As LonghEvent As LongEnd TypePrivate Type SECURITY_ATTRIBUTESnLength As LonglpSecurityDesc
9、riptor As LongbInheritHandle As LongEnd Type- System Functions- Fills a specified DCB structure with values specified in a device-control string.Private Declare Function BuildCommDCB Lib kernel32 Alias BuildCommDCBA _ (ByVal lpDef As String, lpDCB As DCB) As LongRetrieves information about a communi
10、cations error and reportsthe current status of a communications device. The function iscalled when a communications error occurs, and it clears thedevices error flag to enable additional input and output(I/O) operations.Private Declare Function ClearCommError Lib kernel32 _(ByVal hFile As Long, lpEr
11、rors As Long, lpStat As COMSTAT) As LongCloses an open communications device or file handle.Private Declare Function CloseHandle Lib kernel32 (ByVal hObject As Long) As Long Creates or opens a communications resource and returns a handlethat can be used to access the resource.Private Declare Functio
12、n CreateFile Lib kernel32 Alias CreateFileA _ (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, lpSecurityAttributes As Any, _ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long Directs a specified com
13、munications device to perform a function.Private Declare Function EscapeCommFunction Lib kernel32 _ (ByVal nCid As Long, ByVal nFunc As Long) As LongFormats a message string such as an error string returnedby anoher function.Private Declare Function FormatMessage Lib kernel32 Alias FormatMessageA _
14、(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _ ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, _ Arguments As Long) As LongRetrieves modem control-register values.Private Declare Function GetCommModemStatus Lib kernel32 _ (ByVal hFile As Long, lpMode
15、mStat As Long) As LongRetrieves the current control settings for a specifiedcommunications device.Private Declare Function GetCommState Lib kernel32 _(ByVal nCid As Long, lpDCB As DCB) As LongRetrieves the calling threads last-error code value.Private Declare Function GetLastError Lib kernel32 () As
16、 LongRetrieves the results of an overlapped operation on thespecified file, named pipe, or communications device.Private Declare Function GetOverlappedResult Lib kernel32 _ (ByVal hFile As Long, lpOverlapped As OVERLAPPED, _ lpNumberOfBytesTransferred As Long, ByVal bWait As Long) As LongDiscards al
17、l characters from the output or input buffer of aspecified communications resource. It can also terminatepending read or write operations on the resource.Private Declare Function PurgeComm Lib kernel32 _ (ByVal hFile As Long, ByVal dwFlags As Long) As LongReads data from a file, starting at the posi
18、tion indicated by thefile pointer. After the read operation has been completed, thefile pointer is adjusted by the number of bytes actually read,unless the file handle is created with the overlapped attribute.If the file handle is created for overlapped input and output(I/O), the application must ad
19、just the position of the file pointerafter the read operation.Private Declare Function ReadFile Lib kernel32 _ (ByVal hFile As Long, ByVal lpBuffer As String, _ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, _ lpOverlapped As OVERLAPPED) As LongConfigures a communications devi
20、ce according to the specificationsin a device-control block (a DCB structure). The functionreinitializes all hardware and control settings, but it does notempty output or input queues.Private Declare Function SetCommState Lib kernel32 _ (ByVal hCommDev As Long, lpDCB As DCB) As LongSets the time-out
21、 parameters for all read and write operations on aspecified communications device.Private Declare Function SetCommTimeouts Lib kernel32 _(ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As LongInitializes the communications parameters for a specifiedcommunications device.Private Declare Functio
22、n SetupComm Lib kernel32 _(ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As LongWrites data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the fileat the position indicated by the file pointer. After the writeo
23、peration has been completed, the file pointer is adjusted by thenumber of bytes actually written, except when the file is opened withFILE_FLAG_OVERLAPPED. If the file handle was created for overlappedinput and output (I/O), the application must adjust the position ofthe file pointer after the write
24、operation is finished.Private Declare Function WriteFile Lib kernel32 _ (ByVal hFile As Long, ByVal lpBuffer As String, _ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _ lpOverlapped As OVERLAPPED) As LongPrivate Declare Sub AppSleep Lib kernel32 Alias Sleep (ByVal dwMillisecon
25、ds As Long) - Program Constants-Private Const MAX_PORTS = 4- Program Structures-Private Type COMM_ERRORlngErrorCode As LongstrFunction As StringstrErrorMessage As StringEnd TypePrivate Type COMM_PORTlngHandle As LongblnPortOpen As BooleanudtDCB As DCBEnd Type- Program Storage-Private udtCommOverlap
26、As OVERLAPPEDPrivate udtCommError As COMM_ERRORPrivate udtPorts(1 To MAX_PORTS) As COMM_PORT-GetSystemMessage - Gets system error text for the specified error code. -Public Function GetSystemMessage(lngErrorCode As Long) As StringDim intPos As IntegerDim strMessage As String, strMsgBuff As String * 256Call FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lngErrorCode, 0, strMsgBuff, 255, 0)intPos =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1