VB6中的串行通信

时间:2015-03-25 17:17:57

标签: vb6

我是vb6和通信应用程序的新手,我尝试运行一个在两个串口之间传输和接收数据的示例(我通过 com0com 软件安装两个串口)。

在此示例中,我希望User在textbox1中键入charracters,然后单击命令按钮,textbox1中的charracters将显示在textbox2中。

Private Sub Command1_Click()
com1.Output = Text1.Text
End Sub

Private Sub Form_Load()
com1.CommPort = 1
com1.Settings = "9600,n,8,1"
com1.PortOpen = True
com2.CommPort = 2
com2.Settings = "9600,n,8,1"
com2.PortOpen = True

Text1.Text = ""
Text2.Text = ""
End Sub

Private Sub com2_OnComm()
If com2.CommEvent = comEvReceive Then
    Text2.Text = Text2.Text + com2.Input

End If
End Sub

我在示例中屏蔽form并在com0com端口设置,我没有声望发布图片,因此我将其上传到flickr。

http://farm8.staticflickr.com/7627/16740707238_6b1d9ec3ab_b.jpg

但是当我尝试通过textbox1中的类型charracters运行此示例并单击按钮时,没有任何反应。

那么我做错了什么?如果是这样,我该如何从com1端口获取数据到com2port?或任何建议!

感谢阅读!

1 个答案:

答案 0 :(得分:1)

我认为你需要更加努力地看一下文档。

这是一个最小的例子,其中COM3 / COM4是我机器上的循环端口:

Option Explicit

Private Sub Command1_Click()
    If Len(Text1.Text) > 0 Then
        MSComm1.Output = Text1.Text
        Text1.Text = vbNullString
    End If
    Text1.SetFocus
End Sub

Private Sub Form_Load()
    With MSComm1
        .CommPort = 3
        .Settings = "256000,n,8,1"
        .Handshaking = comNone
        .SThreshold = 0 'No events after send completions.
        .RThreshold = 0 'No events after receive completions.
        .PortOpen = True
    End With
    With MSComm2
        .CommPort = 4
        .Settings = "256000,n,8,1"
        .EOFEnable = False
        .Handshaking = comNone
        .InputMode = comInputModeText
        .RThreshold = 1 'Event for each character received.  Terribly
                        'inefficient but if char-by-char events are
                        'required there isn't much choice.
        .SThreshold = 0 'No events after send completions.
        .PortOpen = True
    End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
    MSComm1.PortOpen = False
    MSComm2.PortOpen = False
End Sub

Private Sub MSComm2_OnComm()
    With MSComm2
        If .CommEvent = comEvReceive Then
            .InputLen = 0
            Text2.SelStart = &H7FFF
            Text2.SelText = .Input
        End If
    End With
End Sub