资源描述:
《在VB中利用API进行串口通信.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、在VB中利用API进行串口通信一般来说,在VB中编写串口通讯程序,首先考虑到是使用MSComm控件,可是该控件不能设置超时,而且对许多内部的参数进行了隐藏,从而不能满足有些具体的工作。而使用API进行串口通信,大多是使用VC,很少见到完整的VB代码,为此,我编写了这个模块。 同时,由于串口通信是基于字节流的,为方便程序设计,还编写了三个简单的辅助函数,并写了一个详细的测试代码。OptionExplicit OptionBase0 PrivateDeclareFunctionCreateFil
2、eLib"kernel32"Alias"CreateFileA"(ByVallpFileNameAsString,ByValdwDesiredAccessAsLong,ByValdwShareModeAsLong,ByVallpSecurityAttributesAsLong,ByValdwCreationDispositionAsLong,ByValdwFlagsAndAttributesAsLong,ByValhTemplateFileAsLong)AsLongPrivateDeclareFun
3、ctionReadFileLib"kernel32"(ByValhFileAsLong,lpBufferAsAny,ByValnNumberOfBytesToReadAsLong,lpNumberOfBytesReadAsLong,ByVallpOverlappedAsLong)AsLongPrivateDeclareFunctionWriteFileLib"kernel32"(ByValhFileAsLong,lpBufferAsAny,ByValnNumberOfBytesToWriteAsLo
4、ng,lpNumberOfBytesWrittenAsLong,ByVallpOverlappedAsLong)AsLongPrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLongPrivateConstGENERIC_READ=&H PrivateConstGENERIC_WRITE=&H PrivateConstOPEN_EXISTING=3 PrivateConstINVALID_HANDLE_VAL
5、UE=-1 PrivateDeclareFunctionGetCommStateLib"kernel32"(ByValnCidAsLong,lpDCBAsDCB)AsLongPrivateDeclareFunctionSetCommStateLib"kernel32"(ByValhCommDevAsLong,lpDCBAsDCB)AsLongPrivateDeclareFunctionSetCommTimeoutsLib"kernel32"(ByValhFileAsLong,lpCommTimeo
6、utsAsCOMMTIMEOUTS)AsLongPrivateDeclareFunctionSetupCommLib"kernel32"(ByValhFileAsLong,ByValdwInQueueAsLong,ByValdwOutQueueAsLong)AsLongPrivateDeclareFunctionPurgeCommLib"kernel32"(ByValhFileAsLong,ByValdwFlagsAsLong)AsLongPrivateConstPURGE_TXABORT=&H1
7、 'Killthepending/currentwritestothecommport. PrivateConstPURGE_RXABORT=&H2 'Killthepending/currentreadstothecommport. PrivateConstPURGE_TXCLEAR=&H4 'Killthetransmitqueueifthere. PrivateConstPURGE_RXCLEAR=&H8 'Killthetypeaheadbufferifthere
8、. PrivateTypeDCB DCBlengthAsLong BaudRateAsLong fBitFieldsAsLong'SeeCommentsinWin32API.Txt wReservedAsInteger XonLimAsInteger XoffLimAsInteger ByteSizeAsByte ParityAsByte StopBi