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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 84
How to debug this code 7 65
clicking a shape in a frame array vb6 3 48
VBA: copy range dynamically based on config sheet v2 3 55
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 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