PrivateFunction funCommPortDetect(PortNumber AsInteger) AsInteger OnErrorResumeNext'//屏蔽错误 MSComm0.CommPort = PortNumber'//指定端口号 MSComm0.PortOpen =True'//打开端口 If Err Then'//如果端口不存在产生错误 funCommPortDetect =-1'//返回-1 Else'//端口存在返回当前端口号 funCommPortDetect = PortNumber EndIf MSComm0.PortOpen =False'//关闭打开的端口 End Function
PublicFunction CommPortFix() AsString Dim intCommPortNumber AsInteger, blnFirst AsBoolean Dim strResult AsString OnErrorResumeNext'//屏蔽错误 For intCommPortNumber =1To16'//一般机器上不超过16个串口 If funCommPortDetect(intCommPortNumber) <>-1Then strResult = strResult &"" intCommPortNumber'//如果存在串口1,2,5.则结果strResult=1 2 5 EndIf Next CommPortFix = strResult'//返回结果 End Function
PublicFunction CommPortFix() AsString() Dim intCommPortNumber AsInteger, blnFirst AsBoolean Dim strResult AsString OnErrorResumeNext'屏蔽错误 blnFirst =True For intCommPortNumber =1To16 If funCommPortDetect(intCommPortNumber) <>-1Then If blnFirst Then strResult ="COM"& intCommPortNumber blnFirst =False Else strResult = strResult &""&"COM"& intCommPortNumber EndIf EndIf Next CommPortFix =Split(strResult, "") End Function
如果存在串口1,2,5,定义一个数组,Dim arrString() As String,arrString=CommPortFix().这样arrString(0)="COM1",arrString(1)="COM2",arrString(2)="COM5".
文章评论(0条评论)
登录后参与讨论