funny looping and serial data problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sonoman

    #1

    funny looping and serial data problem

    Hello everyone:
    I am new to Visual Basic and I have a problem that I cannot put my
    finger on. I am working on a school project and I am getting some serial
    data from a microprocessor. I have designed my "Do" loop to wait for
    each data set coming in at intervals of about 1/2 a second from the
    microprocessor.

    When I comment the following lines:

    bufferVar = ""
    myBufferData = ""
    myDataString = ""

    in my code below I get the correct serial data set the first time around
    and after that it repeats the same data in the textbox however many
    times determined by the "for" loop with only one serial data delay (1/2
    a second). BUT if I uncomment these lines I only get the last data set
    once after the correct delay determined by the for loop. So, if I have 6
    serial data sets my program will run for 3 seconds and display only the
    last data set.

    What I would like it to do is to print each data set as it arrives for a
    long period of time. Any Ideas? Thanks in advance.


    Here is my code:


    Private Sub Command1_Click( Index As Integer)

    'setup the port
    If MSComm1.PortOpe n = False Then MSComm1.CommPor t = 5
    MSComm1.Setting s = "9600,N,8,1 "
    MSComm1.PortOpe n = True
    MSComm1.InputMo de = comInputModeTex t

    For i = 1 To 3
    'reset data to enter clean every time
    'endFlag used to find out when buffer has one full data set
    endFlag = False
    'MSComm1.InBuff erCount = 0 to start with empty buffer every time
    MSComm1.InBuffe rCount = 0
    'start clean every time to extract serial data
    'OK, if I comment these three variables I get only the first serial
    data set
    'repeated however many times is indicated in the "for" loop
    'BUT, if I uncomment these three variables I get only the last data set
    'with the correct delay (1/2 a sec per iteration of "for" loop)
    bufferVar = ""
    myBufferData = ""
    myDataString = ""

    setEnd = 0

    'loops until "last item" marker is found
    'and sets location value to setEnd
    Do
    myBufferData = myBufferData & MSComm1.Input
    endFlag = InStr(myBufferD ata, ",")
    Loop Until endFlag = True

    'locates last item marker on data set
    setEnd = InStr(myBufferD ata, ",")

    'gets location markers for x, y and z
    xPos = InStr(myBufferD ata, "x")
    yPos = InStr(myBufferD ata, "y")
    zPos = InStr(myBufferD ata, "z")

    'builds buffer data set string
    'used to print the variable locations to the screen, not important
    bufferVar = bufferVar & "xPos = " & xPos & vbCrLf
    bufferVar = bufferVar & "yPos = " & yPos & vbCrLf
    bufferVar = bufferVar & "zPos = " & zPos & vbCrLf
    bufferVar = bufferVar & "setEnd = " & setEnd & vbCrLf

    myDataString = myDataString & "x = " & Mid(myBufferDat a, (xPos +
    1), (yPos - xPos - 1)) & " "
    myDataString = myDataString & "y = " & Mid(myBufferDat a, (yPos +
    1), (zPos - yPos - 1)) & " "
    myDataString = myDataString & "z = " & Mid(myBufferDat a, (zPos +
    1), (setEnd - zPos - 1)) & vbCrLf

    'write to textBoxes
    txtData.Text = myDataString
    txtBuffer.Text = bufferVar

    Next i

    'when done close com port
    If MSComm1.PortOpe n = True Then MSComm1.PortOpe n = False
    End Sub
  • mickey

    #2
    Re: funny looping and serial data problem

    On Mon, 25 Oct 2004 20:46:12 -0400, Sonoman <billgates@micr osoft.com>
    you typed some letters in random order:

    What do you expect to receive in to the comport exactly ?

    If you know how long the string is you are receiving,then try using
    the 'MSC_OnComm()' event.

    This event gets triggered when the input buffer reaches a certain
    size. The size can be set on the 'Rthreshold' property

    perhaps this helps




    Groetjenz,

    Mickey
    --
    #### gewoan skrieve su ast ut seist ####

    Comment

    • Sonoman

      #3
      Re: funny looping and serial data problem

      mickey wrote:[color=blue]
      > On Mon, 25 Oct 2004 20:46:12 -0400, Sonoman <billgates@micr osoft.com>
      > you typed some letters in random order:
      >
      > What do you expect to receive in to the comport exactly ?
      >
      > If you know how long the string is you are receiving,then try using
      > the 'MSC_OnComm()' event.
      >
      > This event gets triggered when the input buffer reaches a certain
      > size. The size can be set on the 'Rthreshold' property
      >
      > perhaps this helps
      >
      >
      >
      >
      > Groetjenz,
      >
      > Mickey[/color]

      Thank you for your response. I have the Do loop because the length of
      the string varies from 7 to 13 characters randomly. Like I said before
      this is my first time using VB, so I will look into that MSC_OnComm()
      function to see if I could use it.

      Anyway, I don't think that is going to solve my data displaying problem.
      I spoke to one of my professors today and he pointed out something
      interesting about the way I am building my strings. I did not really
      understand him then but now I know where to look.

      Comment

      Working...