0

连接到通讯端口时,我在文本框中收到垃圾值是因为我的比特率错误,那么我如何识别正确的比特率?我正在使用 VB6 从连接微控制器的通信端口获取数据

在这里,我编写了用于以 19200 的比特率接收数据的代码。我只是想知道如何避免垃圾值,但似乎我的微控制器的比特率是 19200

Private Sub Command1_Click()
  If (MSComm1.PortOpen = False) Then  opening port
    MSComm1.PortOpen = True
  End If
  Command1.Enabled = False
  Command2.Enabled = True
End Sub

Private Sub Command2_Click()
  If (MSComm1.PortOpen = True) Then
    MSComm1.PortOpen = False
  End If
  Command1.Enabled = True
  Command2.Enabled = False
End Sub

Private Sub Form_Load()
  With MSComm1
    .CommPort = 1            
    .RThreshold = 1
    .RTSEnable = True
    .Settings = "19200,N,8,1"  
    .InputLen = 1000
    .SThreshold = 1  
    .PortOpen = True
  End With
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If (MSComm1.PortOpen = True) Then
    MSComm1.PortOpen = False
  End If
End Sub

Private Sub MSComm1_OnComm()
Dim Buffer As String

  Select Case MSComm1.CommEvent    
    Case comEvReceive
      Text1.Text = " "
      Buffer = MSComm1.Input
      Text1.Text = Text1.Text & Buffer
  End Select
End Sub
4

2 回答 2

1

很简单,您需要知道控制器使用的比特率、停止位和奇偶校验设置。您可以通过循环遍历所有可能的排列来通过反复试验来做到这一点)但是联系控制器软件的供应商并询问他们会更快。

但是我会尝试 9600,8,N,1 开始。

于 2013-04-09T08:10:20.090 回答
0
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
If (MSComm1.PortOpen = False) Then
MSComm1.PortOpen = True
End If
Command1.Enabled = False
Command2.Enabled = True
End Sub

Private Sub Command2_Click()
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
Command1.Enabled = True
Command2.Enabled = False
End Sub

Private Sub Command3_Click()
Text1.Text = " "
End Sub

Private Sub Form_Load()

MSComm1.CommPort = 1
  MSComm1.Settings = "9600,N,8,1"
  MSComm1.DTREnable = True
  MSComm1.Handshaking = comRTS
  MSComm1.InBufferSize = 1
  MSComm1.RThreshold = MSComm1.InBufferSize
  MSComm1.RTSEnable = True
  MSComm1.InputLen = 1
  MSComm1.InputMode = comInputModeText
  MSComm1.NullDiscard = True
  MSComm1.OutBufferSize = 0
  MSComm1.SThreshold = MSComm1.OutBufferSize

  MSComm1.PortOpen = True

End Sub

Private Sub Form_Unload(Cancel As Integer)
If (MSComm1.PortOpen = True) Then
MSComm1.PortOpen = False
End If
End Sub

Private Sub MSComm1_OnComm()
Dim Buffer As String

Select Case MSComm1.CommEvent
Case comEvReceive

Text1.Text = " "
Buffer = MSComm1.Input
Text1.Text = Text1.Text & Buffer
End Select
End Sub
于 2013-04-17T04:13:47.577 回答