『壹』 怎樣在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