printingcommand-promptdot-matrix

Sending raw data/printer commands to Printer


I understand that is possibly/slightly off topic here, but it is to do with programming a dot matrix printer.

I am trying to generate a new character for on OKI Microline 5520, and am trying to use the command line for this.

The command I am trying to send to the printer is:

CHR$(27);%a;@;CHR$(28);CHR$(34);CHR$(65);CHR$(0);CHR$(65);CHR$(0);CHR$(28);
CHR$(34);CHR$(73);CHR$(0);CHR$(73);

which should create a CE symbol instead of an @ character.

+-+-+-+-+-+-+-+-+-+-+-+
     X   X       X   X
+-+-+-+-+-+-+-+-+-+-+-+
   X           X
+-+-+-+-+-+-+-+-+-+-+-+
 X           X       
+-+-+-+-+-+-+-+-+-+-+-+
 X           X   X   X
+-+-+-+-+-+-+-+-+-+-+-+
 X           X
+-+-+-+-+-+-+-+-+-+-+-+
   X          X
+-+-+-+-+-+-+-+-+-+-+-+
     X   X       X   X
+-+-+-+-+-+-+-+-+-+-+-+
28| 65| 65| 28| 73| 73|
  |34 |0  |0  |34 |0

However, I can't seem to add/send this command to the printer, in a way it can understand.

i was trying the command within the command prompt:

net use Lpt1 \\ComputerName\\datFileName

but that didn't seem to work.

Would anyone have any advice on how I can send this command to this dot matrix printer?


Solution

  • As it turns out, there are several modes to these printers, and as such, it requires a specific hex string to be passed.

    There are also different commands based on whether it is 9 or 24 pins, and as such, it takes a lot of battling with the documentation in order to find out which command to send.

    I ended up using commands similar to these in order to write the command to memory.

    The program looked similar to this, with the hex data being sent to the printer

     [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
    
        [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool ClosePrinter(IntPtr hPrinter);
    
        [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level,  [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
    
        [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool EndDocPrinter(IntPtr hPrinter);
    
        [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool StartPagePrinter(IntPtr hPrinter);
    
        [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool EndPagePrinter(IntPtr hPrinter);
    
        [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
        public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
    
        // SendBytesToPrinter()
        // When the function is given a printer name and an unmanaged array
        // of bytes, the function sends those bytes to the print queue.
        // Returns true on success, false on failure.
        public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            Int32    dwError = 0, dwWritten = 0;
            IntPtr    hPrinter = new IntPtr(0);
            DOCINFOA    di = new DOCINFOA();
            bool    bSuccess = false; // Assume failure unless you specifically succeed.
    
            di.pDocName = "My C#.NET RAW Document";
            di.pDataType = "RAW";
    
            // Open the printer.
            if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )
            {
                // Start a document.
                if( StartDocPrinter(hPrinter, 1, di) )
                {
                    // Start a page.
                    if( StartPagePrinter(hPrinter) )
                    {
                        // Write your bytes.
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            // If you did not succeed, GetLastError may give more information
            // about why not.
            if( bSuccess == false )
            {
                    dwError = Marshal.GetLastWin32Error();
            }
            return bSuccess;