Sign in to follow this  
Followers 0
amakrkr

[PRINER]Problem reading USB port?

3 posts in this topic

Hello everyone,

I am kinda new to VC++ and i was wondering if someone could give me a hand.

I got this printer and it came with .dlls and now i want to write application that will alert me if printer has run out of paper.

So far so good ... i have googled it a lot and came up with a code that actually works (yay!) but only on one specific USB port ... if i connect my printer to another port ... program stops working and it returns an error "Out of paper!" even tho printer is loaded with it.

As i said i am slowly learing how to code in VC++ (thats the reason why i havent made it in AutoIt) but i am really stuck with this.

I hope someone can help me ... code is attached below the post.

Thank you!

PS Sorry about my English >.<

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <fstream>
using namespace std;
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )



#define TSP100_PRINTER_NAME _T("Star TSP100 Cutter (TSP143)")


#define TSP100_LM_NAME      _T("SOFTWARE\\StarMicronics\\STLMPLUG")



typedef struct StarPrinterStatus_
{
  // printer status 1
  BOOL coverOpen;
  BOOL offline;
  BOOL compulsionSwitch;

  // printer status 2
  BOOL overTemp;
  BOOL unrecoverableError;      
  BOOL cutterError;
  BOOL mechError;               

  // printer status 3
  BOOL pageModeCmdError;        
  BOOL paperSizeError;          
  BOOL presenterPaperJamError
  BOOL headUpError;             

  // printer status 4
  BOOL blackMarkDetectStatus;   
  BOOL paperEmpty;
  BOOL paperNearEmptyInner;     
  BOOL paperNearEmptyOuter;     

  // printer status 5
  BOOL stackerFull;             

  // printer status 6
  BOOL etbAvailable;
  BYTE etbCounter;

  // printer status 7
  BYTE presenterState;          

  // raw
  DWORD rawLength;
  BYTE raw[63];
} StarPrinterStatus;

#ifdef UNICODE
#define OpenPort "OpenPort"  

typedef HANDLE (WINAPI * OPENPORT)(LPCWSTR, LPCWSTR);

#define GetPrinterSerialNumber "GetPrinterSerialNumberW"  
typedef BOOL   (WINAPI * GETPRTSRNO)(LPCWSTR, LPWSTR, DWORD, DWORD *);
#else//!UNICODE

#define OpenPort "OpenPortA"
typedef HANDLE (WINAPI * OPENPORT)(LPCSTR, LPCSTR);
#define GetPrinterSerialNumber "GetPrinterSerialNumberA"
typedef BOOL   (WINAPI * GETPRTSRNO)(LPCSTR, LPSTR, DWORD, DWORD *);
#endif

typedef LONG   (WINAPI * CLOSEPORT)(HANDLE);
typedef LONG   (WINAPI * STATUSCHK)(HANDLE, StarPrinterStatus *);



void StatusCheck(HMODULE hTSP100LMDLL)
{
    DWORD               Length   = 0;
    LPBYTE              pBuf     = NULL;
    StarPrinterStatus   status;
    HANDLE              Port;
    wchar_t             raw[256];
    DWORD               i        = 0;
    int                 j        = 0;

    ofstream myfile;  
    myfile.open ("status.bin", ios::out | ios::binary | ios::trunc); 



    GETPRTSRNO TSP100_GetPrinterSerialNumber = (GETPRTSRNO)GetProcAddress(hTSP100LMDLL, GetPrinterSerialNumber);
    
    
    (* TSP100_GetPrinterSerialNumber)(TSP100_PRINTER_NAME, NULL, 0, &Length );
    
    pBuf = (LPBYTE)malloc(Length * 2);
    (* TSP100_GetPrinterSerialNumber)(TSP100_PRINTER_NAME, (LPWSTR)pBuf, Length, &Length);

    
    OPENPORT TSP100_OpenPort = (OPENPORT)GetProcAddress(hTSP100LMDLL, OpenPort);

    Port = (* TSP100_OpenPort)( (LPCWSTR)pBuf, (LPCWSTR)"");
    free(pBuf);

    STATUSCHK TSP100_StatusCheck = (STATUSCHK) GetProcAddress(hTSP100LMDLL, "GetParsedStatus");
    (* TSP100_StatusCheck)( (HANDLE)Port, &status );

    
    // printer status 1
    //if(status.coverOpen                != 0) { myfile <<"Cover open!";                }else{ myfile <<"";                             }
    //if(status.offline                  != 0) { wprintf(_T("Printer Off-line!"));          }else{ myfile<<"";  }
    //if(status.compulsionSwitch         != 0) { wprintf(_T("Error 1!"));               }else{ myfile<<"";  }

    // printer status 2
    //if(status.overTemp                 != 0) { wprintf(_T("Error  2!"));              }else{ myfile<<"";  }
    //if(status.cutterError              != 0) { wprintf(_T("Blade error!"));                   }else{ myfile<<"";  }

    // printer status 4
    if(status.paperEmpty                != 0) { myfile<<"Out of paper!";    } else{ myfile<<""; }

    // printer status 6
    //if(status.etbAvailable             != 0) { wprintf(_T("Error  3!"));              }else{ myfile<<"";  }


    CLOSEPORT TSP100_ClosePort = (CLOSEPORT)GetProcAddress(hTSP100LMDLL, "ClosePort");
    
    (* TSP100_ClosePort)((HANDLE)Port);


    myfile.close();
}





int main( int argc, WCHAR* argv[] )
{
    HKEY    hKey;
    DWORD   dwSize;
    BYTE    tsp100lmDllPath[256];
    HMODULE hTSP100LMDLL = NULL;




    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TSP100_LM_NAME, 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
    {
        return 0;
    }
    if(RegQueryValueEx(hKey, _T("Update"), NULL, NULL, tsp100lmDllPath, &dwSize) != ERROR_SUCCESS)
    {
        if(RegQueryValueEx(hKey, NULL, NULL, NULL, tsp100lmDllPath, &dwSize) != ERROR_SUCCESS)
        {
            RegCloseKey(hKey);
            return 0;
        }
    }

    hTSP100LMDLL = LoadLibrary((LPCWSTR)tsp100lmDllPath);
    if(hTSP100LMDLL == NULL)
    {
        return 0;
    }



    StatusCheck(hTSP100LMDLL);  



    FreeLibrary(hTSP100LMDLL);


    RegCloseKey(hKey);

    return 0;
}

Share this post


Link to post
Share on other sites



You don't need to work at such a low level to find out if your printer is out of paper. Windows has APIs for that, which will work with any kind of printer and connection.

GetPrinter

Printer status flags

PRINTER_STATUS_PAPER_OUT is probably the status flag you'll want to check.

Share this post


Link to post
Share on other sites

You don't need to work at such a low level to find out if your printer is out of paper. Windows has APIs for that, which will work with any kind of printer and connection.

GetPrinter

Printer status flags

PRINTER_STATUS_PAPER_OUT is probably the status flag you'll want to check.

Thank you for your replay. I will try it out tomorrow and let you know how it goes!

thx :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0