Home > Enterprise >  Gebe Thermalprinter Escapecodes
Gebe Thermalprinter Escapecodes

Time:10-21

I'm using a Gebe Thermal Printer (GeBE-COMPACT Plus GPT-4672) and have the following issue:

The printer (like most known thermal printer) uses a set of unique escape commands to use its functionality. I'm writing a .Net application thats sending a printjob to the printer, which works fine, but afterwards, I need to send an Escape sequence to cut and to release the paper. The company has issued a test application which takes a Command and sends it to the printer. The function itself however is stuck in a dll file which I can't access, and also can't use, because the issued dll is not supported by the final targeted system, so I'm trying to create a workaround. The printer is connected via USB.

The command for cutting the paper is as follows (literal):< ESC>C<0d>. Inserting this exact line into the testing program without the added space in between < and ESC works fine. I have used the code provided on the MSDN page to be able to send raw text to the printer. Sending this line of text via my method however just makes it print the lines onto the paper, except for the Escape. I've tried converting the sequence into hex, to no prevail. I've tried to send the text as binary data, also didn't work. It feels like it only sees the Escape Sign and ignores the remaining portion of the string. Does anyone have an idea how to fix this issue? I've spent the past hours, converting back and forth, and having used almost 12 feet of paper because it just won't work.

EDIT:

As requested, The Sourcecode:

Function for the Cut paper button:

        private void BCut_Click(object sender, EventArgs e)
    {
        String output = "<ESC>C<0d>";
        RawPrinterHelper.SendStringToPrinter("Gebe_Drucker", output);
    }

Code for the "SendStringToPrinter" Function, which calls the sendbytestoprinter function:

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;
        }

        public static bool SendStringToPrinter(string szPrinterName, string szString)
        {
            IntPtr pBytes;
            Int32 dwCount;
            // How many characters are in the string?
            dwCount = szString.Length;
            // Assume that the printer is expecting ANSI text, and then convert
            // the string to ANSI text.
            MessageBox.Show(szString);
            pBytes = Marshal.StringToCoTaskMemAnsi(szString);
            // Send the converted ANSI string to the printer.
            SendBytesToPrinter(szPrinterName, pBytes, dwCount);
            Marshal.FreeCoTaskMem(pBytes);
            return true;
        }

Thats about it. I hope that helps.

CodePudding user response:

You can't just write "< ESC>C<0d>" as string - < ESC> and <0d> are special codes. < ESC> is for binary 27 and <0d> for binary 0. You have to construct the string e.g. in this way (hexadecimal coded): String output = "\u001BC\u0000";

  • Related