77范文网 - 专业文章范例文档资料分享平台

VBA 中使用 API 串口通信 Serial Port(英文)

来源:网络收集 时间:2019-03-03 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

'------------------------------------------------------------------------------- '

' This VB module is a collection of routines to perform serial port I/O without ' using the Microsoft Comm Control component. This module uses the Windows API ' to perform the overlapped I/O operations necessary for serial communications. '

' The routine can handle up to 4 serial ports which are identified with a ' Port ID. '

' All routines (with the exception of CommRead and CommWrite) return an error ' code or 0 if no error occurs. The routine CommGetError can be used to get ' the complete error message.

'-------------------------------------------------------------------------------

'------------------------------------------------------------------------------- ' Public Constants

'-------------------------------------------------------------------------------

' Output Control Lines (CommSetLine) Const LINE_BREAK = 1 Const LINE_DTR = 2 Const 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 = 997 Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const FILE_ATTRIBUTE_NORMAL = &H80

Private Const FILE_FLAG_OVERLAPPED = &H40000000 Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Private Const OPEN_EXISTING = 3

' COMM Functions

Private Const MS_CTS_ON = &H10& Private Const MS_DSR_ON = &H20& Private Const MS_RING_ON = &H40& Private Const MS_RLSD_ON = &H80& Private Const PURGE_RXABORT = &H2

Private Const PURGE_RXCLEAR = &H8 Private Const PURGE_TXABORT = &H1 Private Const PURGE_TXCLEAR = &H4

' COMM Escape Functions Private Const CLRBREAK = 9 Private Const CLRDTR = 6 Private Const CLRRTS = 4 Private Const SETBREAK = 8 Private Const SETDTR = 5 Private Const SETRTS = 3

'------------------------------------------------------------------------------- ' System Structures

'------------------------------------------------------------------------------- Private Type COMSTAT

fBitFields As Long ' See Comment in Win32API.Txt 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 '

' The DCB structure defines the control setting for a serial ' communications device. '

Private Type DCB

DCBlength As Long BaudRate As Long

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

'------------------------------------------------------------------------------- ' System Functions

'------------------------------------------------------------------------------- '

' Fills a specified DCB structure with values specified in ' a device-control string. '

Private Declare Function BuildCommDCB Lib \ (ByVal lpDef As String, lpDCB As DCB) As Long '

' Retrieves information about a communications error and reports ' the current status of a communications device. The function is ' called when a communications error occurs, and it clears the ' device's error flag to enable additional input and output ' (I/O) operations. '

Private Declare Function ClearCommError Lib \

(ByVal hFile As Long, lpErrors As Long, lpStat As COMSTAT) As Long '

' Closes an open communications device or file handle. '

Private Declare Function CloseHandle Lib \'

' Creates or opens a communications resource and returns a handle ' that can be used to access the resource. '

Private Declare Function CreateFile Lib \ (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 communications device to perform a function.

'

Private Declare Function EscapeCommFunction Lib \ (ByVal nCid As Long, ByVal nFunc As Long) As Long '

' Formats a message string such as an error string returned ' by anoher function. '

Private Declare Function FormatMessage Lib \ (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 Long '

' Retrieves modem control-register values. '

Private Declare Function GetCommModemStatus Lib \ (ByVal hFile As Long, lpModemStat As Long) As Long '

' Retrieves the current control settings for a specified ' communications device. '

Private Declare Function GetCommState Lib \ (ByVal nCid As Long, lpDCB As DCB) As Long '

' Retrieves the calling thread's last-error code value. '

Private Declare Function GetLastError Lib \'

' Retrieves the results of an overlapped operation on the ' specified file, named pipe, or communications device. '

Private Declare Function GetOverlappedResult Lib \ (ByVal hFile As Long, lpOverlapped As OVERLAPPED, _

lpNumberOfBytesTransferred As Long, ByVal bWait As Long) As Long '

' Discards all characters from the output or input buffer of a ' specified communications resource. It can also terminate ' pending read or write operations on the resource. '

Private Declare Function PurgeComm Lib \

(ByVal hFile As Long, ByVal dwFlags As Long) As Long '

' Reads data from a file, starting at the position indicated by the ' file pointer. After the read operation has been completed, the ' file 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 adjust the position of the file pointer ' after the read operation. '

Private Declare Function ReadFile Lib \ (ByVal hFile As Long, ByVal lpBuffer As String, _

ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, _ lpOverlapped As OVERLAPPED) As Long '

' Configures a communications device according to the specifications ' in a device-control block (a DCB structure). The function

' reinitializes all hardware and control settings, but it does not ' empty output or input queues. '

Private Declare Function SetCommState Lib \ (ByVal hCommDev As Long, lpDCB As DCB) As Long '

' Sets the time-out parameters for all read and write operations on a ' specified communications device. '

Private Declare Function SetCommTimeouts Lib \

(ByVal hFile As Long, lpCommTimeouts As COMMTIMEOUTS) As Long '

' Initializes the communications parameters for a specified ' communications device. '

Private Declare Function SetupComm Lib \

(ByVal hFile As Long, ByVal dwInQueue As Long, ByVal dwOutQueue As Long) As Long '

' Writes data to a file and is designed for both synchronous and a ' synchronous operation. The function starts writing data to the file ' at the position indicated by the file pointer. After the write ' operation has been completed, the file pointer is adjusted by the ' number of bytes actually written, except when the file is opened with ' FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped ' input and output (I/O), the application must adjust the position of ' the file pointer after the write operation is finished. '

Private Declare Function WriteFile Lib \ (ByVal hFile As Long, ByVal lpBuffer As String, _

ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _ lpOverlapped As OVERLAPPED) As Long

Private Declare Sub AppSleep Lib \'------------------------------------------------------------------------------- ' Program Constants

'-------------------------------------------------------------------------------

Private Const MAX_PORTS = 4

'------------------------------------------------------------------------------- ' Program Structures

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库VBA 中使用 API 串口通信 Serial Port(英文)在线全文阅读。

VBA 中使用 API 串口通信 Serial Port(英文).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/491765.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: