Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serial port comms... 1

Status
Not open for further replies.

Neil Toulouse

Programmer
Mar 18, 2002
882
GB
Hi guys

Can anyone point me to an 'idiots guide' on how to read values from a serial port in VFP (v8)? I have looked at bits of info I can find, but most of the links seem to be broken now, and not being up on this side of things I could do with a 'how to start' guide before I head too far down the wrong path.

TIA

Neil

I like work. It fascinates me. I can sit and look at it for hours...
 
Sorry, I can't point you to an idiot's guide, as I haven't really seen any either. But I can maybe give you a little insight from my experience. Which at times has involved some idiocy. [wink]
There are libraries and packages you can buy that may be a lot more graceful, but if you don't mind a little grunt work here is a brief example to get you started.

The only way I've done serial communications using VFP so far has been with the Microsoft MSComm control.
I would just start with some simple little test project to get a bit comfortable. Create a form and drop an instance of the comm control on it. You can get access to it in the usual way, from the Tools->Options->Controls->Microsoft Communications Control ... This will add it to your form controls Active X controls dialog.
In the form's 'Init', add this statement: _VFP.AUTOYIELD = .F.

In your comm control's 'Init', put something like this:
Code:
   THIS.Settings = "9600,N,8,1"  &&... 9600 baud, no parity, 8bits, 1 stop bit
   THIS.RThreshold = 1
   This.SThreshold = 1
   THIS.InputLen = 100
   THIS.inBufferSize = 1024

   THIS.CommPort = THISFORM.nCommPort   &&... serial port you're using
   THIS.PortOpen = .T.  &&... This actually opens/readies the port for use
   RETURN

Add a command button that sends some data in the click event:
Code:
&&... create your output string here. Something like 'AT' + CHR(13)
STORE 'AT' + CHR(13) TO cOutStr
*THISFORM.iTx.PICTURE = 'txrx.ico'  
Thisform.olecontrol1.OUTPUT = cOutStr
=INKEY(.1, "HM")   &&... this statement 'forces' the control to react

The real action happens in the the control's 'OnComm' event. There you can react to sending data as well as receiving. For instance, I like to blink a little 'LED' when sending and another when receiving:
Code:
IF (THIS.commEvent == 1)  &&... xmit
   *THISFORM.iTx.PICTURE = 'dead.ico'  &&... turns off LED after command button turned it on
   RETURN
ENDIF

&&... Something residual in the buffer
IF THIS.InBufferCount > 0 AND THIS.commEvent # 2
   IF THIS.commEvent > 1000   
   &&... some sort of comm error. Close, reopen, re-init
      This.PortOpen = .F.
      DOEVENTS
      x = INKEY(1, 'HM')

      THIS.Settings = "9600,N,8,1"
      THIS.RThreshold = 1
      This.SThreshold = 1
      THIS.InputLen = 100
      THIS.inBufferSize = 1024
      THIS.CommPort = THISFORM.nPort
      DOEVENTS
      x = INKEY(1, 'HM')
   ENDIF 
   RETURN
ENDIF

&&... we have data, let's react to it
IF (THIS.commEvent == 2) 
   DO WHILE THIS.InBufferCount > 0
      cInString = THIS.INPUT
      DOEVENTS
      x = INKEY(.1, 'HM')

      DO CASE
         *... if we get a ACK, send ENQ
         CASE ASC(cInString ) = ACK
            THIS.OUTPUT = CHR(nENQ)
            DOEVENTS
            x = INKEY(.1, 'HM')

         *... if we get a ENQ, send ACK
         CASE ASC(cInString ) = nENQ
            THIS.OUTPUT = CHR(ACK)
            DOEVENTS
            x = INKEY(.1, 'HM')
       
         OTHERWISE
            *... otherwise, concat the buffer
            THISFORM.cInString = THISFORM.cInString + cInString

      ENDCASE
ENDDO 

&&... Here, you can respond to whatever you receive, parsing the data or values and dealing with them.
&&... Is the string all there?
&&... Is there an end-of-string, CRC, ...

RETURN

Hope that helps a little.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Thanks Dave, that'll definately get me started :)

Have a star!

Neil

I like work. It fascinates me. I can sit and look at it for hours...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top