使用comm端口在文本框中接收的垃圾数据

时间:2013-04-09 07:16:00

标签: vb6 serial-port microcontroller bitrate

当连接到comm端口时,我在文本框中收到垃圾值,因为我的比特率是错误的,那我怎么能识别正确的比特率?我正在使用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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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