CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2004
    Posts
    132

    Help with MsComm

    Hi,

    I have a new digital compass device as seen here http://www.oceanserver-store.com/osevkitsorus.html. It outputs a string of characters at a rate up to 40hz. I can change the format of the output string, the simplest being a comma delimited string ending with <cr><lf>. The string length will vary. The following is an example of the output from a terminal showing yaw, pitch roll:-
    167.5,-4.7,7.4
    167.5,-4.7,7.4
    167.5,-4.7,7.4
    167.5,-4.7,7.4
    167.5,-4.7,7.4
    167.5,-4.7,7.4
    167.4,-4.7,7.4
    167.4,-4.7,7.4
    167.4,-4.7,7.4
    167.4,-4.7,7.4
    I am trying to get this data into a VB program and split it up at the commas. I have the MsComm setup:-
    Code:
    MSComm1.Settings = "115200,N,8,1"
    MSComm1.InputLen = 1
    MSComm1.RThreshold = 1
    and the MSComm1_OnComm() sub is:-
    Code:
    Private Sub MSComm1_OnComm()
        Dim InChar As String * 1
        If MSComm1.CommEvent = comEvReceive Then
            Do
                InChar = MSComm1.Input
                If InChar = vbLf Then
                    ' not splitting it up yet, just viewing it in a textbox
                    Text1.Text = Text1.Text & InString
                Else
                    InString = InString & InChar
                End If
             Loop While MSComm1.InBufferCount
        End If
    End Sub
    The results I get in the textbox are like this:-
    167.3,-4.7,7.2
    167.3,-4.7,7.2
    167.3,-4.7,7.2
    167.3,-4.7,7.2
    7.2
    7.2
    7.2
    7.2
    7.2
    7.2
    167.3,-4.7,7.2
    7.2
    167.3,-4.7,7.2
    167.3,-4.7,7.2
    167.3,-4.7,7.2

    Can anyone see why I am only getting the tail end half of the time? I am hoping someone can help me fix this code so I get all of the string on every line so I can easily split it up.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help with MsComm

    Why is the buffer only 1 character? You should use a dynamic string, and then use Instr() to check for vbcrlf
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2004
    Posts
    132

    Re: Help with MsComm

    Quote Originally Posted by dglienna
    Why is the buffer only 1 character? You should use a dynamic string, and then use Instr() to check for vbcrlf
    Even though I have been playing with VB6 for quite along time, my understanding is pretty poor. The original code I copied from this forums somewhere, where someone had a similar query.

    To be honest, I have no idea how to use a dynamic string as you suggested. I'll do some searching. Thanks, you are always a great help to me.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help with MsComm

    Look at this: Turns out to be the InBufferSize property of MSComm1, set to 1024, and InputLen set to 0

    Code:
    Option Explicit
    
    ' Select the right COM port in the Properties of the control!
    
    Private Sub Command1_Click()
      MSComm1.PortOpen = True
      MSComm1.Output = "AT" & Chr$(13)
      Do
        DoEvents
      Loop Until MSComm1.InBufferCount > 1
      Text1.Text = MSComm1.Input
    End Sub
    or from MSDN:

    Code:
    Private Sub Form_Load ()
       ' Buffer to hold input string
       Dim Instring As String, Buffer as String
       ' Use COM1.
       MSComm1.CommPort = 1
       ' 9600 baud, no parity, 8 data, and 1 stop bit.
       MSComm1.Settings = "9600,N,8,1"
       ' Tell the control to read entire buffer when Input
       ' is used.
       MSComm1.InputLen = 0
       ' Open the port.
       MSComm1.PortOpen = True
       ' Send the attention command to the modem.
       MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that 
       ' the modem responds with "OK".
       ' Wait for data to come back to the serial port.
       Do
          DoEvents
       Buffer$ = Buffer$ & MSComm1.Input
       Loop Until InStr(Buffer$, "OK" & vbCRLF)
       ' Read the "OK" response data in the serial port.
       ' Close the serial port.
       MSComm1.PortOpen = False
    End Sub
    Last edited by dglienna; March 11th, 2008 at 08:09 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2004
    Posts
    132

    Re: Help with MsComm

    Thanks, that helped me sort it out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured