VBA中使用API串口通信SerialPort英文.docx

上传人:b****3 文档编号:3939189 上传时间:2022-11-26 格式:DOCX 页数:28 大小:20.48KB
下载 相关 举报
VBA中使用API串口通信SerialPort英文.docx_第1页
第1页 / 共28页
VBA中使用API串口通信SerialPort英文.docx_第2页
第2页 / 共28页
VBA中使用API串口通信SerialPort英文.docx_第3页
第3页 / 共28页
VBA中使用API串口通信SerialPort英文.docx_第4页
第4页 / 共28页
VBA中使用API串口通信SerialPort英文.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

VBA中使用API串口通信SerialPort英文.docx

《VBA中使用API串口通信SerialPort英文.docx》由会员分享,可在线阅读,更多相关《VBA中使用API串口通信SerialPort英文.docx(28页珍藏版)》请在冰豆网上搜索。

VBA中使用API串口通信SerialPort英文.docx

VBA中使用API串口通信SerialPort英文

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

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

'

'ThisVBmoduleisacollectionofroutinestoperformserialportI/Owithout

'usingtheMicrosoftCommControlcomponent.ThismoduleusestheWindowsAPI

'toperformtheoverlappedI/Ooperationsnecessaryforserialcommunications.

'

'Theroutinecanhandleupto4serialportswhichareidentifiedwitha

'PortID.

'

'Allroutines(withtheexceptionofCommReadandCommWrite)returnanerror

'codeor0ifnoerroroccurs.TheroutineCommGetErrorcanbeusedtoget

'thecompleteerrormessage.

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

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

'PublicConstants

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

'OutputControlLines(CommSetLine)

ConstLINE_BREAK=1

ConstLINE_DTR=2

ConstLINE_RTS=3

'InputControlLines(CommGetLine)

ConstLINE_CTS=&H10&

ConstLINE_DSR=&H20&

ConstLINE_RING=&H40&

ConstLINE_RLSD=&H80&

ConstLINE_CD=&H80&

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

'SystemConstants

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

PrivateConstERROR_IO_INCOMPLETE=996&

PrivateConstERROR_IO_PENDING=997

PrivateConstGENERIC_READ=&H80000000

PrivateConstGENERIC_WRITE=&H40000000

PrivateConstFILE_ATTRIBUTE_NORMAL=&H80

PrivateConstFILE_FLAG_OVERLAPPED=&H40000000

PrivateConstFORMAT_MESSAGE_FROM_SYSTEM=&H1000

PrivateConstOPEN_EXISTING=3

'COMMFunctions

PrivateConstMS_CTS_ON=&H10&

PrivateConstMS_DSR_ON=&H20&

PrivateConstMS_RING_ON=&H40&

PrivateConstMS_RLSD_ON=&H80&

PrivateConstPURGE_RXABORT=&H2

PrivateConstPURGE_RXCLEAR=&H8

PrivateConstPURGE_TXABORT=&H1

PrivateConstPURGE_TXCLEAR=&H4

'COMMEscapeFunctions

PrivateConstCLRBREAK=9

PrivateConstCLRDTR=6

PrivateConstCLRRTS=4

PrivateConstSETBREAK=8

PrivateConstSETDTR=5

PrivateConstSETRTS=3

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

'SystemStructures

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

PrivateTypeCOMSTAT

fBitFieldsAsLong'SeeCommentinWin32API.Txt

cbInQueAsLong

cbOutQueAsLong

EndType

PrivateTypeCOMMTIMEOUTS

ReadIntervalTimeoutAsLong

ReadTotalTimeoutMultiplierAsLong

ReadTotalTimeoutConstantAsLong

WriteTotalTimeoutMultiplierAsLong

WriteTotalTimeoutConstantAsLong

EndType

'

'TheDCBstructuredefinesthecontrolsettingforaserial

'communicationsdevice.

'

PrivateTypeDCB

