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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I read a data from serial ports

Status
Not open for further replies.

Ailton

Programmer
Apr 7, 2000
1
BR
I've been using FoxPro For DOS Version 2.6<br><br>I need to read data from serial ports.<br><br>I know I have to use FOPEN, FREAD and so on,<br>but I have no example of its use.<br><br>How can I use this commands to get the data?<br><br><br>Thanks in Advance
 
Trust me on this one, It is not fun.&nbsp;&nbsp;I highly suggest you take the easy way out and not reinvent the wheel.&nbsp;&nbsp;Go check out CommTools at <br><A HREF=" TARGET="_new"> purchase it.&nbsp;&nbsp;The routines are already built and you can do almost anything with a serial port with this application.<br><br>There are some other fox librarys that work with serial ports Comet and NetLib but I do not know where to find them right now.&nbsp;&nbsp;Had to reformat my hard drive a few weeks ago and most of my old bookmarks are lost.<br><br> <p>David W. Grewe<br><a href=mailto:Dave@internationalbid.net>Dave@internationalbid.net</a><br><a href= > </a><br>
 
step 1: initial serial port<br>&nbsp;&nbsp;!mode con com1 2400,n,8,1&gt;NUL<br><br>step2: open it<br>&nbsp;&nbsp;COM=FOPEN(&quot;COM1&quot;,12)<br>&nbsp;&nbsp;notice: the parameter must be '12'(open it must under non-buffer way!)<br><br>step3&nbsp;&nbsp;write data<br>&nbsp;&nbsp;N=FWRITE(COM,string)<br>&nbsp;&nbsp;*N is return value that is the numbers you write to port.<br><br>step4&nbsp;&nbsp;close serial port<br>=INKEY(2)<br>=FWRITE(COM,&quot;+++&quot;) *&quot;+++&quot; is the modem command in AT command<br>=INKEY(1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* if it does not hang up £¬relay the time<br>=FCLOSE(COM)<br><br>step5&nbsp;&nbsp;GOOD LUCK TO YOU!<br>
 
It's been years, but I've written a couple of 2.6 apps to read & write through serial ports.&nbsp;&nbsp;One had to read a scale, then send the data to another location.<br><br>Keep in mind that the example given to you assumes you are using a modem.&nbsp;&nbsp;If you are reading a device, you would of course, not be concerned with the AT commands.&nbsp;&nbsp;<br><br>FoxHelp gives good examples of low-level file commands. Remember there is FPUTS / FWRITE and FREAD / FGETS&nbsp;&nbsp;- their difference being whether they pay attention to crlf's.<br><br>My experience with Fox's low-level commands is that they are easy to use and dependable. I wouldn't waste money on a third-party product.<br><br>If you wanted to farm this job out - check <A HREF=" TARGET="_new"> Hank Castello
 
I have a DNC type app that I wrote back in '92 that sends CNC files to punch press controllers and it uses only the FP provided low level functions. Has worked great all these years... Now I'm having to redo it for NT, but that's another thread (shortly)!

On the 7 PCs (5 Win95 - 2 DOS) that I have this running on, I set an environment variable with the proper comm parameters in the Autoexec.bat file and call mode there also:

:: **** GMAN section - Don't screw with this section ****
SET PORTCFG=COM2:4800,E,7,1
mode %portcfg%
:: **** End GMAN section ****

In my PRG I have something like:

M.comset=GETENV('PORTCFG')
M.comset=IIF(LEN(M.comset) < 15,'COM1:4800,E,7,1',M.comset)

! MODE &M.comset >NUL

...

M.cport=LEFT(M.comset,4)
chandl=FOPEN(M.cport,12)
IF chandl < 0
WAIT WINDOW 'CAN NOT OPEN COM PORT !! 'NOWAIT
CANCEL
ENDIF

...

The user types in the filename of the CNC file to download and I do a few other things, then I eventually get to a hard loop that waits to for an XON char(17) from the controller. This is all from memory but it's something like:

ON ESCAPE DO whatever

DO WHILE PATIENT
OK=Fread(chandl,1)
PATIENT=IIF(ok=chr(17),.F.,.T.)
ENDDO

Once I got through the loop I just started sending the file line by line with FPUTS. A final FPUTS(chandl,'%') tells this controller I'm through. This may seem very crude, but I've never had any buffer overruns on the controllers.

GMAN [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top