Jan 20, 2017 at 4:59am UTC
I need to send a string or char to a "POS Receipt Printer" via USB.
My problem is how to establish a communication with the printer by USB port in C++ (I'm using MFC)
I tried this:
HANDLE dev = CreateFile(
"\\\\.\\USB001",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(dev!=INVALID_HANDLE_VALUE)
{
DWORD dwWritten = 0;
LPCSTR lpszTest = "Hello world!";
::WriteFile(dev, lpszTest, strlen(lpszTest), &dwWritten, NULL);
::CloseHandle(dev);
}
but, it returns by GetLastError() the value = 2 (Can not find the specified file)
Also, I tried this:
HANDLE dev;
CString s("\\\\.\\USB001");
LPWSTR p=(LPWSTR)(LPCTSTR)s;
if(OpenPort(p,(PHANDLE)&dev))
{
CString str = "Hello world!";
LPBYTE pByte = new BYTE[str.GetLength()+1];
memcpy(pByte,(VOID*)LPCTSTR(str),str.GetLength());
LPDWORD r;
r=0;
WritePort(dev,pByte,6,r);
delete [] pByte;
}
But I got frustrated,
due that OpenPort and WritePort belongs to #include <winsplp.h> and <winspool.h> , so it generates a lot of dependencies(Libs,dlls) that escapes my knowledge.
If anyone can help me, I'll really appreciate it.
Jan 21, 2017 at 12:20am UTC
Thank you Thomas. It worked!
This link was the key!
The whole day prowling, analyzing this example code, finally I got the solution.
Very thankful.