DCBlengthAsLong

BaudRateAsLong

fBitFieldsAsLong'SeeCommentsinWin32API.Txt

wReservedAsInteger

XonLimAsInteger

XoffLimAsInteger

ByteSizeAsByte

ParityAsByte

StopBitsAsByte

XonCharAsByte

XoffCharAsByte

ErrorCharAsByte

EofCharAsByte

EvtCharAsByte

wReserved1AsInteger'Reserved;DoNotUse

EndType

PrivateTypeOVERLAPPED

InternalAsLong

InternalHighAsLong

offsetAsLong

OffsetHighAsLong

hEventAsLong

EndType

PrivateTypeSECURITY_ATTRIBUTES

nLengthAsLong

lpSecurityDescriptorAsLong

bInheritHandleAsLong

EndType

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

'SystemFunctions

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

'

'FillsaspecifiedDCBstructurewithvaluesspecifiedin

'adevice-controlstring.

'

PrivateDeclareFunctionBuildCommDCBLib"kernel32"Alias"BuildCommDCBA"_

(ByVallpDefAsString,lpDCBAsDCB)AsLong

'

'Retrievesinformationaboutacommunicationserrorandreports

'thecurrentstatusofacommunicationsdevice.Thefunctionis

'calledwhenacommunicationserroroccurs,anditclearsthe

'device'serrorflagtoenableadditionalinputandoutput

'(I/O)operations.

'

PrivateDeclareFunctionClearCommErrorLib"kernel32"_

(ByValhFileAsLong,lpErrorsAsLong,lpStatAsCOMSTAT)AsLong

'

'Closesanopencommunicationsdeviceorfilehandle.

'

PrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLong

'

'Createsoropensacommunicationsresourceandreturnsahandle

'thatcanbeusedtoaccesstheresource.

'

PrivateDeclareFunctionCreateFileLib"kernel32"Alias"CreateFileA"_

(ByVallpFileNameAsString,ByValdwDesiredAccessAsLong,_

ByValdwShareModeAsLong,lpSecurityAttributesAsAny,_

ByValdwCreationDispositionAsLong,ByValdwFlagsAndAttributesAsLong,_

ByValhTemplateFileAsLong)AsLong

'

'Directsaspecifiedcommunicationsdevicetoperformafunction.

'

PrivateDeclareFunctionEscapeCommFunctionLib"kernel32"_

(ByValnCidAsLong,ByValnFuncAsLong)AsLong

'

'Formatsamessagestringsuchasanerrorstringreturned

'byanoherfunction.

'

PrivateDeclareFunctionFormatMessageLib"kernel32"Alias"FormatMessageA"_

(ByValdwFlagsAsLong,lpSourceAsAny,ByValdwMessageIdAsLong,_

ByValdwLanguageIdAsLong,ByVallpBufferAsString,ByValnSizeAsLong,_

ArgumentsAsLong)AsLong

'

'Retrievesmodemcontrol-registervalues.

'

PrivateDeclareFunctionGetCommModemStatusLib"kernel32"_

(ByValhFileAsLong,lpModemStatAsLong)AsLong

'

'Retrievesthecurrentcontrolsettingsforaspecified

'communicationsdevice.

'

PrivateDeclareFunctionGetCommStateLib"kernel32"_

(ByValnCidAsLong,lpDCBAsDCB)AsLong

'

'Retrievesthecallingthread'slast-errorcodevalue.

'

PrivateDeclareFunctionGetLastErrorLib"kernel32"()AsLong

'

'Retrievestheresultsofanoverlappedoperationonthe

'specifiedfile,namedpipe,orcommunicationsdevice.

'

PrivateDeclareFunctionGetOverlappedResultLib"kernel32"_

(ByValhFileAsLong,lpOverlappedAsOVERLAPPED,_

lpNumberOfBytesTransferredAsLong,ByValbWaitAsLong)AsLong

