程序如下:
Option ExplicitPrivate Sub Command2_Click()
  Unload Me
  End
End SubPrivate Sub Form_Load()
   
   MSComm1.PortOpen = True '每次都是这里提示错误“8002, 无效端口错误”,这句代码这样写难道不对吗?以下只要涉及到串口的语句也都提示错误
End Sub
                                                                                 
Private Sub Command4_Click() '条码复位
MSComm1.Output = "R"
End SubPrivate Sub Form_Unload(Cancel As Integer)
MSComm1.PortOpen = False
End Sub
Private Sub Timer1_Timer() '定时扫描
If MSComm1.InBufferCount < 1 Then
   MSComm1.InBufferCount = 0
   Text2.Text = 0
Else
Text2.Text = MSComm1.Input  '读取条码的数据
MSComm1.InBufferCount = 0
End If
End Sub

解决方案 »

  1.   

    用下列代码检查你可用串口号,将MSComm控件的属性CommPort设置为可用COM号.
    Option Explicit
        Dim a As Integer
        Dim b(16) As Integer
        Dim c(16) As Integer
        Dim entrySub Form_Load()
        On Error GoTo abc
        For a = 1 To 4
            MSComm1.CommPort = a
            MSComm1.PortOpen = True
            c(a) = a
            MSComm1.PortOpen = False
        Next
        For a = 1 To 4             '显示有效端口号
            If b(a) = c(a) Then
                'Print ; "无效COM口= " & b(a)
            Else
                'Print ; "有效COM口= " & a
                entry = "COM" & a   ' 创建输入项。
                Combo1.AddItem entry
          End If
        Next
        Exit Sub
    abc:
        b(a) = a
        Resume Next
    End Sub