Need to write a character directly to LPT1 port
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
Need to write a character directly to LPT1 port
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
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
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
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
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
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
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
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
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
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
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
- Antonio Linares
- Site Admin
- Posts: 37481
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Contact:
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
Randall:
Here is my code:
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
Here is my code:
Code: Select all
cPort := "LPT1"
nFp := fopen((cPort),2)
IF nFp = -1
MsgInfo ("Error")
Return Nil
else
? nfp
Endif
I have my W2000 configured with just a generic printer on LPT1 and removed all other printer drivers. Any idea?
Thanks
Rafael
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
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
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
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain
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
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
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
- Rafael Clemente
- Posts: 365
- Joined: Sat Oct 08, 2005 7:59 pm
- Location: Barcelona, Spain