-1

I have some trouble with ESC/POS commands. The code below prints well regular text, but when it comes down the ESC/POS commands the printer does nothing. I have tried many different ways how to pass on the data (see cData1 & cData2). Can anyone help with pointing out the correct way how to pass the command? Thanks, Oliver.

HANDLE  CreateFileResult;
BOOL    bResult;
PSP_INTERFACE_DEVICE_DETAIL_DATA dummy = GetDevices();

if (dummy != 0)
    {
    CreateFileResult = CreateFile(dummy->DevicePath, 
                                      GENERIC_WRITE | GENERIC_READ, 
                                      FILE_SHARE_WRITE | FILE_SHARE_READ, 
                                      NULL, 
                                      OPEN_ALWAYS, 
                                      FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
                                      NULL);

        if (INVALID_HANDLE_VALUE == CreateFileResult) 
        {
          std::cout << "Handle Failed " << std::endl;
        }
        else
        {

          char cData1[] = { (CHAR)27, (CHAR)112 , CHAR(0), CHAR(255), CHAR(0) };
          char cData2[] = { 0x1b, 0x70 ,0x00 ,0xFF ,0x00 };

            DWORD dwBytesToWrite = (DWORD)strlen(cData2);
            DWORD bytesWritten;
            OVERLAPPED osWrite = { 0,0,0 };


            WriteFile(CreateFileResult, cData2, dwBytesToWrite, &bytesWritten, &osWrite);
         }
  • 5
    `strlen` won't work with a zero in there. Do you want the zeros to be sent to the printer? If "yes" you can use `dwBytesToWrite = sizeof(cData2);` – 001 Aug 02 '18 at 19:38
  • Yeah, what @JohnnyMopp said. You can fix it with `dwBytesToWrite = sizeof(cData2);`. And are you absolutely sure that the printer will respond properly to this sequence? There's a lot of abstraction in the printing process. – Mark Ransom Aug 02 '18 at 19:43
  • I am working with EPSON TM-T20II, the command to open the cash drawer from Epson's webpage is Hex 1B, 70, m, t1, t2 m = 0,1,48,49 t1 & t2 = 0 - 255 So it should be correct. I have actually written something similar in C# (but using the Windows.Devices namespace classes to communicate with the printer) and there passing on (char)27 + (char)112 + (char)0 + (char)255 + (char)0 works perfectly. – user3077276 Aug 02 '18 at 19:52

1 Answers1

0

So after hours of debugging I found out there are 2 problems with my code: 1) For some reason the ESC/POS command that works with C# + Windows.Devices.USB namespace doesn't work with the current WriteFile approach. My guess is that Windows.Devices.USB does a lot more behind the scenes and somehow manages to make it work. The command that works with simply writefile is 0x1B, 0x70, 0x00, 0x98, 0x00. 2) As written by @JohnnyMopp & @MarkRanson the way how I was measuring buffer size was wrong. Thank you very much for helping out.