Page 1 of 2
Need to write a character directly to LPT1 port
Posted: Fri Jun 02, 2006 7:43 pm
by Rafael Clemente
I have been asked to write a simple program to handle an external 8-relays board that can be controled through the standard parallel port.
I need just to send one ASCII character to LPT1 to activate the desired relay. The problem is that I can not find how to do it:
I've tried the TDosPrn() command() method but it seems to go through the Windows printer driver and keeps answering "Printer does not repond".
I've tried also OutportByte (888, 64) and get a GPF.
I'm pretty sure there must be a rather simple way of doing it, but at present I am just lost. Any ideas?
Rafael
Posted: Fri Jun 02, 2006 8:13 pm
by Randal
Rafael,
This is how I write directly to the LPT port.
nFp := fopen( (cPort), 2)
IF nFp = -1
MsgInfo("Error opening "+cPort+". Error Code: "+alltrim(str(ferror())) )
RETURN NIL
ENDIF
cBuffer := space(1) // or some character
fwrite( nFp, cBuffer )
fclose(nFp)
Regards,
Randal
Posted: Fri Jun 02, 2006 8:14 pm
by Randal
Oops, cPort would be equal to "LPT1"
Regards,
Randal
Posted: Fri Jun 02, 2006 9:00 pm
by Rafael Clemente
Randall:
Just tried your example on a W2000 professional. After some waiting, I got a Windows message "LPT1 is not connected. Retry or cancel?"
When you try it on you system, do you get an inmediate answer or does it take a while? What Operating System do you use?
Thanks a lot for your help
Rafael
Posted: Fri Jun 02, 2006 10:25 pm
by Randal
Rafael,
The code I posted was a small snippet of a larger function I use to print to dot matrix printers. I have customer's using every version of Windows available, including Windows 2000.
If the function cannot open LPT1 for writing it returns a low level fopen error code. This has worked for years using FW/Clipper and FWH/xHarbour. I've never seen the error message you posted.
Perhaps you should check Control Panel, System, Device Manager or check your bios and make sure the LPT port is functional. Do you have the device you are using plugged into this port? What if you unplug it, do you get a different error message? Do you have any printer drivers installed that may be assigned to this port? Does the device you are using have some kind of drivers you installed?
Regards,
Randal
Posted: Fri Jun 02, 2006 10:53 pm
by Rafael Clemente
Randall:
Here is the error message I get:
[img]c:\Image1.jpg[/img]
My Windows is just an standard Windows with the usual laser and inkjet drivers installed, but nothing out of the normal. My other Clipper DOS programs write Ok to the parallel port. The problem seems to be just with the relays board...
Of course, there is also a possibility that the board is defective (although I got it as new). I'll investigate that next week.
Meanwhile, thank a lot again.
Rafael
Posted: Fri Jun 02, 2006 11:00 pm
by Rafael Clemente
Posted: Sat Jun 03, 2006 3:43 pm
by Randal
Rafael,
Where in your code do you get the error, when opening the port with fopen or when trying to fwrite?
Be sure you are using "LPT1" not "LPT1:".
I have had some instances where USB printers will grab what is suppose to go to LPT1. In those cases I have the user delete all the printer drivers (except the dot matrix printer) and then reinstall them. You might try deleting all installed printers and see what happens. It is possible that what you are fwriting is being intercepted by one of the printer drivers.
The error looks like something that would be generated from a printer driver. Using low level functions like fopen/fwrite would not generate that kind of error.
Regards,
Randal
Posted: Sat Jun 03, 2006 5:32 pm
by Antonio Linares
Rafael,
To post images please use
www.imageshack.us and copy here the provided link.
Thanks,
Posted: Tue Jun 06, 2006 8:06 am
by Rafael Clemente
Randall:
Here is my code:
Code: Select all
cPort := "LPT1"
nFp := fopen((cPort),2)
IF nFp = -1
MsgInfo ("Error")
Return Nil
else
? nfp
Endif
When I run it, I get nFp = 3 "Path not found()" ????
I have my W2000 configured with just a generic printer on LPT1 and removed all other printer drivers. Any idea?
Thanks
Rafael
Posted: Tue Jun 06, 2006 8:15 am
by Rafael Clemente
Randall:
Sorry: I didn't realize nFp = 3 wasn't an error code but the file handle.
But still, the program doesn't work. I'll continue investigating...
R.
Posted: Tue Jun 06, 2006 5:14 pm
by tnhoe
Try this :-
function fLptSend
local cDcb, nBytes
local nComm := OpenComm( 'LPT1', 1200, 128 )
local nError
if ! BuildCommDcb( "LPT1:1200,n,8,1", @cDcb )
nError = GetCommError( nComm )
Msgwait( " BuildCommDcb Error: " + Str( nError ),'',.3 )
endif
if ! SetCommState( cDcb )
nError = GetCommError( nComm )
Msgwait( "SetCommState Error: " + Str( nError ),'',.3 )
return .t.
endif
if ( nBytes := WriteComm( nComm, 'your ASCII string') ) < 0
nError = GetCommError( nComm )
Msgwait( "WriteComm Error: " + Str( nError ),'',.1 )
else
Msgwait('Sent !!!',.1 )
endif
if FlushComm( nComm, 0 ) != 0
nError = GetCommError( nComm )
Msgwait( " FlushComm Error: " + Str( nError ),'',.01 )
endif
if ! CloseComm( nComm )
nError = GetCommError( nComm )
Msgwait( "CloseComm Error: " + Str( nError ),'',.01 )
endif
return nil
Posted: Thu Jun 08, 2006 7:41 am
by Rafael Clemente
Hoe:
I've tried your function using Xp SP2 and an standard LPT1 printer (I have sent my relay board back to factory to make sure it works).
First, I get " *** BuildCommDcb Error: 0" Then, after a looong delay, the printer finally prints the my ASCII string. No other errors appear. I wonder:
a) Why the long delay? Can the printer be forced to write inmediately?
b) It is a paralell printer. Shouldn't the BuildCommDcb() function work only with serial devices?
Thanks very much for your help
Rafael
Posted: Thu Jun 08, 2006 7:46 am
by Enrico Maria Giordano
Try SetCommState( nComm, cDcb ).
This is a change from 16 bit syntax.
EMG
Posted: Thu Jun 08, 2006 8:10 am
by Rafael Clemente
Enrico:
Just tried it but I don't see any difference. The printer still takes a long time (several minutes) before it outputs the Ascii string. I suppose it is ths Xp who flushes the printer spooler. Is there any way to force to do it at once?
Thanks,
Rafael