vba中使用api串口通信serialport英文.docx

上传人:b****8 文档编号:10894938 上传时间:2023-02-23 格式:DOCX 页数:35 大小:20.61KB
下载 相关 举报
vba中使用api串口通信serialport英文.docx_第1页
第1页 / 共35页
vba中使用api串口通信serialport英文.docx_第2页
第2页 / 共35页
vba中使用api串口通信serialport英文.docx_第3页
第3页 / 共35页
vba中使用api串口通信serialport英文.docx_第4页
第4页 / 共35页
vba中使用api串口通信serialport英文.docx_第5页
第5页 / 共35页
点击查看更多>>
下载资源
资源描述

vba中使用api串口通信serialport英文.docx

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

vba中使用api串口通信serialport英文.docx

vba中使用api串口通信serialport英文

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

 

'

'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=&H

PrivateConstGENERIC_WRITE=&H

PrivateConstFILE_ATTRIBUTE_NORMAL=&H80

PrivateConstFILE_FLAG_OVERLAPPED=&H

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

 

'COMMEscapeFunctionsPrivateConstCLRBREAK=9PrivateConstCLRDTR=6

PrivateConstCLRRTS=4

PrivateConstSETBREAK=8

PrivateConstSETDTR=5

PrivateConstSETRTS=3

 

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

'SystemStructures

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

PrivateTypeCOMSTAT

fBitFieldsAsLong'SeeCommentin

cbInQueAsLong

cbOutQueAsLong

EndType

 

PrivateTypeCOMMTIMEOUTS

ReadIntervalTimeoutAsLong

ReadTotalTimeoutMultiplierAsLong

ReadTotalTimeoutConstantAsLong

WriteTotalTimeoutMultiplierAsLong

WriteTotalTimeoutConstantAsLong

EndType

 

'

'TheDCBstructuredefinesthecontrolsettingforaserial

'communicationsdevice.

'

PrivateTypeDCB

DCBlengthAsLong

BaudRateAsLong

fBitFieldsAsLong'SeeCommentsin

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

DimintPosAsInteger

DimstrMessageAsString,strMsgBuffAsString*256

 

CallFormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,lngErrorCode,0,strMsgBuff,255,0)

 

intPos=

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

当前位置:首页 > 考试认证 > 从业资格考试

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

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