Sending raw data/printer commands to Printer

女生的网名这么多〃 提交于 2019-12-12 02:26:52

问题


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?


回答1:


This command could help: copy /B afile.bin LPT1:, where that afile.bin should contain your printer command string. Viewed in a hexadecimal editor (with × in ascii display for any nonprintable character):

hexa  1B2561401C22410041001C22490049
ascii  × % a @ × " A × A × × " I × I

This copy /B afile.bin LPT1:, performed from command line, brings no cruces.

But before sending a file to the printer, many word processors send either an “initialization string” or an I-Prime signal to the printer. Your printer user's guide should give a procedure to ignore the reset code and eliminate possible I-Prime signal problems derived from your word processor.




回答2:


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;


来源:https://stackoverflow.com/questions/27187463/sending-raw-data-printer-commands-to-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!