Gracias
--
Gustavo Mejia A.
Medellin, Colombia
-------
http://www.fortunecity.com/skyscraper/solomon/517/rest/mscomm.htm
Troubleshooting Tips for the MSComm Control
The information in this article applies to:
- Standard, Professional, and Enterprise Editions of Microsoft
Visual
Basic, 16-bit and 32-bit, for Windows, version 4.0
- Standard and Professional Editions of Microsoft Visual
Basic, version
3.0, for Windows
SUMMARY
The MSComm control encapsulates much of the functionality
provided by the communications functions of the
Windows API. This encapsulation makes these functions easier
to use but does limit the functionality of the
MSComm control. Here are some tips for troubleshooting the
MSComm control.
MORE INFORMATION
1.If you are using Visual Basic 3.0, make sure that you are
using the updated MSComm.VBX, dated 5/12/93.
If you do not have the updated MSComm.VBX, you can
download MSComm.Exe, a self-extracting file,
from the following services:
- Microsoft's World Wide Web Site on the Internet
On the www.microsoft.com home page, click the Support
icon Click Knowledge Base, and search
for Mscomm.exe Open the article, and click the button to
download the file - Internet
(anonymous FTP) ftp ftp.microsoft.com Change to the
Softlib\Mslfiles directory Get Mscomm.exe
- The Microsoft Network On the Edit menu, click Go To,
and then click Other Location Type
mssupport Double-click the MS Software Library icon Find
the appropriate product area
Download Mscomm.exe - Microsoft Download Service (MSDL)
Dial (206) 936-6735 to connect to
MSDL Download Mscomm.exe For additional information,
please see the following article(s) in
the Microsoft Knowledge Base: ARTICLE-ID: Q101944 TITLE :
UPD: New MSCOMM Control Available
2.If you are using Windows for Workgroups 3.11, obtain the
updated version of SERIAL.386 by
downloading WG1001.Exe, a self-extracting file, from the
Microsoft Software Library (MSL) on
the following services:
- Microsoft's World Wide Web Site on the Internet
On the www.microsoft.com home page, click the Support
icon Click Knowledge Base, and search
for WG1001.EXE Open the article, and click the button to
download the file - Internet
(anonymous FTP) ftp ftp.microsoft.com Change to the
Softlib\Mslfiles directory Get WG1001.EXE
- The Microsoft Network On the Edit menu, click Go To,
and then click Other Location Type
mssupport Double-click the MS Software Library icon Find
the appropriate product area
Download WG1001.EXE - Microsoft Download Service (MSDL)
Dial (206) 936-6735 to connect to
MSDL Download WG1001.EXE ARTICLE-ID: Q112418 TITLE : BUG:
Serial Port Driver for WFW 3.11
Sends Extra Byte
3.Use the Windows Accessories Terminal application
(HyperTerminal in Windows 95) to determine
if a connection can be established independent of Visual
Basic. This will determine if a
connection can be made using only the communication
functions in the Windows API. If a
connection can not be made, examine the physical elements
of the connection. Is the proper
cable being used? Is it connected to the serial port? If
a connection can be made, note the
settings that were used and use the same settings with
the MSComm control.
4.Use the Visual Basic VBTerm and Dialer sample
applications with the appropriate settings. If
these work, use this code as a starting point for your
application. If they don't work, first
try the suggestions below on these samples. If these
suggestions fix the problem, then use
the samples as a starting point. Otherwise start with the
simplest code possible and use the
following suggestions as a starting point.
5.Set the RTSEnable property of the MSComm control to True.
Many modems and other communication
devices use the RTS signal for handshaking.
6.If the Visual Basic application using the MSComm control
is running on a Windows for
Workgroups 3.11 machine with a 16550AF UART chip and the
InBufferSize property is greater
than 1024, COMxFIFO=0 must be added to the [386Enh]
section of the System.ini file. This
entry disables the FIFO buffer of COMx's 16550 UART. If a
serial port does not have a 16550
UART, this setting is ignored. The default value is 1
(enabled).
7.Set the RThreshold property to 1. If you set the
RThreshold property to any other value, you
may have trouble unless all of your data consists of
fixed-length packets.
8.Set the Interval property to 55 for best performance.
(The default of 1000 is too long.)
9.Set the InputLen property to 0 unless there is an
overriding reason not to.
10.Always receive data when data is available. Don't rely on
the
RThreshold property to tell you if data is available.
Use the OnComm
event procedure. Double-buffer the incoming data.
Extract the data from
the buffer and process it. Clean up the buffer to
remove the processed
data. For example:
Sub Comm1_OnComm ()
Static ReceiveBuffer As String
ReceiveBuffer = ReceiveBuffer & Comm1.Input
'Always buffer incoming data no matter what
generated the OnComm
'event.
If Len(ReceiveBuffer >= Limit) Then
Call Process(Left$(ReceiveBuffer,Limit)) ReceiveBuffer =
Right$(ReceiveBuffer,
Len(ReceiveBuffer) - Limit) 'Cleans-up buffer. End If End
Sub 'If you are looking for a
terminating character sequence instead of 'length, then
the test would use InStr instead of
Len. This procedure double-buffers the received data and,
unless the program is bound to fail
because of some other performance problems, provides good
security for the received data. It
is extremely important to handle every character as it
comes in at high baud rates.
REFERENCES
If the MSComm control is not satisfactory, the communication
APIs available in Windows may provide
the necessary solution. Daniel Appleman's "Visual Basic
Programmer's Guide To The Windows API" book
is an excellent resource for the 16-bit communications APIs as
is the VBComDem sample (Q75856). For
information on using the 32-bit communications APIs see
"Create Communications Programs for Windows
95 with the Win32 Comm API" in the December 1994 issue of the
Microsoft Systems Journal.
How To Send to the Serial Port by Using Mscomm32.ocx
The information in this article applies to:
Microsoft Visual FoxPro for Windows, versions 3.0, 5.0
SUMMARY
This article describes the settings necessary to send data to
the serial port by using Mscomm32.ocx.
Mscomm32.ocx ships with the Professional Edition of Visual
FoxPro. It can be used in Windows 95 or Windows
NT only.
MORE INFORMATION
The most frequently used properties to send data to the serial
port using the mscomm control are as follows:
CommPort
The CommPort property specifies the communications port
number. A numeric property that corresponds to the
Comm port. By default, this property is set to 1 corresponding
to com1. Valid values are 1, 2, 3, or 4 depending on
the serial ports installed on the computer and their
configuration.
Settings
The Settings property configures the baud rate, parity, data
bits, and stop bits for the serial port. It is a character
string with individual values separated by commas. By default,
the Settings property contains 9600,N,8,1. This
corresponds to 9600 baud, no parity, 8 data bits, and 1 stop bit.
PortOpen
The PortOpen property specifies a logical value that controls
whether or not the serial port is open and active.
Once the previous properties are set to begin using the serial
port, you can set this property to true.
Output
The Output property is assigned the string of characters to be
sent to the serial port. To output the string "Hello
World" to the serial port after the previous properties are
set, use the following command:
myform.mycomm.output = "Hello World"
Other properties that may be also be needed depending on the
application are as follows:
CommEvent
The CommEvent property contains a value that represents the
most recent communications event or errors.
Sthreshold
The Sthreshold property specifies the minimum number of
characters in the output buffer that are sent.
OutBufferCount
The OutBufferCount control returns the number of characters
waiting in the transmit buffer. This should always
be zero if the Sthreshold property is zero. Setting the
OutBufferCount property to zero will clear the transmit
buffer.
OutBufferSize
The OutBufferSize property specifies the size of the transmit
buffer. By default, this buffer is 512 bytes. The
larger the transmit buffer, the less memory available to other
applications. Slow baud rates and large text strings
written to the serial port may mean you need to make this
value larger.
Following is a sample that shows how to set up the comm
control and dial the phone number 555-1234 by using
the standard Hayes Modem commands.
PUBLIC ComForm
ComForm = CREATEOBJECT('Form')
ComForm.AddObject("Testcom","Olecontrol","MSCOMMLib.MSComm")
ComForm.Testcom.CommPort = 2 && Use Comm2, The second
Serial Port.
ComForm.Testcom.Settings = "14400,N,8,1" && 14.4 Kbaud, No
Parity,
&& 8 data Bits, 1
Stop Bit
ComForm.Testcom.PortOpen = .T.
ComForm.Testcom.Output = "ATDT555-1234" + chr(13) &&
Dialing the number
* The chr(13) is needed to complete the modem command sequence
ComForm.Testcom.PortOpen = .F.
***** End Code *****
How To Receive from the Serial Port by Using MScomm32.ocx
The information in this article applies to:
Microsoft Visual FoxPro for Windows, versions 3.0, 5.0
SUMMARY
This article gives you two techniques you can use to receive
data from the serial port using the Mscomm32.ocx
control. The first uses an event-driven method and does not
require you to poll the serial port to check for the
presence of received characters. This technique allows the
most flexibility and does not require extensive coding
to prevent buffer over-runs. The second technique requires you
to poll the input buffer periodically to check for
the presence of received characters. This article describes
these two techniques and provides examples for each.
MORE INFORMATION
You need to set the following properties regardless of which
technique you use:
CommPort property: Set this numeric property to the
desired communications port. Valid values are 1, 2,
3, or 4 depending on the serial ports available and the
configuration of the individual computer. These
values correspond to Com1, Com2, Com3, and Com4
respectively.
Settings property: Set this character property to the
baud rate, Parity, Data Bits, and Stop Bits required
by the device connected to the serial port. This property
is a character, comma-delimited list. For example,
to set the serial port to 14,400 baud, Even Parity, 7
Data Bits, and 1 Stop Bit, set the string to:
14400,E,7,1
PortOpen property: Set this logical property to true to
open communications to the serial port. You can
also check this property to determine if the port opened
correctly.
Technique One: Event-Driven Receive
The event Driven technique generates an OnComm event when
there are characters waiting in the input buffer.
Also, the CommEvent property will contain a numeric 2. For the
OnComm event to be triggered, you must set the
Rthreshold property to a value other than zero (its default).
The most common setting for the rthreshold property
is 1, meaning that the OnComm event is triggered if a minimum
of one character is waiting in the input buffer.
For example, the following code may be placed in the OnComm
event to append received data to a property of a
form called mybuffer.
Procedure MyCom.OnComm
IF This.CommEvent = 2
ThisForm.mybuffer = ThisForm.mybuffer + This.Input
ENDIF
ENDPROC
Technique Two: Polling the Input Buffer
Polling the input buffer requires that the program
periodically stop what it is doing and check to see if there are
characters waiting in the input buffer. When using this
technique, leave the Rthreshold property at 0 (its default
value), and check the InBufferCount property to see if it is
greater than zero, which indicates that there are
characters waiting in the buffer.
NOTE: Using a technique such as checking the length of the
Input property results in lost characters because as
soon as the Input property is accessed, the Input buffer is
emptied. Use the InBufferCount property instead.
Assuming the Mscomm control is on the form and has the name
MyCom and that there is a form property named
mybuffer, the following code illustrates how to poll for
waiting characters:
Procedure myform.myproc
IF Thisform.MyCom.InBufferCount > 0
Thisform.mybuffer = Thisform.mybuffer + Thisform.MyCom.Input
ENDIF
ENDPROC
The procedure code could be called in a timer method to
facilitate checking for characters at semi-regular
intervals. However, if large amounts of data are expected to
be received from the serial port, Technique One will
reduce the chance of over-running the input buffer.
Communications Control (MSCOMM32.OCX)
The Communications control, MSCOMM32.OCX, provides serial
communications for your application by
allowing the transmission and reception of data through a
serial port. The Communications control is installed
only if you are running Windows NT or Windows 95.
Syntax: MSComm
The Communications control provides the following two ways to
handle communications:
- Use event-driven communications to handle serial port
interactions. In
many situations, you want to be notified the moment an
event takes
place, such as when a character arrives or a change occurs
in the
Carrier Detect (CD) or Request To Send (RTS) lines. In
such cases, you
can use the Communications control's OnComm event to trap
and handle
these communication events. The OnComm event also detects
and handles
communications errors.
-or-
- Poll for events and errors by checking the value of the
CommEvent
property after each critical function of your program.
This may be
the best method if your application is small and
self-contained.
For example, if you are writing a simple phone dialer, it
may not make
sense to generate an event after receiving every
character, because the
only characters you plan to receive are the OK response
from the modem.
Each Communications control you use corresponds to one serial
port. If you need to access more than one serial
port in your application, you must use more than one
Communications control. The port address and interrupt
address can be changed from the Windows Control Panel.
When you create and distribute applications that use the
Communications control, you should install
MSCOMM32.OCX in the customer's WINDOWS\SYSTEM subdirectory.
The following example, from the Visual FoxPro Help file, shows
how to perform basic serial port communications:
* Use COM1.
Comm1.CommPort = 1
* 9600 baud, no parity, 8 data, and 1 stop bit.
Comm1.Settings = "9600,N,8,1"
* Tell the control to read entire buffer when Input is used.
Comm1.InputLen = 0
* Open the port.
Comm1.PortOpen = .T.
* Send the attention command to the modem.
Comm1.Output = "AT" + CHR(13)
* Wait for data to come back to the serial port.
DO WHILE Comm1.InBufferCount <= 2
* Read the "OK" response data in the serial port.
InString = Comm1.Input
ENDDO
* Close the serial port.
Comm1.PortOpen = .F.
How To Transmit and Receive Binary Data using MSCOMM32
Article ID: Q154741
Creation Date: 14-AUG-1996
Revision Date: 20-JAN-1997
The information in this article applies to:
Microsoft Visual FoxPro for Windows, versions 3.0, 3.0b
SUMMARY
This article illustrates some techniques using the Visual
FoxPro 3.0 Communications (MSComm) control for
receiving and transmitting binary data over RS-232 cable
(without having modems).
MORE INFORMATION
The Communications control, MSCOMM32.OCX, provides serial
communications for your application by
allowing the transmission and reception of data through a
serial port where only a string of characters to the
transmit buffer is permitted. This feature limits you to
transmitting only text- based files.
This article demonstrates how to use the MSComm control to
transmit and receive binary data using RS-232
cable. (The techniques in this article will also work with
modems.) The binary data must be converted one byte at
a time to a character and then transmitted. When received, the
data must be converted from a character back into
binary data one byte at a time.
On DBCS-enabled operating systems (running operating system
software that uses one of the double-byte
character sets), binary data will be corrupted if one of the
binary values matches a DBCS lead character. The
MSComm control will interpret this byte and the following byte
as one double-byte character and return only
one byte for the equivalent ASCII character. To resolve this
problem, you need to convert one byte into ASCII
size of three characters to preserve the lead character.
Sample Code
The following code illustrates this process:
WARNING: USE OF THE SAMPLE CODE PROVIDED IN THIS ARTICLE IS AT
YOUR OWN RISK. Microsoft
provides this sample code "as is" without warranty of any
kind, either express or implied, including but not
limited to the implied warranties of merchantability and/or
fitness for a particular purpose.
* Transmitter Code.
* INIT event of Comm OLE control * 28800 baud, no parity, 8
data, and 1 stop bit. * In RS-232, maximum speed of
28800 baud can be used
This.Settings = "28800,N,8,1"
This.InputLen = 1
This.CommPort = 1
This.PortOpen = .T.
* ONCOMM event of Comm OLE Control * The following code is
needed to make sure that next set of
characters * can be transmitted (CommEvent = 2 is triggered
from the receiver side)
IF This.CommEvent = 2
This.input
IF gnTop <= gnEnd
gcString = FREAD(gnFileHandle, 1) && Store to memory
q=asc(gcstring)
* change ASCII to character (size of 3) to preserve
the lead char
thisform.olecontrol1.output = str(q,3)
gnTop = gnTop + 1
ENDIF
ENDIF
* INIT event of form
PUBLIC gnFileHandle
PUBLIC gnEnd
PUBLIC gnTop
PUBLIC q
* You should replace 'c:\sample.hlp' with your own binary file
STORE FOPEN('c:\sample.hlp') TO gnFileHandle && Open the
file
STORE FSEEK(gnFileHandle, 0, 2) TO gnEnd && Move pointer
to EOF
STORE FSEEK(gnFileHandle, 0) TO gnTop && Move pointer to BOF
gntop=1
q=""
Property of OleControl1
RThreshold = 1 * triggers when at least one char is on the
buffer
SThreshold = 3
* Receiver Code.
* INIT event of OleControl1 * 28800 baud, no parity, 8 data,
and 1 stop bit.
This.Settings = "28800,N,8,1"
This.InputLen = 3
This.CommPort = 1
This.PortOpen = .T.
* OnComm event
IF This.CommEvent = 2 AND This.InBufferCount > 0
qq=CHR(VAL(This.Input))
=FWRITE(gnFileHandle,qq)
this.output = CHR(26)
ENDIF
* INIT event of form
PUBLIC gnFileHandle
* You should replace 'c:\sample.hlp' with your own file name
IF FILE('c:\sample.hlp') && Does file exist?
gnErrFile = FOPEN('c:\sample.hlp',12) && If so, open
read-write
ELSE
gnErrFile = FCREATE('c:\sample.hlp') && If not, create it
ENDIF
= FCLOSE(gnErrFile) && Close the file
STORE FOPEN('c:\sample.hlp',1) TO gnFileHandle && Open
the file
* CLICK event of button
*This tells the transmitting side to start sending the file
Thisform.Olecontrol1.output = CHR(26)
Property of OleControl1
RThreshold = 3
The event-driven technique generates an OnComm event when
there are characters waiting in the input buffer.
Also, the CommEvent property will contain a numeric 2. For the
OnComm event to be triggered, you must set the
Rthreshold property to a value other than zero (its default).
The most common setting for the RThreshold
property is 1, meaning that the OnComm event is triggered if a
minimum of one character is waiting in the input
buffer. In this article, 3 is being used as a value of
RThreshold property because 3 characters at a time are being
sent.
Subject: Communications Control
I am some luck, but mostly trouble using the Communications
Control
(MSCOMM32.OCX)
The following will work once, but not a second time, unless I
turn off my
modem (sportster Vi 28,800), then back on.
Thisform.CommCtrl.CommPort = 1
Thisform.CommCtrl.Settings = "9600,n,8,1"
Thisform.CommCtrl.InputLen = 0
ThisForm.CommCtrl.PortOpen = .T. &&Open Com port
Thisform.CommCtrl.InBufferCount = 0 && Flush the input buffer
Thisform.CommCtrl.Output = "ATDT" + CHR(13) + CHR(10) &&
Produces dial tone
ThisForm.CommCtrl.PortOpen = .F.
If anyone is using the control successfully, am I doing
something wrong ?
Subject: RE: Communications Control
I haven?t tried it in VFP but it works in VB 4.0. The trick is
to close and
reinitialise your port each time. eg:
if <port is open>
close port
endif
open port
send control codes to the modem
do the dialing..
Another trick that might come handy is that when communicating
in a
terminal mode and you want to send data, sending something
will fill up the
receive buffer (because of the echo settings of the terminal)
and you have
to immediately clear the input buffer before receiving any
response.
Subject: RE: Communications Control
Try setting the handshaking property to something other than 0.
Subject: RE: Communications Control
Thanks Eric !
Setting the handshaking property from the default of 0-None to
1-XON/XOFF
didn't work, but setting to 2-RTS/CTS or 3-Both works great. I
can finally
proceed with the application. Thanks again.
Subject: Re: Communications Control
I haven't used the OCX yet, so I don't know what the command
would be, but
I do see the problem. After you produce the dial tone, you
are closing the
port before releasing the phone line (ie.. the phone line is
still open).
Send a command Thisform.CommCtrl.Output = "ATZ+ CHR(13) + CHR(10)"
(usually resets & hangs up)
before the command Thisform.CommCtrl.PortOpen = .F..