Ian
Ian

Reputation: 1263

C# - Read Parallel Port State (Simple Push Switch)

I am updating in C# some software that was originally written in VC++ V1.0 for DOS6.

One aspect of the software is a check on the parallel port, where a simple push-button switch is connected. I don't currently know to which two pins the switch is connected, but I have the source for the old program, the relevant sections of which are below...

#define switch_mask        0x10

void main(int argc, char *argv[])
{
    int NewState = 0, OldState = 0;

    /* Check switch */
    NewState = _bios_printer (_PRINTER_STATUS, 0, 0);

    if (NewState != OldState)
    {
    checkParallelPort (NewState);
    OldState = NewState;
    }    
}

void checkParallelPort (int portState)
{
    int SwitchState;
    /* Switch bit is Active LOW */
    SwitchState = (portState & switch_mask) ? 1 : 0;
}

Now _bios_printer (within bios.h) is obviously not available to me in C#, but I'm struggling to find an alternative that can do this simple task.

Info on _bios_printer is here. I've done plenty of searching for reading/writing to/from the parallel port in .Net, but nothing seems to provide me with the port status.

Also, can you conclude from this code (and how it checks the 'status') where the two switch wires are connected on the DB25 plug?

I'd be grateful if anyone has some help/advice on this please. Many thanks

Upvotes: 0

Views: 3300

Answers (2)

Ian
Ian

Reputation: 1263

Thanks for the reply. Here's what I ended up with...

I used this tutorial on CodeProject... http://www.codeproject.com/KB/vb/Inpout32_read.aspx When converted to C#, I used something similar to below. (Apologies if there is an error - I 'paraphrased' the code below from what i've ended up with - it works for me!)

private void button1_Click(object sender, EventArgs e)
{

        short InReg = 0x379;    // Location of Port Status register
        int NewState;           // int named NewState

        NewState = InpOut32_Declarations.Inp(InReg);   //Now NewState has the values in 'Status port'

        MessageBox.Show(NewState);   //A popup will indicate the current value of NewState 

}

static class InpOut32_Declarations
{
    [DllImport("inpout32.dll", EntryPoint = "Inp32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

    //Inp and Out declarations for port I/O using inpout32.dll.

    public static extern int Inp(ushort PortAddress);
    [DllImport("inpout32.dll", EntryPoint = "Out32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

    public static extern void Out(ushort PortAddress, short Value);

}

Interestingly, as my Parallel Port starts at 0xE800 and not 0x378, I had to modify the source of inpout32.dll as the out32 method only accepts a short and 0xE800 is too big for a short! changed it to unsigned short.

Thanks for your help

Upvotes: 0

Martin James
Martin James

Reputation: 24847

It seems to be checking 'Error', pin 15. IIRC, this is pulled up internally, so your switch should pull down pin 15. Connect it between pins 15 and 18.

There are some drivers available that allow the reading of the I/O map ports. You will have to import and make DLL calls and then almost certainly poll the pin :((

I do wish this interface was dead and buried!

Upvotes: 1

Related Questions