各位前辈,大家好,我是初级菜鸟,想用VB发送1个字节到我的单片机,于是在FORM中添加了一个命令控件,
代码如下,确无法运行,总是提示实时错误442,希望能够得到大家的帮助,另外也希望有兴趣的朋友一起讨论,谢谢!Private Sub CmdTest_Click()
If MSComml.PortOpen = False Then
MSComm1.CommPort = 1
MSComm1.Settings = "56000,n,8,1"
MSComm1.PortOpen = True
MSComm1.OutputMode = comOutputModeBinary
MSComm1.InputMode = comInputModeBinaryElse
MsgBox "串口已被占用", vbAbortRetryIgnore, "错误提示"
End IfMSComm1.OutBufferCount = 0
MSComm1.InBufferCount = 0Dim ByteArray() As Byte
ReDim ByteArray(1)
ByteArray(0) = 51
MSComm1.Output = ByteArrayEnd Sub
 

解决方案 »

  1.   


    Private Sub CmdTest_Click()
        'If MSComml.PortOpen = False Then'MSComml错,应为MSComm1
        If MSComm1.PortOpen = False Then
            MSComm1.CommPort = 1
            MSComm1.Settings = "57600,n,8,1"
            MSComm1.InputMode = comInputModeBinary
            'MSComm1.OutputMode = comOutputModeBinary'没OutputMode属性
            MSComm1.PortOpen = True
        End If
        MSComm1.OutBufferCount = 0
        MSComm1.InBufferCount = 0
        Dim ByteArray() As Byte
        ReDim ByteArray(0) '定义ByteArray(1),有2字节
        ByteArray(0) = 51
        MSComm1.Output = ByteArray
    End Sub
      

  2.   


    Private Sub CmdTest_Click()
        'If MSComml.PortOpen = False Then'MSComml错,应为MSComm1
        If MSComm1.PortOpen = False Then
            MSComm1.CommPort = 1
            MSComm1.Settings = "57600,n,8,1"
            MSComm1.InputMode = comInputModeBinary
            'MSComm1.OutputMode = comOutputModeBinary'没OutputMode属性
            MSComm1.PortOpen = True
        End If
        MSComm1.OutBufferCount = 0
        MSComm1.InBufferCount = 0
        Dim ByteArray() As Byte
        ReDim ByteArray(0) '定义ByteArray(1),有2字节
        ByteArray(0) = 51
        MSComm1.Output = ByteArray
    End Sub