我用VB做了一个串口调试程序
If MSComm1.PortOpen = False Then''''''串口初始化''''''
    MSComm1.Settings = "9600,N,8,1"
    'MSComm1.CommPort = 4
    'MSComm1.CommPort = Port
    'MSComm1.Settings = (Baud) & "," & (parity_bit) & "," & (Data_bit) & "," & (Stop_bit)
    MSComm1.InputLen = 0
    MSComm1.InputMode = comInputModeText
    MSComm1.InBufferCount = 0
    MSComm1.OutBufferCount = 0
    MSComm1.SThreshold = 0
    MSComm1.RThreshold = 1 '缓冲区中接收到一个字符,就产生一次OnComm事件
    MSComm1.PortOpen = True ' 打开端口。
    
    
   Label9.Caption = "OK"
Else
' 关闭串行端口。
    MSComm1.PortOpen = False
     Label9.Caption = "False"
End If
End Sub
'''''初始数据'''''''
Private Sub Form_Load()
Text1.Text = "发送区 Action"
Text2.Text = "接收区 Action"
End Sub'''''读取数据'''''''
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent      '事件状态.
Case 1
    Text2.Text = "在传输缓冲区中有比 Sthreshold 数少的字符。"
Case 2      '当MSCOMM1.COMMEVENT的值时收数据
    Text2.Text = Text2.Text & vbLf & MSComm1.Input、
end select
End Sub
我希望每次点击一下鼠标Text2就显示从串口com4接收的数据并换一行
点三次的结果为:
接收区 Action发送区 Action
发送区 Action
发送区 Action问题是第一行没有换行,正确应该如何改?

解决方案 »

  1.   

    照你这样改点三次的结果是:
    接收区 Action

    送区 Action

    送区 Action
      

  2.   

    Text2.Text & MSComm1.Input+chr(13)+chr(10)即可
      

  3.   

    三楼的大虾我照你的改了
    点三次的结果是:
    接收区 Action发
    送区 Action

    送区 Action

    送区 Action
    还是不对
      

  4.   

    先清下text2
    text2.text=""
    text2.text=Text2.Text & MSComm1.Input+chr(13)+chr(10)
      

  5.   

    先清下text2 
    text2.text="" 
    text2.text=Text2.Text & MSComm1.Input&chr(13)&chr(10)
    +可能不行
      

  6.   


    “接收区 Action”这个 我想要保留这个做第一行text2 
    能不能不清除?
      

  7.   

    发送端发过来的就是“发送区 Action”?1
    MSComm1.RThreshold = 132
    Text2.Text = Text2.Text & vbCRLf & MSComm1.Input
      

  8.   

    还是有问题。
    结果变成了:
    接收区 Action
    发送区 Action发送区 Actio
    n
    发送区 Action大哥们帮忙呀
    难到就做不到?
    的结果
    接收区 Action
    发送区 Action
    发送区 Action
    发送区 Action
      

  9.   


                        .txtTemp.Text = .txtTemp.Text & "你的文本" & VBA.vbCrLf
                        .txtTemp.SelStart = Len(.txtTemp.Text)
                        .txtTemp.SelLength = 0
      

  10.   

    依旧有问题变成了:
    接收区 Action发送区 Action
    发送区 Acti
    on
    发送区 Ac
      

  11.   

    初始化的时候
    Private Sub Form_Load()Text1.Text = "发送区 Action"
    Text2.Text = "接收区 Action"
    End Sub
    以后发的时候就是
    MSComm1.Output = Text1.Text & vbCr
      

  12.   

    你一下了发了这么一长串,接收又设为每次接收一个字符,那变不能在每次接收时都写Text2了。
    正确的做法是:
    用一个字符串变量保存接收到的字节,判断接收到回车符时再更新Text2。'以下写在所有函数体外
    Dim str As String'在Form_Load()中
    str = ""'在接收函数中定义
    Dim rChr As String,Case 2      '当MSCOMM1.COMMEVENT的值时收数据
        rChr = MSComm1.Input    
        If (rChr = vbCr) Then
          Text2.Text = Text2.Text + vbCrLf  + str
          str = "" 
        Else
          str = str + rChr 
        End If