Home Parallel Port Serial Port USB .NET Webboard I/O Today API Project Link


 Hardware Interfacing in C# .NET framework programming.

Parallel Port Interfacing in C#.NET Programming
Thai Page Click Here

Introduce


Before coding I would like to take some information about reaching the ports by using a language and OS.
When we was using Windows 98 we could reach the ports with a function which is "outportb".
You can not reach the ports directly in NT, 2000 and XP because of their kernel and their printer drivers.
Let me prove this: First make the circuit as in the pic and then shutdown your computer, start it,
if you have multi boot menu select Windows 98, when it starts there is no light up in the circuit but in Win2000 and XP all
the 8 lights are on so we know that the signal is coming and the pins are registered by their kernel with the printerdriver.

Coding to do the interfacing
We can easily program the parallel port in DOS. But as we know, DOS programs have their own limitations.
So, if you want to move from DOS to Windows, go through this article.
This is an introduction to program the parallel port in VC++. You need not have much knowledge about VC++.
This article is designed for one who know basics of parallel port and beginners of VC++.
If you don't know anything about parallel port, read my first article "Parallel port programming with C (Part 1)".
There you get basic information about parallel port and programming the port in Turbo C or Borland C.

Download Source Code + inpout32.dll files
We used inpout32.dll in us program for interoping.
You can download the inpout32.dll after get it to
path name is "Windows/System32 ".

Note: I am importing it with the reference of http://www.logix4u.net/ So for further info about the driver check out the site

Creatting Public Class PortAccess with Import DLL
In Form1.cs is main form that you will coding in view code window by used the following thus below.

using System;
using System.Runtime.InteropServices;

namespace ParallelPort
{
   Private class PortAccess
  {
   Call OutPut function from DLL file.
     [DllImport("inpout32.dll", EntryPoint="Out32")]
     public static extern void Output(int adress, int value);

   Call Input functionfrom DLL file
     [DllImport("inpout32.dll", EntryPoint="Inp32")]
     public static extern void Input( int adress);
  }

..........................................
...............................

If you have already to import inpout32.dll to your debug or release directory.
By the way, the main thing in us Form1.cs is PortAccess.Output.
It takes two variables which are address and value. If your data ports are set in "0x378" (see Part 1) you will have to write "888" because "378" Hexadecimal is equal to "888" in decimal. (Default LPT1 is set to "378") If you are using LPT2 which is "0x278" you have to write for the address "632" For ex: for full signaling to pins we have to call the Output method of PortAccess like:

PortAccess.Output(888, 255);

And for null data we have to send "0" to the Output method like:

PortAccess.Output(888, 0);

I wrote a func for reseting the LEDs which is:

private void Reset_LEDs()
{ PortAccess.Output(adress, 0); }

I didn't use loops for checkboxes and pictureboxes you can also enumerate these for quick coding. First, I do like that but after I changed to several if-else statements because I had to change the GUI. But I left them on the code for performance issues anyone who want speed can use these.
You can always use this program to test the parallel port. Now, make a circuit connecting all the input pins to switches, all the output pins to LEDs with 2.2K or 10K resisters. If you press switch, corresponding pin value should change in the screen, If you change state of any output pin, corresponding LED should glow.
Every thing is ok. But as you know, this program will run only in win9x. If your program is needed to run in windows xp and higher versions, you need to write a kernel mode device driver. Do not worry if you are not up to that level. There are DLL files available freely for such drivers. You can use those files and call them from your program.

Requirements:
  • Microsoft Visual Studio.NET Professional or greater.
  • Windows 2000 or Windows XP.
  • A modem installed on one of the Comm Ports. (for example most laptops include a modem).
Running the Project:
To run the Project:
  • Unzip the code into a directory of your choice.
  • Open ParallelPortInterfacing.sln file.
  • Click the buttons on the form to test the functionality.
  • Examine the code in the project to understand how the code works.



Copyright (c) 2000 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com