Solved

Parallel Port I/O under NT 4.0

Posted on 1997-05-21
17
243 Views
Last Modified: 2013-12-03
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
Comment
Question by:flannlo
  • 7
  • 5
  • 3
  • +1
17 Comments
 
LVL 15

Accepted Solution

by:
NickRepin earned 40 total points
ID: 1397549
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
 

Author Comment

by:flannlo
ID: 1397550
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
 

Author Comment

by:flannlo
ID: 1397551
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
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397552
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
 
LVL 4

Expert Comment

by:nil_dib
ID: 1397553
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
 

Expert Comment

by:bushnaq
ID: 1397554
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
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397555
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
 

Expert Comment

by:bushnaq
ID: 1397556
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 15

Expert Comment

by:NickRepin
ID: 1397557
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
 

Expert Comment

by:bushnaq
ID: 1397558
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
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397559
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
 

Expert Comment

by:bushnaq
ID: 1397560
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
 
LVL 4

Expert Comment

by:nil_dib
ID: 1397561
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
 
LVL 4

Expert Comment

by:nil_dib
ID: 1397562
Another idea:
Is it possible to write a programm with some inline assembler code to kommunicate with the port?
0
 

Expert Comment

by:bushnaq
ID: 1397563
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
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397564
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
 
LVL 15

Expert Comment

by:NickRepin
ID: 1397565
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now