‘壹’ 怎样在vb中自动检测到可用串口号的具体程序代码
j = 0x0dx0aFor i = 1 To 16 Step 1x0dx0aIf comISP.PortOpen = True Then '先关闭串口x0dx0acomISP.PortOpen = Falsex0dx0aEnd Ifx0dx0ax0dx0acomISP.CommPort = ix0dx0aOn Error Resume Next '说明当一个运行时错误发生时,控件转到紧接着发生错误的语句之后的语句,并在此继续运行。访问对象时要使用这种形式而不使用 On Error GoTo。x0dx0acomISP.PortOpen = Truex0dx0aIf Err.Number <> 8002 Then '无效的串口号。这样可以检测到虚拟串口,如果用Err.Number = 0的话检测不到虚拟串口x0dx0aIf j = 0 Thenx0dx0aj = ix0dx0aEnd Ifx0dx0acboPort.AddItem "COM" & i '生成串口选择列表x0dx0aEnd Ifx0dx0acomISP.PortOpen = Falsex0dx0aNext ix0dx0ax0dx0aIf j >= 1 Thenx0dx0acboPort.Text = "COM" & j '自动打开可用的最小串口号x0dx0acomISP.CommPort = jx0dx0acomISP.PortOpen = Truex0dx0acmdOpenCom.Caption = "关闭串口"x0dx0ashpCOM.FillColor = vbGreenx0dx0aIf Err.Number = 8005 Then '串口已打开,vbExclamation 'x0dx0acomISP.PortOpen = Falsex0dx0acboPort.Text = ""x0dx0acmdOpenCom.Caption = "打开串口"x0dx0ashpCOM.FillColor = vbRedx0dx0aEnd Ifx0dx0aEnd If