I am writing an interface program for a remotely controlled vehicle.
I need help in getting a code to access the parallel port from the program. i.e generate some outputs at the parallel port for a particular keypress.

I had to do that to open a quickbooks cash drawer once. Here is how I went about it:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

...

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern SafeFileHandle CreateNamedPipe(
       String pipeName,
       uint dwOpenMode,
       uint dwPipeMode,
       uint nMaxInstances,
       uint nOutBufferSize,
       uint nInBufferSize,
       uint nDefaultTimeOut,
       IntPtr lpSecurityAttributes);
    /* -------------------------------------------------------------------- */
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern int ConnectNamedPipe(
       SafeFileHandle hNamedPipe,
       IntPtr lpOverlapped);
    /* -------------------------------------------------------------------- */
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern SafeFileHandle CreateFile(
       String pipeName,
       uint dwDesiredAccess,
       uint dwShareMode,
       IntPtr lpSecurityAttributes,
       uint dwCreationDisposition,
       uint dwFlagsAndAttributes,
       IntPtr hTemplate);
    /* -------------------------------------------------------------------- */
    public static void OpenRegister()
    {
#if (DEBUG)
      DevExpress.XtraEditors.XtraMessageBox.Show("Cash drawer opened.", "Shotgun Hill", MessageBoxButtons.OK, MessageBoxIcon.Information);
#else
      try
      {
        uint GENERIC_READ = (0x80000000);
        uint GENERIC_WRITE = (0x40000000);
        uint OPEN_EXISTING = 3;
        uint FILE_FLAG_OVERLAPPED = (0x40000000);

        SafeFileHandle pipeHandle =
           CreateFile(
              "LPT1",
              GENERIC_READ | GENERIC_WRITE,
              0,
              IntPtr.Zero,
              OPEN_EXISTING,
              FILE_FLAG_OVERLAPPED,
              IntPtr.Zero);

        //could not get a handle to the named pipe
        if (pipeHandle.IsInvalid)
          return;

        using (FileStream fStream = new FileStream(pipeHandle, FileAccess.Write))
        {
          char cOp = Convert.ToChar(7);
          byte bOp = Convert.ToByte(cOp);
          fStream.WriteByte(bOp);
          fStream.Flush();
          fStream.Close();
        }
      }
      catch { }
#endif
    }

I didn't have the cash drawer locally so I made it show a messagebox when I was debugging, and when it was in customer (release) mode it would actually open the drawer.

I was shooting trap at my gun club and they had a quickbooks cash drawer and it died on them so I wrote them an application to handle transactions :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.