问题描述
新手请多关照诸位大神们都知道Win7里面是可以修改COM口的具体值的,我在测试我的可用COM口自动检测程序时,发现在修改到COM10及以上就检测不到了,求解?程序如下ThanksalotPublicSharedFunctionAutoGetCOMNumber()AsInteger'这是Class里的程序DimMiportNumberAsIntegerIfmiport<0ThenThrowNewApplicationException("系统中的串行端口未定义!")ExitFunctionEndIfTryFori=1To20mhrs=CreateFile("COM"&i.ToString,GENERIC_READOrGENERIC_WRITE,0,0,OPEN_EXISTING,0,0)Ifmhrs<>-1ThenDimlpErrorCodeAsIntegerDimiRcAsIntegerDimuDcbAsDCBMiportNumber=iiRc=ClearCommError(mhrs,lpErrorCode,Nothing)'清除通信错误iRc=PurgeComm(mhrs,PurgeBuffers.RXClearOrPurgeBuffers.TxClear)'清除缓冲区iRc=SetupComm(mhrs,mibuffersize,mibuffersize)'设置缓冲区大小iRc=GetCommState(mhrs,uDcb)'取得通信端口设置值'设置通信参数uDcb.BaudRate=miBaudRateuDcb.ByteSize=CByte(midatabit)uDcb.Parity=CByte(meParity)uDcb.StopBits=CByte(mestopbit)iRc=SetCommState(mhrs,uDcb)'将参数写入系统IfiRc=0Then'如果设置不成功DimlCodeAsIntegerlCode=GetLastError()MsgBox("DCB结构创建发生错误,,错误代码="&lCode.ToString)End'设置不成功,在提示信息后退出程序EndIfExitForEndIfNextReturnMiportNumberCatchexAsExceptionThrowNewApplicationException("自动获取串口失败"&ex.Message,ex)Return0EndTryEndFunction'这是按钮程序PrivateSubAutoGetCOM_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesAutoGetCOM.ClickIfNotfPortOpenThenDimflagAsIntegerflag=Class1.AutoGetCOMNumber()Ifflag>0ThenfPortOpen=1AutoGetCOM.Text="GotCOM"&flagOpenTheCOM.Text="关闭通信端口"ElseMsgBox("自动获取失败")EndIfEndIfEndSub