Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Send data to POS Printer by USB

Send data to POS Printer by USB

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12827479
    wrote on last edited by
    #1

    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

    _ D 2 Replies Last reply
    0
    • U User 12827479

      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

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      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 to CreateFile.

      «_Superman_»  _I love work. It gives me something to do between weekends.

      _Microsoft MVP (Visual C++) (October 2009 - September 2013)

      Polymorphism in C

      1 Reply Last reply
      0
      • U User 12827479

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups