Read Epson Printer Status using C++ and ESC/P

Joined
Aug 12, 2011
Messages
4
I'm trying to get the status of a PLQ-20 Espon printer, using C++, but with no success.

I tried that using GDI API and Escape function with PASSTHROUGH parameter, but the printer never understands the escape codes with that function.

I tried to use WIN 32 API and the example code found here. That works for sending some escape codes like BEL (to sound the buzzer) or FF (Form Feed, to eject paper from the rear of the printer), but not ESC O (to eject paper from the front of the printer), ESC 0 / ESC 1 (to initialize the printer / reset errors).

So, I tried this way to get the status of the printer with a ESC j escape code but with no success (the ReadPrinter function returns 0). Moreover, the print buffer seems to be not empty nonetheless I only send escape commands.

I don't know if I do a mistake sending escape codes or trying to read the printer status.

If anyone could post examples, it could be fine for everyone because it's hard to find them on the web.

Below is the code I use to send commands and read the result


Code:
// Returns: void
// 
void PrintError( DWORD dwError, LPCTSTR lpString )
{
#define MAX_MSG_BUF_SIZE 512
    TCHAR   *msgBuf;
    DWORD   cMsgLen;

    cMsgLen = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_ALLOCATE_BUFFER | 40, NULL, dwError,
                MAKELANGID(0, SUBLANG_ENGLISH_US), (LPTSTR) &msgBuf,
                MAX_MSG_BUF_SIZE, NULL );
    printf("%s Error [%d]:: %s\n", lpString, dwError, msgBuf );
    LocalFree( msgBuf );
#undef MAX_MSG_BUF_SIZE
}
// end PrintError
// **********************************************************************

// **********************************************************************
// ReadFileWithAlloc - allocates memory for and reads contents of a file
// 
// Params:
//   szFileName   - NULL terminated string specifying file name
//   pdwSize      - address of variable to receive file bytes size
//   ppBytes      - address of pointer which will be allocated and contain file bytes
// 
// Returns: TRUE for success, FALSE for failure.
//
// Notes: Caller is responsible for freeing the memory using GlobalFree()
// 
BOOL ReadFileWithAlloc( LPTSTR szFileName, LPDWORD pdwSize, LPBYTE *ppBytes )
{
    HANDLE      hFile;
    DWORD       dwBytes;
    BOOL        bSuccess = FALSE;

    // Validate pointer parameters
    if( ( pdwSize == NULL ) || ( ppBytes == NULL ) )
        return FALSE;
    // Open the file for reading
    hFile = CreateFile( szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
    if( hFile == INVALID_HANDLE_VALUE )
    {
        PrintError( GetLastError(), TEXT("CreateFile()") );
        return FALSE;
    }
    // How big is the file?
    *pdwSize = GetFileSize( hFile, NULL );
    if( *pdwSize == (DWORD)-1 )
        PrintError( GetLastError(), TEXT("GetFileSize()") );
    else
    {
        // Allocate the memory
        *ppBytes = (LPBYTE)GlobalAlloc( GPTR, *pdwSize );
        if( *ppBytes == NULL )
            PrintError( GetLastError(), TEXT("Failed to allocate memory\n") );
        else
        {
            // Read the file into the newly allocated memory
            bSuccess = ReadFile( hFile, *ppBytes, *pdwSize, &dwBytes, NULL );
            if( ! bSuccess )
                PrintError( GetLastError(), TEXT("ReadFile()") );
        }
    }
    // Clean up
    CloseHandle( hFile );
    return bSuccess;
}
// End ReadFileWithAlloc
// **********************************************************************

// **********************************************************************
// RawDataToPrinter - sends binary data directly to a printer
// 
// Params:
//   szPrinterName - NULL terminated string specifying printer name
//   lpData        - Pointer to raw data bytes
//   dwCount       - Length of lpData in bytes
// 
// Returns: TRUE for success, FALSE for failure.
// 
BOOL RawDataToPrinter( LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount )
{
    HANDLE     hPrinter;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob;
    DWORD      dwBytesWritten;

    // Need a handle to the printer.
    if( ! OpenPrinter( szPrinterName, &hPrinter, NULL ) )
    {
        PrintError( GetLastError(), TEXT("OpenPrinter") );
        return FALSE;
    }

    // Fill in the structure with info about this "document."
    DocInfo.pDocName = TEXT("My Document");
    DocInfo.pOutputFile = NULL;
    DocInfo.pDatatype = TEXT("RAW");
    // Inform the spooler the document is beginning.
    if( (dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo )) == 0 )
    {
        PrintError( GetLastError(), TEXT("StartDocPrinter") );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Start a page.
    if( ! StartPagePrinter( hPrinter ) )
    {
        PrintError( GetLastError(), TEXT("StartPagePrinter") );
        EndDocPrinter( hPrinter );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Send the data to the printer.
    if( ! WritePrinter( hPrinter, lpData, dwCount, &dwBytesWritten ) )
    {
        PrintError( GetLastError(), TEXT("WritePrinter") );
        EndPagePrinter( hPrinter );
        EndDocPrinter( hPrinter );
        ClosePrinter( hPrinter );
        return FALSE;
    }

    /*********************************/
    // CODE USED TO READ THE PRINTER
    LPBYTE retData = NULL;
    LPDWORD bbr = NULL;

    if(ReadPrinter(hPrinter, retData, 1, bbr))
    {
        printf("OUT : %i", retData);
    }
    else
    {
        printf("Failed to read printer");
    }
    /*********************************/

    // End the page.
    if( ! EndPagePrinter( hPrinter ) )
    {
        PrintError( GetLastError(), TEXT("EndPagePrinter") );
        EndDocPrinter( hPrinter );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Inform the spooler that the document is ending.
    if( ! EndDocPrinter( hPrinter ) )
    {
        PrintError( GetLastError(), TEXT("EndDocPrinter") );
        ClosePrinter( hPrinter );
        return FALSE;
    }
    // Tidy up the printer handle.
    ClosePrinter( hPrinter );
    // Check to see if correct number of bytes were written.
    if( dwBytesWritten != dwCount )
    {
        //printf( TEXT("Wrote %d bytes instead of requested %d bytes.\n"), dwBytesWritten, dwCount );
        return FALSE;
    }
    return TRUE;
}
// End RawDataToPrinter
// **********************************************************************

int main( int argc, char* argv[] )
{
    LPBYTE  pBytes = NULL;

    int textSize = 2;

    DWORD   dwSize = textSize;

    pBytes = (LPBYTE) malloc (textSize*sizeof(BYTE));

    pBytes[0] = 0x1B;
    pBytes[1] = 0x6A;


    if( ! RawDataToPrinter(L"EPSON PLQ-20 ESC/P2", pBytes, dwSize) )
        printf("Failed to send data to printer.\n" );
    else
        printf("Data sent to printer.\n" );

    free(pBytes);
    return 0;
}
// end main
// **********************************************************************

Thanks!
 

duf

Joined
Oct 12, 2011
Messages
4
Do you have found a solution to the problem. I ask because I have the same problem with dot OKI printer.
In contrast to your function I used other types of data in function:
Code:
bool RawDataToPrinter( wchar_t* szPrinterName, char* lpData, unsigned int dwCount )
I do not know how in this case send a command with the text. Is it possible?
Code:
char *cSendBuf = "text\0x1b%9123";
 
Joined
Aug 12, 2011
Messages
4
I don't have a really solution to this problem. This function is working well when sending text and/or commands. The problem is not here.

There are several emulation modes on the printer (Epson, Olivetti, ...) and using Epson printer with Epson simulation mode, it is not possible to get the printer's status ...

I explained the problem on stack overflow (I'm not able to post URLs on this forum so you can search for 7039673 in the StackOverflow search bar).

Check if it is not a problem with emulation.

If you are not trying to get the status of your printer, but send raw data, you should open a new thread on this forum.
 
Joined
Aug 12, 2011
Messages
4
How can I do it?

First, check on the OKI website which emulations are available for your printer (you can see it on the product description).

Next, check if you are using the commands associated with this language

And finally, check which emulation is used on your printer.

You can look at the user's guide on OKI website.
 

duf

Joined
Oct 12, 2011
Messages
4
First, check on the OKI website which emulations are available for your printer (you can see it on the product description).
Default IBM
EPSON FX

Next, check if you are using the commands associated with this language
I think, I`m. I found a page with commands for OKI

http:
//my.okidata.com/man4410.nsf/0c51a224bef4a04785256941004ffbbf/ea556226e0fd0b51852566e20049a75a?OpenDocument

And finally, check which emulation is used on your printer.
EPSON FX
I printed settings from the printer.

Thank You.

I took advantage of your example:
Code:
  LPBYTE cSendBuf = NULL;

  int textSize = 5;

  DWORD dwSize = textSize;

  cSendBuf = (LPBYTE) malloc (textSize*sizeof(BYTE));

  cSendBuf[0] = 0x18; // clear print buffer
  
  // set left margin
  cSendBuf[1] = 0x1B;
  cSendBuf[2] = 0x6C;
  cSendBuf[3] = 0xA;
  
  // T letter
  cSendBuf[4] = 'T';
Do you know may be a friendlier way to send commands with the text?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top