Page 1 of 1

Easy Parallel Port Programming Tests (Under Windows)

Posted: Sat Apr 09, 2016 12:03 am
by ~
Programming the Parallel Port Under Windows

>> See also the Parallel Port OSDev Wiki Page <<

Image Download lpthandler.tar

We just need to connect a LED between pins 9 and 2 of the LPT port on the motherboard.

Or as the pinout shows, between any of the orange (data) pins and the gray (ground, GND) pins. We have a total of 16 of those pins, 2 for each data bit to be able to plug the 2 wires in the LED or in other device.

Make sure to put the LED in its correct polarity, or just flip it, or it simply won't allow any electricity flow and won't turn on.


Usage of this program to turn on all of the 8 LEDs:

Code: Select all

lpthandler.exe 3bc 11111111

   or port 378
lpthandler.exe 378 11111111

   or port 278
lpthandler.exe 278 11111111
Usage of this program to turn off the 8 LEDs:

Code: Select all

lpthandler.exe 3bc 00000000

   or port 378
lpthandler.exe 378 00000000

   or port 278
lpthandler.exe 278 00000000
Each of the 8 bits to write must be an 1 or 0.
A 0 turns off and an 1 turns on.

Code: Select all

/*
 In short we only need:
 
  Out32(LPT_Base_Address+2, 0);          //Reset Control port with 0
  Out32(LPT_Base_Address,   0xFF-0x00);  //Write Data Port with any byte value
*/
 
 
 
//Here the LPT_Base_Address can be:
//
//          0x3BC -- LPT1
//          0x378 -- LPT1
//          0x278 -- LPT2
//          0x3BC -- LPT3
///
 
//We need to reset the Control Port writing a 0
//to configure their options, controlled for each of
//their bits:
///
  Out32(LPT_Base_Address+2, 0);
 
//Now send an value betwewen 0 and 255 to the data port:
///
  Out32(LPT_Base_Address, 0xFF-0x00);

In case the code in unclear, you can see how it was typed in real time, and also videos that show the program working:
YouTube video: Basics of Parallel Port Programming Under Windows, Part 1 of 2
YouTube video: Basics of Parallel Port Programming Under Windows, Part 2 of 2


Image


>> Watch the Code Recording for lpthandler.c <<
Image



The first thing is to get the code and binary for inpout32.dll for general low-level access under Windows:

inpout32_source_and_bins.zip [Mirror 1]

inpout32_source_and_bins.zip [Mirror 2]

Then we need what I/O addresses the parallel port has. The I/O ports are like memory addresses that range from 0 to 65535. To each of those addresses, we can send 1, 2 or 4 at a time, or we can receive 1, 2 or 4 bytes at a time. By now we just need to send 1 byte at a time.



LPT1 is at port (0x378, 378h)(888 decimal) or (0x3BC, 3BCh)(956 decimal).

LPT2 is at port (0x278, 278h)(632 decimal).

LPT3 is at port (0x3BC, 3BCh)(956 decimal).

The parallel port is an 8-bit peripheral, so it uses 8 data pins to hold the 8 bits of each byte (since it uses parallel, simultaneous signals), and those are the pins that we will manipulate by turning them on or off each time we write the data port, for instance, at port 0x378 (The Orange Pins, from 2 to 9):





Female Parallel Connector, at the Motherboard:
Image




Now we need to use typedef short _stdcall (*_Out32)(short portaddr, short value), which comes from the inpout32.dll and hwinterface.sys.

Something like this:

Code: Select all

Out32(378h, 255-0);



The full source code is as follows:

Code: Select all

//NOTE: Compile with GCC under MinGW.
//
// gcc lpthandler.c -o lpthandler.exe
//
//Usage:
// lpthandler.exe LPT_Port Byte_Bit_Data
//
// LPT_Port:
//  - can be 3bc, 378 or 278
//
// Byte_Bit_Data:
//  - can range from 00000000 to 11111111 (written like that, in binary).
//    Every bit controls one of 8 LEDs.
//
// LEDs are connected between DATA and GROUND lines.
// DATA to GROUND lines are connected like this, for pins:
//
// DATA is + and GROUND is -
//
//  2-18
//  3-19
//  4-20
//  5-21
//  6-22
//  7-23
//  8-24
//  9-25
//
// Remember to set the parallel ports as Standard/Normal in the BIOS
// to ensure proper working of the basics with this program.
///

#include <stdio.h>
#include <windows.h>

typedef short _stdcall (*_Out32)(short portaddr, short value);
typedef short _stdcall (*_Inp32)(short portaddr);

int main(int argc, char *argv[])
{
 HINSTANCE INPOUT32_DLL_Handle;
 _Inp32 Inp32;
 _Out32 Out32;


 //Load the inpout32.dll library. It's also associated
 //with hwinterface.sys, but it should open it by itself:
 ///
  INPOUT32_DLL_Handle=LoadLibrary("inpout32.dll");
  if(INPOUT32_DLL_Handle == NULL)
  {
   printf("LoadLibrary failed for inpout32.dll...\n\n");
   return -1;
  }


 //Get access to the Out32/Inp32 functions from inpout32.dll:
 ///
  Out32 = (_Out32)GetProcAddress(INPOUT32_DLL_Handle, "Out32");
  Inp32 = (_Inp32)GetProcAddress(INPOUT32_DLL_Handle, "Inp32");

 //NOTE: The following two lines are the actual logic of the program
 //      and are at the top of importance and the program's key logic.
 //      Everything else could be considered API noise:

 //Configure the CONTROL Port with a 0 to sanitize the port state:
 ///
  Out32(strtol(argv[1],NULL,16)+2, 0);  //Bit 0: Strobe
                                        //Bit 1: Auto Line Feed
                                        //Bit 2: Initialize Printer
                                        //Bit 3: Select Printer
                                        //Bit 4: Enable IRQ Via Ack Line
                                        //Bit 5: Enable Bi-Directional Port
                                        //Bit 6: Unused
                                        //Bit 7: Unused



 //Write the Data port with the desired 8-bit value:
 ///
  Out32(strtol(argv[1],NULL,16), strtol(argv[2],NULL,2));


 //Unload our inpout32.dll library:
 ///
  FreeLibrary(INPOUT32_DLL_Handle);

 return 0;
}


Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 11:09 am
by jojo
Not a great idea to just jam an LED in there without any current limiting resistors.

Also... I don't understand. Is this supposed to be, like, a tutorial? Because this feels more like a tool announcement than a tutorial. But unless you have a win 9x machine or an XP box with giveio or something sitting around (which I do, in your defense, and I was just parallel-port bit-banging SPI with it the other day), this isn't really super useful.

You might as well just buy a teensy or an rpi and play with that because that's going to work great with your modern computer and have a lot more flexibility to boot.

Code: Select all

END BUZZKILL

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 12:12 pm
by Schol-R-LEA
It isn't useful for most people who have a PC less than fifteen years old, period. Parallel ports are relics, and were even in the 1990s. I haven't seen a new computer that had one on the mobo since 2000, and while the adapters are cheap, there just seems little point to this.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 12:19 pm
by ~
Schol-R-LEA wrote:It isn't useful for most people who have a PC less than fifteen years old, period. Parallel ports are relics, and I haven't seen a new computer that had one on the mobo since 2000, and I don't know of anyone who even makes the adapter cards anymore.
You can always buy a PCI card with parallel and serial ports. Also a PCI card with mouse and keyboard PS/2 ports.

LEDs and microcontroller programming would be uses for that port, or maybe to try to learn about scanner and printer protocols before learning to use USB peripherals.

jojo wrote:Not a great idea to just jam an LED in there without any current limiting resistors.

Also... I don't understand. Is this supposed to be, like, a tutorial? Because this feels more like a tool announcement than a tutorial. But unless you have a win 9x machine or an XP box with giveio or something sitting around (which I do, in your defense, and I was just parallel-port bit-banging SPI with it the other day), this isn't really super useful.

You might as well just buy a teensy or an rpi and play with that because that's going to work great with your modern computer and have a lot more flexibility to boot.

Code: Select all

END BUZZKILL
It's a parallel port tutorial with a working example. It uses inpout32.dll and hwinterface.sys, and they are included with the example and source.

There's no reason why using the privileged WinAPI hardware functions wouldn't work under Windows 7 or higher if you have parallel ports at least through a PCI card.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 12:42 pm
by iansjack
A Pi Zero is cheaper than a PCI parallel port card, and rather more versatile. :)

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 1:01 pm
by ~
iansjack wrote:A Pi Zero is cheaper than a PCI parallel port card, and rather more versatile. :)
What I like about the PC platform is that it packs absolutely everything that could ever be needed, and if it isn't included, there are always cards or peripherals that can be added to a PCI slot or an USB port.

But those small computers (Raspberry Pi, Arduino boards, etc...) are important for hardware tests, microcontroller programming and even to implement web servers, or to use regularly instead of a regular PC/laptop to waste the least amount of electricity for normal daily tasks, even if they involve software development.

A GUI, Firefox, Chrome and the usual compilers and assemblers would be enough in a Raspberry Pi for example.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 1:25 pm
by jojo
I mean, whatever floats your boat, man. But this is already really well documented in many other places and is still way outshined by Arduino, which can painlessly speak with a modern computer out of the box with zero setup/driver bodges over USB, has around 4x the I/O with the direction of each pin being independently controllable, includes built-in goodies like PWM channels and serial bus controllers and ADC/DACs and handling of interrupts which you certainly can't do with a parallel port in modern windows, and is cheap as hell (teensies, which I love, can be had for under $20 apiece)

So, I get why you might like going retro or why you might have a personal affinity for doing dumb crap with obsolete hardware, because god knows I do as well. But I don't know why you're sharing this particular info with a group of people who are familiar with this stuff to the point that something like this is a couple of orders of magnitude below trivial.

Like, it might have made sense if you made a fun/silly project controlled with parallel port GPIO and shared it. But this is decidedly not that.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 3:15 pm
by ~
jojo wrote:I mean, whatever floats your boat, man. But this is already really well documented in many other places and is still way outshined by Arduino, which can painlessly speak with a modern computer out of the box with zero setup/driver bodges over USB, has around 4x the I/O with the direction of each pin being independently controllable, includes built-in goodies like PWM channels and serial bus controllers and ADC/DACs and handling of interrupts which you certainly can't do with a parallel port in modern windows, and is cheap as hell (teensies, which I love, can be had for under $20 apiece)

So, I get why you might like going retro or why you might have a personal affinity for doing dumb crap with obsolete hardware, because god knows I do as well. But I don't know why you're sharing this particular info with a group of people who are familiar with this stuff to the point that something like this is a couple of orders of magnitude below trivial.

Like, it might have made sense if you made a fun/silly project controlled with parallel port GPIO and shared it. But this is decidedly not that.
It wasn't documented anywhere here or the Wiki, so I just tried to make it public. There was just a stub that didn't explain in a way that made it feel like everything was straightforward to understand without leaving any doubt about how to configure the port in all possible conceivable modes, and with running examples.

For example, it might be known for many how to load and use DLLs, drivers and use the parallel port under Windows, but it isn't public or clear like this. It's intented to be trivial because it's just the most basic information so anyone could understand immediately what to do, where to start.

It's like there was a Big Bang of high-quality information to learn about the WinAPI, hardware programming, graphics and memory algorithms, etc., but now in the modern days we don't have much of that API information. Only those who were already developers learned that.

Around 10 years ago, websites like this and OSDever.net had a lot of information like this, but as you can see there is little activity now of that kind. Questions are made but there isn't much explained in the answers in practical tems, and what is done is integrated in the different projects' source code, which would need to be studied and fully explained in detail, to extract the standarized algorithms and API documentation.

At least I guess that the effort of making running examples for all tiny tricks, API call documentation and algorithms one comes across, helps to reach the more advanced topics with considerably less trouble, both for whom does it and for those that it helps.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 3:29 pm
by jojo

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 3:31 pm
by jojo
I suppose thanks for expanding the wiki, though. That's never a bad thing.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Mon Apr 18, 2016 4:02 pm
by ~
jojo wrote:I suppose thanks for expanding the wiki, though. That's never a bad thing.
The idea is to build documentation that is absolutely implementable and noise-free in a single place. It needs to be so good that the source code, its comments, the videos that show how to compile and run (and what effect the program has), and any other documentation be enough to teach a new trick to a programmer to provide a clear starting point, and make it useful in real applications. It shouldn't even take a week to learn one of those tricks, but around a day instead, or it wouldn't really explain what is being needed to make evident of the logic of the program.

A lot can be improved if it's done in all other topics, aspects and algorithms. In some years, the Wiki and other documents and projects' source code would look better with the additional intricate documentation around about them.

But we need a ton of runnable and high-quality examples that teach specific things that aren't really common when you start learning at least. If you remember the times of Turbo C++ 1, you would remember that the examples it contained were very good. But after Win9x and even more after Windows XP, this sort of clear tutorials that start from the most trivial seem almost limited only to those of that time, for classic hardware and software (which contain algorithms that are still as valuable as at that time anyway).


As you can see this topic indicates the very start in the most simple and brief way, to control the parallel port under Windows, which was no longer so obvious.

As you can see, you wouldn't find it directly in the links you put, but you would need to study them and write a similar code snippet from scratch. But now with this, it can just be reused since you know exactly where to start and exactly how.

Those documents might be good, but the noise needs to be taken out and each one of the things it teaches converted into running examples, to make it useful for a beginner.

Programmer's Heaven was an exceptional resource and it still is. The problem is that all of their files and documents were moved to a download directory and the main page now has a Questions/Answers format.

The old interface had more value than this, and the Questions/Answers would have been better in a subdomain.

The point is that those documents cause a lot of enthusiasm in newbies, but when I have seen them in the past and surely many others, the enthusiasm remains but I have been left unimpressed by seeing that they don't contain everything in such practical detail so that I can implement it immediately in real-world drivers.

When a competent newbie is unimpressed by the available documentation, that's a bad sign that tells that the documentation available is halfway from teaching what is really needed to implement useful things at the end of the reading.

But as you can see, with this topic now you can go ahead and control every output pin individually.

Later we would get to add more information and programs showing how to control the different port modes, explaining what is their original usage. But right now, since at least I don't have that information clear, I just don't include it because I don't have any way to teach it usefully, so by now it would just be padding.

When I get more information that is usable in real contexts (like driving specific models of parallel printers or scanners), then I will be able to explain it and make it worth to read it.

Re: Easy Parallel Port Programming Tests (Under Windows)

Posted: Tue Apr 19, 2016 7:02 am
by iansjack
~ wrote:
jojo wrote:I suppose thanks for expanding the wiki, though. That's never a bad thing.
The idea is to build documentation that is absolutely implementable and noise-free in a single place.
The best way to produce noise-free documentation is to drop that silly recorder idea. It just gets in the way.

I'm honestly not convinced that there is a lot of call for comprehensive documentation of obsolete interfaces; I would guess that those who need this information are competent enough to find it.