'

'Discardsallcharactersfromtheoutputorinputbufferofa

'specifiedcommunicationsresource.Itcanalsoterminate

'pendingreadorwriteoperationsontheresource.

'

PrivateDeclareFunctionPurgeCommLib"kernel32"_

(ByValhFileAsLong,ByValdwFlagsAsLong)AsLong

'

'Readsdatafromafile,startingatthepositionindicatedbythe

'filepointer.Afterthereadoperationhasbeencompleted,the

'filepointerisadjustedbythenumberofbytesactuallyread,

'unlessthefilehandleiscreatedwiththeoverlappedattribute.

'Ifthefilehandleiscreatedforoverlappedinputandoutput

'(I/O),theapplicationmustadjustthepositionofthefilepointer

'afterthereadoperation.

'

PrivateDeclareFunctionReadFileLib"kernel32"_

(ByValhFileAsLong,ByVallpBufferAsString,_

ByValnNumberOfBytesToReadAsLong,ByReflpNumberOfBytesReadAsLong,_

lpOverlappedAsOVERLAPPED)AsLong

'

'Configuresacommunicationsdeviceaccordingtothespecifications

'inadevice-controlblock(aDCBstructure).Thefunction

'reinitializesallhardwareandcontrolsettings,butitdoesnot

'emptyoutputorinputqueues.

'

PrivateDeclareFunctionSetCommStateLib"kernel32"_

(ByValhCommDevAsLong,lpDCBAsDCB)AsLong

'

'Setsthetime-outparametersforallreadandwriteoperationsona

'specifiedcommunicationsdevice.

'

PrivateDeclareFunctionSetCommTimeoutsLib"kernel32"_

(ByValhFileAsLong,lpCommTimeoutsAsCOMMTIMEOUTS)AsLong

'

'Initializesthecommunicationsparametersforaspecified

'communicationsdevice.

'

PrivateDeclareFunctionSetupCommLib"kernel32"_

(ByValhFileAsLong,ByValdwInQueueAsLong,ByValdwOutQueueAsLong)AsLong

'

'Writesdatatoafileandisdesignedforbothsynchronousanda

'synchronousoperation.Thefunctionstartswritingdatatothefile

'atthepositionindicatedbythefilepointer.Afterthewrite

'operationhasbeencompleted,thefilepointerisadjustedbythe

'numberofbytesactuallywritten,exceptwhenthefileisopenedwith

'FILE_FLAG_OVERLAPPED.Ifthefilehandlewascreatedforoverlapped

'inputandoutput(I/O),theapplicationmustadjustthepositionof

'thefilepointerafterthewriteoperationisfinished.

'

PrivateDeclareFunctionWriteFileLib"kernel32"_

(ByValhFileAsLong,ByVallpBufferAsString,_

ByValnNumberOfBytesToWriteAsLong,lpNumberOfBytesWrittenAsLong,_

lpOverlappedAsOVERLAPPED)AsLong

 

PrivateDeclareSubAppSleepLib"kernel32"Alias"Sleep"(ByValdwMillisecondsAsLong)

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

'ProgramConstants

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

PrivateConstMAX_PORTS=4

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

'ProgramStructures

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

PrivateTypeCOMM_ERROR

lngErrorCodeAsLong

strFunctionAsString

strErrorMessageAsString

EndType

PrivateTypeCOMM_PORT

lngHandleAsLong

blnPortOpenAsBoolean

udtDCBAsDCB

EndType

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

'ProgramStorage

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

PrivateudtCommOverlapAsOVERLAPPED

PrivateudtCommErrorAsCOMM_ERROR

PrivateudtPorts(1ToMAX_PORTS)AsCOMM_PORT

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

'GetSystemMessage-Getssystemerrortextforthespecifiederrorcode.

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

PublicFunctionGetSystemMessage(lngErrorCodeAsLong)AsString

DimintPos

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1