dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Transfer content from buffer to variable

  1. #1
    Join Date
    Jul 2011
    Posts
    5

    Unhappy Transfer content from buffer to variable

    Transfer value from VB input (RS232/USB) to a string variable.

    For years I have been using the same to code to read a measuring instrument connected to a multiplexer. All of a sudden I hit a snag.

    The instrument is connected to a USB port. Using a Poll string the instrument is instructed to send the reading to the PC. (USBMux.Output = strPollString)

    Thereafter VB6 (SP6) is instructed to read the input buffer. (strUSBMux = USBMux.Input)
    By halting the execution of the script on the red line below one can read the contents of the
    USBMux.Input buffer. This buffer contains the value transmitted by the instrument, however the variable “strUSBMux” remains empty. (Dim strUSBMux as String)

    I have never come across this problem before. I can’t figure out why the variable remains empty while the buffer USBMux.Input actually contains a value.

    sngBegin = Timer
    USBMux.Output = strPollString
    Do
    Loop Until Timer - sngBegin >= 0.1!
    USBMux.DTREnable = True
    Do
    sngEinde = Timer
    DoEvents
    strUSBMux = USBMux.Input
    DoEvents
    lPos1 = InStr(1, strUSBMux, Chr$(13))
    !!! Execution of script is halted here to be able to read the contents of the variables.!!!
    If lPos1 > 0 Then
    Exit Do
    End If

    Any suggestion will b highly appreciated.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Transfer content from buffer to variable

    I suppose you are using the MSComm control.
    So, why don't you use the OnComm() event of the control to get the data?
    like
    Code:
    Dim strUSBMux as String
    Private Sub USBMux_OnComm()
      static strIn$, p%
      strIn = strIn + USBMux.Input
      p = inStr(strIn, vbCr)  
      If p Then strUSBMux = strIn: strIn = ""
    End Sub
    You have to set USBMux.RThreshold to 1, though.
    You could then sit out in a loop to wait for input to complete:
    Code:
      Do
        DoEvents
      Loop Until strUSBMux <> ""

  3. #3
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,329

    Re: Transfer content from buffer to variable

    Rather than a loop would be better to use a timer, Within the timer code check the inbuffercount when it is greater than 0 there is something there to read.

    Make sure to use a form level or static variable so you can keep appending data as it arrives until you have the complete transmission.


    The reason your varibale is blank is that you are overwriting it with every read so it is possible that when you loop exits the only thing in your var is the CR you are checking for everthing prior to that could have been overwritten by your code. You must append the data to your var as it arrives.
    Last edited by DataMiser; December 16th, 2011 at 04:38 PM.
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Transfer content from buffer to variable

    Which works perfectly when using the OnComm() event to append incoming data.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)