2013-02-15 61 views
1

我在VB.NET中创建一个超类型应用程序,并且串行连接有问题。当我向设备发送数据时,我收到了我期待的响应,但在响应中有额外的CR。也就是说,如果我在超级终端中发送命令,我会得到如下响应:串行连接回车

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 
+0

也许一个额外的换行,而不是,你就必须从字符串中删除它。使用SerialPort.ReadLine()而不是ReadExisting(),这样更加高效并且使过滤更容易。包括设置NewLine属性,所以你必须根本不做任何过滤。 – 2013-02-15 22:23:44

回答

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 
+0

嗨,不幸的是,我已经尝试过,但它仍然是一样的!不管怎么说,还是要谢谢你 – 2013-02-16 00:13:00