串行连接回车

时间:2013-02-15 22:08:52

标签: vb.net serial-port carriage-return

我正在VB.NET中创建一个Hyperterminal类型的应用程序,并且串行连接有问题。当我向设备发送数据时,我得到了我期待的响应,但在响应中有一个额外的CR。即如果我在Hyperterminal中发送命令,我会得到一个响应,例如;

response one
response two

但在我的应用程序中它会返回;

response one

response two

代码如下;

Private Sub textSend_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles textSend.KeyPress

        If e.KeyChar = Convert.ToChar(Keys.Enter) Then 

            connSerial.Write(textSend.Text & vbCr)
            textSend.Text = ""

            e.Handled = True 

        End If

    End Sub

Private Sub connSerial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles connSerial.DataReceived
        ReceivedText(connSerial.ReadExisting()) 
    End Sub
    Private Sub ReceivedText(ByVal [text] As String)
        If Me.textReply.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            textReply.AppendText([text])
        End If

    End Sub

我使用1200波特率,实际上每一行都被另一行分开,但是如果我将波特率提高到57600,它只会偶尔发生。即;

response 1
response 2
response 3

response 4
response 5

1 个答案:

答案 0 :(得分:0)

您可以使用WriteLine Method代替Write Method

   If e.KeyChar = Convert.ToChar(Keys.Enter) Then 
        connSerial.WriteLine(textSend.Text & vbCr)
        textSend.Clear();
        e.Handled = True 
   End If