Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
MSComm Documentation
Message
From
27/09/2002 14:11:17
 
 
To
27/09/2002 14:01:54
James Hill
Jim Hill Productions
Halifax, Nova Scotia, Canada
General information
Forum:
Visual FoxPro
Category:
ActiveX controls in VFP
Miscellaneous
Thread ID:
00705355
Message ID:
00705364
Views:
33
Have you done a search on the UT on 'mscomm?' I was looking for the same answers months ago and as I recall Jim Livermore gave me a lot of help. I have a project that communicates via mscomm with a phonetree system now that works well. I don't know of any good tutorials or documentation other than the few MSFT had posted. They didn't help me much either though. Below is my code to help get you started. My code sends and receives data over the com port and sending the records in a dbf would work similarliy. It is mostly the oncomm event of the control that matters. I had to subclass it and put it on a form to work reliably. You can make the form hidden if necessary.

Tracy
*portion of calling program
DO FORM COMFORM NAME ComForm LINKED
DO phmenu.mpr
DO openporttophonetree
DO rcvoption
DO WHILE !llexit
	READ EVENTS
	llexit=.T.
ENDDO

*Exit
IF UPPER(TYPE('ComForm'))="O"
	IF COMFORM!=.NULL.
		COMFORM.RELEASE
	ENDIF
	COMFORM=.NULL.
	RELEASE COMFORM
ELSE
	IF UPPER(TYPE('ComForm'))!="U"
		COMFORM=.NULL.
		RELEASE COMFORM
	ENDIF
ENDIF
IF UPPER(TYPE('oAnimation.name'))="C"
	oAnimation.aviclose()
ENDIF
oAnimation=.NULL.
RELEASE oAnimation


*---------------------------------------------------
PROCEDURE openporttophonetree
		IF llexit .or. UPPER(TYPE('ComForm.MsCom'))!="O"
			IF UPPER(TYPE('ComForm.MsCom'))!="O"
				mIsOpened=.F.
				RETURN
			ELSE
				RETURN
			ENDIF
		ENDIF
		IF UPPER(TYPE('ComForm.MsCom'))="O"
			WITH COMFORM.mscom
				IF .PortOpen
					.PortOpen = .F.
				ENDIF
				IF !.PortOpen
					mIsOpened = .F.
					*.RTSEnable = .T.
					*.InBuffersize = 2048
					*.InBuffersize = 4072
					*.InputLen = 0
					*.RThreshold = 1
					*.SThreshold = 1
					.CommPort = m_port
					.Settings = ALLTRIM(STR(m_baudrate))+","+s_parity+","+ALLTRIM(STR(m_databits))+","+ALLTRIM(STR(m_stopbits))
					.InputLen = 0
					.RThreshold=1
					.SThreshold=1
				ENDIF
				IF !.PortOpen
					.PortOpen = .T.
				ENDIF
				IF .PortOpen
					mIsOpened = .T.
				ELSE
					mIsOpened = .F.
				ENDIF
			ENDWITH
		ELSE
			IF !llexit
				MESSAGEBOX('PhoneTree device NOT responding.'+CHR(13);
						+'Could not open port to phonetree.',16,'Phonetree Error!',5000000)
			ENDIF
		ENDIF	&& is comform.mscom a valid object?
		*---Check to see if the PhoneTree Device is responding
		IF llexit .or. UPPER(TYPE('ComForm.MsCom'))!="O"
			IF UPPER(TYPE('ComForm.MsCom'))!="O"
				mIsOpened=.F.
				RETURN
			ELSE
				RETURN
			ENDIF
		ENDIF
		DO setupphonetree
		IF llexit .or. UPPER(TYPE('ComForm.MsCom'))!="O"
			IF UPPER(TYPE('ComForm.MsCom'))!="O"
				mIsOpened=.F.
				RETURN
			ELSE
				RETURN
			ENDIF
		ENDIF
		DO getstatus
*--MsCommContainer.OnComm()
*--Last Modified:
*--TCHolzer 01/22/2002  Trimmed extra characters from receive string
*--TCHolzer 01/31/2002 Corrected storing of input buffer to variable s for ON_LIST case
IF THISFORM.llescape
	RETURN
ENDIF
LOCAL ;
	i,;
	istop,;
	lnStart,;
	lnBegin
#define    ON_IDLE           0		&& dummy status to prevent onevent firing
#define    ON_STATUS         1		&& status of phonetree
#define    ON_SIZE           2		&& number of records in list
#define    ON_VERSION        3		&& phonetree version
#define    ON_MESSAGE        4		&& receive message from phonetree
#define    ON_LIST           5		&& List messages sent to
#define    ON_PUTMSG         6      && send messge to phonetree
#define    ON_PUTLST         7      && send list to phonetree
#define    ON_STOPCALL       8      && stop calling from list immediately
#define    _END_CHAR         CHR(10)
#define    _NULL             CHR(0)

this.creceivebuffer=""
this.creceivestr=""
this.lcstring=""
this.result=""
lnstart=SECONDS()
lnBegin=SECONDS()

IF UPPER(TYPE('OnCommType'))="N"
	THIS.oncommtype=OnCommType
	IF this.oncommtype<>ON_IDLE
		=this.waitforresponse(1)
	ELSE
		=this.waitforresponse(1)
	ENDIF
ELSE
	this.oncommtype=0		&& idle by default
	=this.waitforresponse(1)
ENDIF

