Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="text"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.>

C++
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);
         }


What I have tried:

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.
Posted
Updated 2-Aug-18 11:20am

The problem is you are using strlen to determine the length of the data but there are nulls in the string and strlen stops counting when it finds the first one. For example, cData1 has five bytes and strlen will return 2.

You should just count the bytes and set a constant to that value (4 or 5 - I don't know if you want the last null) and use that to test your logic. When you get that working correctly then you can make it more flexible and general purpose.
 
Share this answer
 
It does not work because you are using strlen() to get the length of data to be written but your data is binary and contains NULL bytes. So you are writing only two bytes (0x1B and 0x70) which is an incomplete command. Using strlen() won't even work properly when the data does not contain a NULL byte when not appending one as string terminator.

Because you have a char array with fixed size, you can use the array size instead:
DWORD dwBytesToWrite = sizeof(cData2);
That will write all five bytes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900