Send data to POS Printer by USB
-
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() a 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 and , so it generates a lot of dependencies (libs,dlls) that escapes my knowledge. If anyone can help me, I will be really grateful. Yany
-
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() a 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 and , so it generates a lot of dependencies (libs,dlls) that escapes my knowledge. If anyone can help me, I will be really grateful. Yany
To open a handle to a USB device, you have to use the SetupDixxx APIs to enumerate and find the device. Here is an example for enumerating USB devices - http://www.velleman.eu/images/tmp/usbfind.c[^] Once the correct VID/PID pair is found, you can use the
DevIntfDetailData->DevicePath
in a call toCreateFile
.«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
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() a 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 and , so it generates a lot of dependencies (libs,dlls) that escapes my knowledge. If anyone can help me, I will be really grateful. Yany
Just a guess, but have you tried something like:
CreateFile("\\\\.\\USB#VID_0781&PID_5530#077443117B924E40#{a5dcbf10-6530-11d2-901f-00c04fb951ed}", ...);
or if your USB drive has a drive letter mapped to it:
CreateFile("\\\\.\\D:", ...);
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles