• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Parallel Port I/O under NT 4.0

Are there any ways to write and read a byte to/from a parallel port without writing a device driver??  I am writing the program in VC++, which functions can I use to do the port I/O??  If there are any sample code fragments, please send to me too.

Thanks in advance.
0
flannlo
Asked:
flannlo
  • 7
  • 5
  • 3
  • +1
1 Solution
 
NickRepinCommented:
You can use CreateFile, ReadFile, WriteFile.

Here is a simple program using asynchronous IO.
To simplify it, use 0 instead FILE_FLAG_OVERLAPPED in CreateFile() and NULL instead of &st_Overlapped in WriteFile().

When you connect line printer to LPT1 (w/o installing printer driver) and run this program, the printer will print 'print string'.

#include <windows.h>
#include <iostream.h>

void main(void) {
   HANDLE hLPT;
   BYTE* ccb="print string";
   BOOL bWriteFile,bCloseHandle, bWait=FALSE, bOVResult;
   OVERLAPPED st_Overlapped;

   memset(&st_Overlapped,0,sizeof(OVERLAPPED));
   hLPT=CreateFile("LPT1",GENERIC_READ|GENERIC_WRITE,0,NULL,
      OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
   cout<<"hLpt="<<hex<<hLPT<<endl;

   // Creating event
   st_Overlapped.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);

   DWORD num,numb;
   bWriteFile=WriteFile(hLPT, ccb, strlen(ccb), &num,&st_Overlapped);
   cout<<"bWriteFile="<<dec<<bWriteFile<<' '<<GetLastError()<<endl;
   if(GetLastError()!=ERROR_IO_PENDING) {
      cout<<"Fatal error"<<endl;
   }
   else {
      BOOL r=GetOverlappedResult(hLPT,&st_Overlapped,&numb,TRUE);
      cout<<"GetOverlappedResult()="<<r<<' '<<GetLastError()<<endl;
   }

   bCloseHandle=CloseHandle(hLPT);
}

0
 
flannloAuthor Commented:
Would you mind telling me what FILE_FLAG_OVERLAPPED, CreateEvent, and memset do in the program??  What are their purposes, and the advantages of using them??

Thanks a lot.
0
 
flannloAuthor Commented:
Also, can I simply use CreateFile, ReadFile and WriteFile to do a bidirectional port so that I can use a parallel port to communicate between two computers??  If so, how can I use these functions to read and write from/to the status port and the control port of the parallel port??

Thanks a lot.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
NickRepinCommented:
You can use FILE_FLAG_OVERLAPPED (asyncronous I/O) when you need to perform any task while I/O operations is not competed yet. I.e., WriteFile/ReadFile returns immediately:

    WriteFile(....)
    // ...
    // doing any task; write i/o processing in background
    // ...
    GetOverlappedResult(...)  // check if i/o is completed

In my opinion, you should not care about status/control ports (to do this, you have to write device driver).

Try just to  perform WriteFile on one computer and ReadFile on another.

Here is a sample with syncronous i/o:

#include <windows.h>
#include <iostream.h>