LnBegin=SECONDS()
DO WHILE THIS.commevent<>2 .and. !thisform.llexit	&& if user presses escape, cancel and return
	=this.waitforresponse(1)
	IF SECONDS()-LnBegin>3
		IF this.oncommtype<>ON_IDLE
			*--Should have switched commevent to 2 by now, not receiving for some reason
			MESSAGEBOX('An Error Occurred with Phonetree.'+CHR(13);
		          +'Unable to get response.  Please verify'+CHR(13);
		          +'the Phonetree system is turned on and'+CHR(13);
		          +'try again.',48,'ERROR communicating with Phonetree.',50000000)
		ENDIF
		EXIT
	ENDIF
ENDDO
DO WHILE THIS.CommEvent=2 .and. !thisform.llexit		&& is user presses escape, cancel and return
	IF thisform.llexit
		RETURN
	ENDIF
	IF THIS.CommEvent=2	&& capture the information in this.input
		IF THIS.InBufferCount>0	&& received data
			*--If a message or a list was received, wait for the end of it
			IF this.oncommtype=ON_MESSAGE.OR.this.oncommtype=ON_LIST	&& receive message or receive list from phonetree
				lnstart=SECONDS()
				DO WHILE !CHR(26) $ this.lcstring
					this.lcString = this.lcstring+this.input
					IF this.lcString=="".or.alltrim(this.lcstring)=">"	&& nothing received!
						IF SECONDS()-lnstart>120
							EXIT
						ENDIF
					ENDIF
				ENDDO
				IF this.oncommtype=ON_LIST
					s=this.lcstring
					this.result=this.lcstring
					this.lcstring=""
				ENDIF
			ELSE
				this.lcstring  = this.lcstring + this.input
			ENDIF
			IF !(this.lcstring == "")
				this.result = ""
				*--we captured all of the data from the port, make sure it is not just
				*--an acknowledgement of a carriage return sent to phonetree
				IF ALLTRIM(this.lcstring)=">"	&& this is from all carriage returns sent to phonetree
					EXIT
				ENDIF
				i=1
				istop=(LEN(this.lcstring))
				IF this.oncommtype!=ON_LIST
					FOR i = 1 TO istop
						DO WHILE ASC(SUBSTR(this.lcstring, i, 1))!=0 .and. SUBSTR(this.lcstring, i, 1) != _NULL ;
							.and. ASC(SUBSTR(this.lcstring, i, 1))!=32
							IF SUBSTR(this.lcstring, i, 1) != _END_CHAR .and. SUBSTR(this.lcstring, i, 1)!=">";
								.and.SUBSTR(this.lcstring, i, 1) != _NULL;
								.and.ASC(SUBSTR(this.lcstring, i, 1))!=32
								this.result = LEFT(this.lcstring, i)
								i=i+1
							ELSE
								i=i+1
							ENDIF
						ENDDO	
						i=i+1
					ENDFOR
				ENDIF
			ELSE
				this.result=""
			ENDIF
			IF this.oncommtype!=ON_MESSAGE .and. this.oncommtype!=ON_LIST	&& receive message or list from phonetree, do NOT strip
			    FOR i = 1 TO LEN(this.result)
			    	IF ASC(SUBSTR(this.result, i, 1))=32 .and. i>1
			    		this.result = LEFT(this.result, i-1)
		    		ENDIF
		    		IF ASC(SUBSTR(this.result, i, 1))=13 .and. i>1
		    			this.result = LEFT(this.result, i-1)
			    	ENDIF
			    ENDFOR
			ENDIF
			DO CASE
			CASE this.oncommtype=ON_STATUS
				mPTStatus=this.result
			CASE this.oncommtype=ON_VERSION
				mPTVersion=this.result
			CASE this.oncommtype=ON_SIZE
				mPTSize=this.result
			CASE this.oncommtype=ON_MESSAGE
				s=this.result
			CASE this.oncommtype=ON_LIST
				*Do NOTHING s is stored above
			OTHERWISE
				*DO NOTHING
			ENDCASE
		ELSE	&& just in case only
			EXIT
		ENDIF
	ELSE	&& just in case only
		EXIT
	ENDIF	&& is commevent 2 or not, of not we exit the loop
ENDDO	&& while this.commevent=2
RETURN

*mscomcontainer.waitforresponse
*~ This method loops for the specified
*~ amount of time calling DOEVENTS().
*~ This allows time for the modem to respond to the requests.

LPARAMETERS lnDelayInSeconds

LOCAL lnStartTime
lnStartTime = SECO()


DO WHILE SECO() <= (lnStartTime + lnDelayInSeconds) ;
AND NOT SECO() < lnStartTime
IF thisform.llexit
RETURN
ENDIF
DOEVENTS()
ENDDO
RETURN


>I have posted this query on both VFUG and the Microsoft Fox Programmer's Exchange but so far no answers. I Have searched the Help file, MSDN Library and The Hacker's Guide for VFP7 but I have been unable to find any useful documentation, or examples, on programming the MSComm control. What I need to do - for the first time ever - is to feed the contents of a dbf, one record at a time, to a serial port. Can anyone tell me how to do it or where to find some documentation.
>TIA
.·*´¨)
.·`TCH
(..·*

010000110101001101101000011000010111001001110000010011110111001001000010011101010111001101110100
"When the debate is lost, slander becomes the tool of the loser." - Socrates
Vita contingit, Vive cum eo. (Life Happens, Live With it.)
"Life is not measured by the number of breaths we take, but by the moments that take our breath away." -- author unknown
"De omnibus dubitandum"
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform