0

I am getting junk values in textbox when connected to the comm port is it because my bitrate is wrong, then how could I identify the correct bit rate? I am using VB6 to get data from comm port to which a micro controller is connected

Here I have written code for receiving data at a bitrate of 19200. I just wanted to know how to avoid the junk values but it seems that the bitrate of my microcontroller is 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
dsolimano
  • 8,870
  • 3
  • 48
  • 63
user1963933
  • 11
  • 1
  • 4
  • 12

2 Answers2

1

Quite simply you will need to know what bit rate, stop bits and parity setting your controller is using. You could do this by trial and error by looping through all possible permutations) but it would be quicker to contact the vendor of the controller software and ask them.

However I would try 9600,8,N,1 to start with.

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • if am using trial and error,what should i try to vary the most – user1963933 Apr 09 '13 at 08:56
  • should i change inputlen too? – user1963933 Apr 09 '13 at 08:58
  • @user1963933 the baud rate can vary. Most devices use 8,N,1. InputLen has no effect on the integrity of the received data and just limits how much you receive on each read form the control. – Deanna Apr 09 '13 at 09:11
  • 1
    it could also be that the data you receive is correct, but encoded so you cant read it directly. also: you will receive some data (a few characters) when you connect a cable to the commport while it is listening, this data is no real data but probably comes from connecting the pins. wait a little for the real data to arrive – Hrqls Apr 09 '13 at 11:44
  • Matt Wilko i used the 9600,8,N,1,but still junk values are coming but am getting correct values in simulator – user1963933 Apr 10 '13 at 05:37
  • @Deanna can i give any values for inputlen.am connecting a microcontroller to comm port.vb is intrefacing the comm port – user1963933 Apr 10 '13 at 05:40
  • http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3440221-bring-back-classic-visual-basic-an-improved-versi – user1963933 Apr 10 '13 at 06:22
  • @user1963933 You can, but the `InputLen` property has no effect on how it will receive data from the device. – Deanna Apr 10 '13 at 08:01
  • @Deanna am getting output in simulators perfectly but i am getting junk values in textbox of vb applcation – user1963933 Apr 10 '13 at 08:56
  • I gave the settings 9600,8,N,1 in simulator and its perfect ,but the same settings in vb is still giving me junk values – user1963933 Apr 10 '13 at 08:57
  • @hqrls sir i think you are right,i want to create a delay in reading the comm port how can i do that? – user1963933 Apr 16 '13 at 04:29
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
user1963933
  • 11
  • 1
  • 4
  • 12