void main(void) {
   HANDLE hLPT;
   BYTE* str="out string";

   hLPT=CreateFile("LPT1", GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
   if(hLPT==INVALID_HANDLE_VALUE) {
      // Error!
      return;  
   }

   DWORD bytesWritten, bytesToWrite;
   bytesToWrite=strlen(str);
   BOOL r=WriteFile(hLPT, str, bytesToWrite, &bytesWritten, NULL);
   if(!r || bytesWritten!=bytesToWrite) {
      // Error!
      // return;
   }

   CloseHandle(hLPT);
}

0
 
nil_dibCommented:
Hi,
I've  tested the programm with syncronous i/o. But its allways the same: the PC trys to print the Byte I wrote to the port. If the printer is installed I can see the Byte/Word... in the print status window. Without a printer installed, the programm returns: The device is not ready.
0
 
bushnaqCommented:
I have the same problem with my own code.
Using Windows 95 it is working with or without a driver.
Reading to and writing from the parallel port.
Under NT there has to be a driver installed before you can write.
I could not get Reading from port work on NT.
I have the feeling that this is a bug in NT.


0
 
NickRepinCommented:
Of course, without a printer installed, the program returns 'device not ready' because there is nothing attached and nobody answers on physical port. Without a printer DRIVER installed but with a PRINTER installed, it must be all ok.
0
 
bushnaqCommented:
Under Windows 95 is works OK without a printer driver.
The printer driver is realy not neccesery.
All i sneeded is the port monitor and this one is alwais installed.
The spooler will call the port monitor that will again call the parallel port driver to actually access the parallel port.
Under Windwos NT it will not work.
Like I mentioned under NT I cannot even read from the port with a driver installed.

0
 
NickRepinCommented:
bushnaq,

I am sorry, I cannot exactly understand your problem.
As I know, there is set of Windows NT drivers, like 'Parallel', 'Parport', 'ParVDM' wich create logical device 'LPT'. Of course, my program cannot write/read directly to parallel port, but to these drivers.
Drivers, in turn, write to parallel port. Ie  just like to serial port.
You can access to parallel port just like serial one. No more, no less.
0
 
bushnaqCommented:
I think we are talking about the same thing.
Only the parallel port driver can access the hardware directly.
All I'm saying is that:
1- If I use CreateFile() to open a parellel port.
2- Use ReadFile() to read from Port.
3- Under Widnwos 95 it will work regardless of the fact if a printer driver is installed or not.
4- Under Windwos NT it will only work if there is no printer dirver installed. It makes no difference if the printer dirver is a bidirectional one or not.
5- Use WriteFile() to write to Port.
6- Under Widnwos 95 and Widnwos NT it will work regardless of the fact if a printer driver is installed or not.

So, there is a big difference between Windwos 95 and Windows NT
way of reacting to reading from parallel port.
 
0
 
NickRepinCommented:
A see now. May be. But in my opinion, it is not correctly to directly access to parallel port (via CreateFile) if there is printer driver installed. Though, it may be useful, eg, if we need to temporary connect two PC via parallel interface and we have a printer attached.
0
 
bushnaqCommented:
Microsoft is actually mentioning exactly this function to access the parallel port for reading and writing. And it is working for Windows 95.
A printer driver is realy not responsible for accessing the port. It is there for translating the system data into a printer language and comunicate withthe priner.
An application needing to use the parallel port should not go through the printer driver but from the spooler to the port driver. CreateFile() will do exactly this for you.

0
 
nil_dibCommented:
That means definitive that have to write a VxD driver to write a byte to the port (NT4.0) without printer, printer driver ... !?
Is there a possibility to access an existing port driver? I found a lpt.vxd driver file under Win95. Can I use this one witout writing my own VxD?  
0
 
nil_dibCommented:
Another idea:
Is it possible to write a programm with some inline assembler code to kommunicate with the port?
0
 
bushnaqCommented:
There is a source from Microsoft for the parallel port under NT called intpar.
It is located in the comm folder of the NT4 DDK.
I have no idea if the normal port addresses that where used under DOS can be used under NT to retrive status information. It is not that easy to know what port to use. This depends very much on what kind of parallel port the system has.
Under DOS the BIOS would have helped with providing the Int 17h functions for the parallel port.
I think that maybe this is a bug in NT that Microsoft will fix some day.
I do not think that writing a parallel port driver will help. The problem is the spooler that will not let anyone access the parallel port driver if no printer driver is using it.
0
 
NickRepinCommented:
bushnaq,

'A printer driver is realy not responsible for accessing the port'. I am agree with you. At least should not.  But as I see in your previous comments it is not true for NT 4.0.
Also, I think the spooler is used by printer driver, not CreateFile.
0
 
NickRepinCommented:
nil_dib, I have done all for you. I dont see any problem to access the parallel port without a printer driver.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now