MSComm control input property

Posted on 1997-08-25
Last Modified: 2008-03-03
I want to use the MSComm active X control to get some data from a serial device.  I know that the device is sending me data because I have another device that lets me look at the data comming through the serial port directly, therefore, the availability of data is not an issue.
I have defined the input buffer to be of 1000 bytes. I also defined the inputlen property to be 0 so that I can read the entire contents of the buffer at once.
Here is the problem: when I define the input to be text, the input property only retrieves a few characters from the buffer eventhough I know the buffer has 1000 characters by looking at the inputbuffercount property.  The number of characters retrieved vary from 1 to 9; no more, no matter what I define the inputlen property to be.
If I define the input to be binary, the input property only retrieves half the buffer, no more, no matter what the buffer size is.
How can I retrieve the whole buffer?
P.S. By the way, my settings are 9600,n,8,1
Question by:pizarro
  • 2
  • 2

Accepted Solution

ChrisLewis earned 200 total points
ID: 1432151
You shouldn't have to deal with your InputBufferSize.  Try leaving it at default (1024) rather than messing with it.  I'm going to be stupid and just give you some generals on the MSComm control.

How are you retrieving your information? There are two ways to retrieve information from the Comm control, polling and event driven.

For polling, you set up a do loop and read characters.  This is useful if you are reading a set string from the comm port, or a string that is terminated with a character sequence.  Just loop until a set numer of characters are retrieved, or the terminating string is recieved.

Polling is easier if you are causing the comm device to send you information.  You send the command to start information, then retrieve it as it comes to you.

MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
MSComm1.PortOpen = True
MSComm1.Output = "RUN PROGRAM" + Chr$(13)
'Here's where we do our retrieval...
'-----+----- for a set number of characters...
Loop Until MSComm1.InBufferCount >= 20  'wait for 20 characters
Instring = MSComm1.Input
MSComm1.PortOpen = False

- or -
'-----+----- Waiting for a terminating string...
Instring = ""
  Instring = Instring & MSComm1.Input
Loop Until instr(Instring,"+++END+++") > 0
MSComm1.PortOpen = False

For event driven, you set the RThreshold property to indicate how many characters of information cause an event.

sub form_load
  MSComm1.CommPort = 1
  MSComm1.Settings = "9600,N,8,1"
  MSComm1.RThreshold = 1
  MSComm1.PortOpen = True
  MSComm1.Output = "RUN PROGRAM" + Chr$(13)
End sub

Private Sub MSComm_OnComm ()
  Select Case MSComm1.CommEvent
    ' Handle each event or error by placing
    ' code below each case statement

    ' Errors - CHECK FOR THESE, and fix as neccessary...
    Case comEventBreak      ' A Break was received.
    Case comEventCDTO      ' CD (RLSD) Timeout.
    Case comEventCTSTO      ' CTS Timeout.
    Case comEventDSRTO      ' DSR Timeout.
    Case comEventFrame      ' Framing Error
    Case comEventOverrun      ' Data Lost.
    Case comEventRxOver      ' Receive buffer overflow.
    Case comEventRxParity      ' Parity Error.
    Case comEventTxFull      ' Transmit buffer full.
    Case comEventDCB      ' Unexpected error retrieving DCB]

    Case comEvCD      ' Change in the CD line.
    Case comEvCTS      ' Change in the CTS line.
    Case comEvDSR      ' Change in the DSR line.
    Case comEvRing      ' Change in the Ring Indicator.
    Case comEvReceive      ' Received RThreshold # of
                  ' chars.
      'Grab your characters here...
      INSTRING = INSTRING & MSComm1.Input
      IF LEN(INSTRING) >= 20 THEN 'Deal with fixed string
     END IF
    ' - OR -
    IF instr(Instring,"+++END+++") > 0  THEN
    END IF

    Case comEvSend      ' There are SThreshold # of
                  ' characters in the transmit
                  ' buffer.
    Case comEvEof      ' An EOF charater was found in
            ' the input stream
  End Select
End Sub

PROCESS_INSTRING_HERE would send a stop command to your comm device, or whatever you need to do next.

Event driven has one big advantage in that you can be doing other things while waiting for your comm device.

I wrote a program to read analog temp sensors, and toyed with both methods.  I ended up using polling because my return information was small enough to not need event driven processing.

Hope this helps,

Author Comment

ID: 1432152
The answer is very complete; however, the problem that I am facing is that whenever I invoke one of the methods you explained, I don't get the contents of the whole buffer.
For example, when I use polling, I do it so that the control reads 20 characters, but when I use the input method to retrieve the contents, I only get from 1 to 9 characters.  i.e. although the inbuffercount property says there are 20 characters in the receive buffer, I only get a few when I use the input method.
This happens when I am expecting the receive buffer to have text characters.
When I define the control so that I'm expecting binary characters, I only get half the size of the buffer.  Why is this?

Expert Comment

ID: 1432153
It's interesting that InputBuffer has 20 characters, yet input is only showing from 1 to 9 characters.  Could you be getting some null characters or CHR$(0)'s that are terminating the VB string early?  

What exactly are you expecting from the Comm Device.

With BinaryMode, have you tried setting the input buffer to twice your need and gotten the full string?  Could your comm device have a different size byte that VB? Could you be looking at UniCode characters?  (Most likely not...)


Author Comment

ID: 1432154
Forget the comment above. This works like a charm.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question