Need to write a character directly to LPT1 port

Need to write a character directly to LPT1 port

Postby Rafael Clemente » Fri Jun 02, 2006 7:43 pm

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
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby Randal » Fri Jun 02, 2006 8:13 pm

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
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Postby Randal » Fri Jun 02, 2006 8:14 pm

Oops, cPort would be equal to "LPT1"

Regards,
Randal
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Postby Rafael Clemente » Fri Jun 02, 2006 9:00 pm

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
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby Randal » Fri Jun 02, 2006 10:25 pm

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
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Postby Rafael Clemente » Fri Jun 02, 2006 10:53 pm

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
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby Rafael Clemente » Fri Jun 02, 2006 11:00 pm

Oops: Forgot the link to the image:
http://hyperupload.com/download/02e5d67 ... 1.jpg.html

R.
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby Randal » Sat Jun 03, 2006 3:43 pm

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
Randal
 
Posts: 260
Joined: Mon Oct 24, 2005 8:04 pm

Postby Antonio Linares » Sat Jun 03, 2006 5:32 pm

Rafael,

To post images please use www.imageshack.us and copy here the provided link.

Thanks,
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42077
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Rafael Clemente » Tue Jun 06, 2006 8:06 am

Randall:
Here is my code:
Code: Select all  Expand view

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
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby Rafael Clemente » Tue Jun 06, 2006 8:15 am

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.
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby tnhoe » Tue Jun 06, 2006 5:14 pm

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
Regards

Hoe, email: easywin3@yahoo.com
User avatar
tnhoe
 
Posts: 83
Joined: Tue Nov 08, 2005 11:09 am
Location: Malaysia

Postby Rafael Clemente » Thu Jun 08, 2006 7:41 am

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
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Postby Enrico Maria Giordano » Thu Jun 08, 2006 7:46 am

Try SetCommState( nComm, cDcb ).

This is a change from 16 bit syntax.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8710
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Postby Rafael Clemente » Thu Jun 08, 2006 8:10 am

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
User avatar
Rafael Clemente
 
Posts: 365
Joined: Sat Oct 08, 2005 7:59 pm
Location: Barcelona, Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 73 guests