For your concern, try to make sure you set LPT1 as your default parrallel port and set its address as '0x378' and then you can call this method - 'PortAccess.Output(888, 0);' through interop inpout32.dll. Write to parallel port in Windows 7. [DllImport('inpout32.dll', EntryPoint='Out32')] public static extern void Output(int adress, int value); } I tried using this code available at codeproject, although the program is not generating any errors I am not able to observe any output. I even installed inpout32.dll.
Days of research and programming have led me to try all variants of inpout32.dll and inpoutx64.dll: binaries, source code, 32-bit, 64-bit, address wrappers. None work: no change is seen to the output bits of the port.
However, I know it is possible, because using another commercially available program that does parallel port output, I can detect a trigger (state change) on all eight output bits (D0-D7) by passing a value between 0 and 255, exactly what I want to do in my own application.
I have followed all the advice from at least these pages:
I am using Windows 7, 64-bit; and my SIIG Cyberpro port is mapped as LPT3 at address 0xCCD8, with four status bits at address 0xCCD4. I have another ECP printer port mapped as LPT1 at 0x0378, but that does not work either.
I know better than to try direct _inp(), _outp() calls on Win7.
Can anyone help?
If I need to download and modify the driver code, I can do that if I have to, but I think it should not be that difficult.
My final version of code uses 32-bit compilation, interfacing to inpout32.dll:
1 Answer
FYI:
When I added
and called that function rather than Out32(ushort addr, ushort value) the code worked.
I don't know why the exact interface matters, but it does; and perhaps that is indeed because of sign extension on the 16-bit port address, as suggested [somewhere TBD].
shiprshiprNot the answer you're looking for? Browse other questions tagged c#windowsdriverparallel-port or ask your own question.
Contents at a Glance
Introduction
The parallel port, or printer port, is one of the easiest pieces of hardware available to users for communicating with external circuitry or homemade hardware devices. Most people think that interfacing with an external circuit is hard and never come up with a solution. However, in this article, I am trying to convince you that porting with another external device is very easy and it has only a few tricks to be remembered!
I will also be discussing a sample surveillance application that can be developed with few requirements. It is very simple and anyone who reads this article can use it at home. I will then be giving an introduction to Serial Port Programming, which will be my next article on The Code Project. In that section, I will illustrate some basics of Serial Data Transmission and how to use a more advanced serial port instead of a parallel port.
The application that I have given is developed in Visual Basic .NET and it uses inpout32.dll for direct port access. In Windows XP and NT versions, direct port accessing is prohibited, so we need to have a driver in order to communicate with ports directly. Therefore we are using a freeware driver, inpout32.dll, which can be found here.
WARNING
Parallel Ports that are built into your machine can be damaged very easily! So, use all of my sample code at your own risk. All of these sample programs and circuits are thoroughly tested and they will never lead you to damage your hardware or your computer. I have never experienced such a bad incident! However, use these programs with great care…
Communicating with the Parallel Port
The parallel port usually comes as a 25-pin female port and it is commonly used to connect printers to a computer. Many geeks also use it to connect their own devices to their PCs. There is a few more things to remember when using a PC's Parallel Port. It can load only 2.5mA and ~2.5 volts. It is better to use opto-couplers or ULN2803 when interfacing with an external device.
If you're just thinking of lighting up an LED through a Printer Port, try to connect a 330 ohm resistor in between. There is some more useful information to consider when programming a Printer Port: the usual Printer Port is a 25-pin female D-shaped port and it has several registers. A register is a place where you can write values and retrieve them. There are different types of registers called Data Register, Control Register and Status Register.
Data Register
This is the register that allows the user to write values into the port. In simple words, these pins can be used to output a specific value in a data register. You can also change voltages in specific pins. These are called output pins. There are altogether 8 output pins available, ranging from D0 to D7.
Status Register (Pins)
These are called input pins or status registers and can hold a value that the outside world gives to the Parallel Port. So, this port acts like a reader and it has 5 pins for inputs. The pin range is S4 to S7.
Control Register (Pins)
This register can be used in both ways: it enables a user to write values to the outside world, as well as read values from the outside world. However, you need to remember that most of the control pins work in an inverted manner. You can see them with a dash sign on the top of the pin. Pin range is C0 to C3. Ground pins are used as neutral; these pins are used as (-) in batteries. If you're connecting a device to a Parallel Port in order to read or write, you have to use one or more ground pins and a read/write pin to work. For example, if you're trying to light up an LED, then you have to connect the (-) of the LED to a ground pin and the (+) of the LED to an output pin. For reading purposes, use the same mechanism.
Addressing the Parallel Port and Registers
Port addressing controls a great deal in port programming, as it is the door that enables programs to connect to the external circuit or device. Therefore I would like to explain all the available port addresses.
In normal PCs, a parallel port address can be one of the addresses given below, but depending on the BIOS settings and some other issues, the parallel port address can vary. However, it always lies in one of these address ranges.
As I mentioned above, those are the commonly used address ranges for parallel ports. Now you might be wondering how we communicate with each of them and how they work. It's very simple! Each of those above-mentioned address ranges has its own address for Data Register, Control Register and Status Register. So, if you want to access a Data register of port 888, then the Data register is also 888. However, if you want to access a status register, then add +1 to the 888 = 889 and this is the status register. To access a control register, what you have to do is add another +1 so that it is 890 and that is the Control register of the 888-based parallel port. If your parallel port address is different, please do this accordingly to find its corresponding registers.
There is another simple way to check the parallel port address of your computer. You can get to know the parallel port I/O addresses through the device manager. First, open the device manager (Start -> Settings -> Control Panel -> System -> Hardware -> Device Manager). Then select the parallel port you are interested in from the Ports (COM & LPT) section. With the mouse's right button, you can get the menu where you select Properties. When you go through steps, you will see a dialog similar to this. Locate the first item in the listbox and it carries the value 0378 – 037F so that this is the address range of the Printer Port. The data register address is 0378 in hex or 888 in dec.
Python 3 Serial Port Examples
Small Description of Inpout32.dll
Here in this article, we are using a third party tool called inpout32.dll to connect to ports available in your PC. We are using it because in Windows NT based versions, all the low-level hardware access is prohibited and a separate I/O driver is needed. When we read the status of the port, we have to take the support of that third party application.
In inpout32.dll, all of the system drivers and small tools needed to activate and run that system driver are included. So, we don't have to consider much or think about how this happens; it will do everything for us…
As far as we are using a DLL file to communicate with the hardware, we have to know the links that this particular file provides for the user, or the entry points for that particular file. Here, I will describe all the entry points one by one.
DLL Entry used to read
DLL Entry used to output values
The Inp32
function can be used to read values from any address that is given through its PortAddress
parameter. Also, the Out32
function is specifically used to write values to data ports. A long description on both functions can be found below.
Reading a Pin (Status Register) or External Value
Inp32
is a general-purpose function defined inside inpout32.dll to communicate with various hardware addresses in your computer. This function is included in a DLL, so this file can be called within any programming language that supports DLL import functions.
Here, for this sample application, I have used Visual Basic .NET because it can be used by any person who knows a little about the .NET Framework. What you have to do to read the status of a parallel port is very simple. I will explain everything in steps, but keep in mind that in the sample coding I assume that the Address of the status register is 889 or 0x379h in hex.
- Open Visual Studio .NET (I made this in the .NET 2005 version).
- Make a new Visual Basic .NET Windows application.
- Go to the Project menu and Click on 'Add new Item.' Then select the Module item in the listbox.
- Type any considerable name for the new module that you're going to create, for example, porting.vb.
- In the Module file, there should be some text like this:
- Go to your form and add a new text box. Then add a Command button and, in the click event of the command button, paste this code:
This will work well only if the name of the text box you just made is
txtStatus
. - Run your application and click on the button so that the text should be 120 or some similar value. Then that is the status of your parallel port.
- If you want to check whether this application runs properly, you just have to take a small wire (screen wire or circuit wire is ok) and short a Status Pin and a Ground Pin. For example, Status pins are 10,11,12,13,15 and if you short the 10th pin and 25th (Ground) pin, then the value in the textbox will be 104. If you short some other pins, like the 11th and 25th, then the value of the text box will be 88 or a similar value.
Now you can clearly see that all the power is in your hands. If you're a creative person with a sound knowledge of electronics, then the possibilities are endless…
Note!
Before you run and compile this application, download inpout32.dll from this page and copy it to the Bin folder of your project directory, the place where the executable file is made, so that the DLL should be in the same directory to work. Alternatively, you can just copy and paste the DLL file to your Windows System directory and then you don't want to have that file in your application path. You only have to follow one guide.
Writing to a Pin (Data Register)
When writing data to the Data Register, you have to follow the same rules. There is no difference in basics! As you learned in the above context, do the same thing here with a few differences. I will explain to you what they are, but keep in mind that in the sample coding I assume that the address of the data register is 888 or 0x378h in hex.
- Go to the Project menu and Click on 'Add New Item.' Then select the Module item in the list box, the same as above.
- Type any considerable name for the new module that you're going to create, for example, porting.vb.
- In the Module file, there should be some text like this:
- Go to your form and add a Command button. In the Click event of the Command button, paste this code:
This will work properly only if the name of the text box you just made is
txtWriteVal
. Run your application and click on the Button so that the parallel port will output voltages in pins according to the value you entered in the text box.
How to Light up LEDs
Now to test the value that you output. Assume that you wrote value 2 to the data register and you want to know which pin outputs +5V. It's really simple to check. The data register starts from pin number 2 and ends in pin number 9, so there are 8 pins for output. In other words, it's an 8-bit register. So, when you write 2 to its data register, +5 voltage will be there in the 3rd pin. You have to take a scientific calculator and convert Decimal 2 to Binary; then the value is 2(DEC) = 1 0(BIN)
, so 1 0
means 0 0 0 0 0 0 1 0
is the status at the data register.
Example 2
If you write value 9
to a data register, then the binary of 9
is 0 0 0 0 1 0 0 1
. So, +5V can be taken from the 2nd pin and the 5th pin.
Binary Value | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 |
Data Register | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
Actual Pin number | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 |
Example 3
Assume you're writing 127
to a data register. Then the binary of 127
is 0 1 1 1 1 1 1 1
and therefore all the pins will be +5V, excluding the 9th pin.
Binary Value | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Data Register | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
Actual Pin number | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 |
When you want to light up an LED, there is a special way to connect them to a data register. So, you need some tools to connect. As a tutorial, I will explain how to connect only 1 LED.
Here you have to connect a ground pin to any pin starting from the 18th to 25th. All are ground pins, so there is no difference. The output pin or positive of the LED should be connected to a pin in the data register. When you write a value which enables that particular data register pin, then the LED will light up.
Note!
Never connect devices that consume more voltage than LEDs, as this would burn your parallel port or entire motherboard. So, use them at your own risk...! I recommend that you refer to the method written below if you're going through more advanced methods. If you want to switch electric appliances on and off, then you will obviously have to use relays or high voltage Triacs. I recommend using relays and connecting them through the method I have illustrated below.
More Advanced Way to Connect Devices to Parallel Port
(Compact 8 Channel Output Driver)
Labview Serial Port Examples
If you're not satisfied with lighting up LEDs and you're hungry to go further, then use this method to connect all your advanced or more complex devices to the Parallel Port. I would recommend that you consider using ULN2803 IC. This is an 8-channel Darlington Driver and it is possible to power up devices with 500mA and +9V with it.
Here in this diagram, you can see how to connect a set of LEDs to this IC. Also remember that you can replace these LEDs with any devices such as relays or Solenoids that support up to 500mA and they will run smoothly.
How Do the Surveillance System and Sample Application Run?
The surveillance system that I have written is very small and can only be used at home or for personal use. However, you can make it more advanced if you know how more complex security systems work. Here, the application has a timer and it always checks for status register changes. If something changes, then the application's status will be changed and a sound will be played accordingly.
Serial Port Pinout
This can be used to check whether your room's door is closed. What you have to do is place a push to turn on the switch on the edge of the door and you have to place it in a manner which activates when the door is opened. If you're not using a push to turn on the switch, then just place two layers of thin metal that can be bent and place them on the door. However, those two layers of metal should be able to make a short circuit when the door is closed. One piece of metal layering should be soldered to a screen wire (circuit wire) and that should be connected to the ground pin. The other layer of metal piece should be connected to a status pin of the parallel port. Status pins are 10, 11,12,13,15.
References
I have learned to program hardware ports since the year 2000. As far as I can remember, most of the time I used to get resources from Google.com, and I have used the links I found there... However, keep in mind that creativity cannot be searched from the web. It should be in your mind... So be creative and support the community...