low level printing Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
FireMail
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Jun 10, 2005 8:34 am
Location: Austria
Contact:

low level printing

Post by FireMail » Fri Feb 22, 2008 11:24 am

hi there,

i'm currently writing an application that communicates with, lets say: hardware ;)
this hardware can communicate with rs232 (already implemented and working) and usb.
The funny thing is: there are no usb drivers, when installing this hardware to work with usb it creates a printer. For controling this hardware you have to print on it :twisted:

Now here is my question:
i generate the commands i want to print with my software and only want to stream these commands to the printer. no html, no pdf, no grafic ... simply my 2 million commands. i searched through forums and also the documentation, but there is no low level communication with printers, or am i wrong?

hope you can give me a better answer, kind regards
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GB/CS/CM/IT !d++ s+:-- a-- C++++$ UBL*++++$ P--- L++++$ !E-- !W+++$? !N-- !o K--? w++()$ !O M$ !V !PS? !PE? !Y? !PGP !t !5 !X R+++ tv++ !b? DI D++ G e+++ h++ r++ y+
------END GEEK CODE BLOCK------

FireMail
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Jun 10, 2005 8:34 am
Location: Austria
Contact:

Post by FireMail » Fri Feb 22, 2008 7:22 pm

i solved the problem by using native winapi functions. but maybe there is a good wx solution so that its easily portable?
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GB/CS/CM/IT !d++ s+:-- a-- C++++$ UBL*++++$ P--- L++++$ !E-- !W+++$? !N-- !o K--? w++()$ !O M$ !V !PS? !PE? !Y? !PGP !t !5 !X R+++ tv++ !b? DI D++ G e+++ h++ r++ y+
------END GEEK CODE BLOCK------

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Thu Feb 28, 2008 11:16 am

You can use streaming files for printing. This works for Windows as well as for Linux. There are lowlevel access functions to do so. They are undocumented. You will need to go through the sources (wx/filefn.h).

You can use wxOpen, wxRead, wxWrite, wxClose for your purpose.
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

FireMail
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Jun 10, 2005 8:34 am
Location: Austria
Contact:

Post by FireMail » Thu Feb 28, 2008 2:07 pm

for printers?

have you any examples?
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GB/CS/CM/IT !d++ s+:-- a-- C++++$ UBL*++++$ P--- L++++$ !E-- !W+++$? !N-- !o K--? w++()$ !O M$ !V !PS? !PE? !Y? !PGP !t !5 !X R+++ tv++ !b? DI D++ G e+++ h++ r++ y+
------END GEEK CODE BLOCK------

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Fri Feb 29, 2008 7:03 am

Oh... I looked at my sources and saw that I'm using native Windows API on Windows and generic way elsewhere. Anyway, I give you some relevant lines / pieces. Maybe you can adapt them to your code. But I need to say that I'm no specialist in such things.

Code: Select all

// header
#if defined(__WXMSW__) || defined(__WIN95__)

  #include <windows.h>
  #include <winbase.h>

#else

  #include <fcntl.h>
  #include <termios.h>
  #include <unistd.h>
  #include <string.h>

  #include <wx/file.h>
  #include <wx/ffile.h>
  #include <wx/utils.h>

#ifndef HANDLE
  #define HANDLE int
#endif

#ifndef INVALID_HANDLE_VALUE
  #define INVALID_HANDLE_VALUE -1
#endif

#ifndef DWORD
  #define DWORD unsigned int
#endif
  
#endif

  #include <wx/filefn.h>




HANDLE printHandle;
wxString mszPrinterName;

Code: Select all

// implementation

// open
#if defined(__WXMSW__) || defined(__WIN95__)

    printHandle = CreateFile (mszPrinterName.c_str(),
        GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL
        );

    if((int)printHandle<0)
    {
      CloseHandle(printHandle);
      printHandle = CreateFile (mszPrinterName.c_str(),
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL
        );
    }

#else

    printHandle =  wxOpen (mszPrinterName.c_str(), O_WRONLY | O_NOCTTY, 0);
		if((int)printHandle<0)
		{
			unlink(mszPrinterName.c_str());
      printHandle =  wxOpen (mszPrinterName.c_str(), O_RDWR | O_CREAT, 0xffff);      
    }

#endif

Code: Select all

// write
wxString szData("teststring to print\r\n");
  if((printHandle > 0) &&
       (printHandle != INVALID_HANDLE_VALUE))
    {

#if defined(__WXMSW__) || defined(__WIN95__)

      DWORD bytesWritten = 0;
      DWORD bytesCount = (DWORD) wxStrlen(szData.c_str());

      int dataLength=szData.size();
      WriteFile(printHandle, szData.c_str(), bytesCount, &bytesWritten,NULL);

      CloseHandle(printHandle);
      printHandle = INVALID_HANDLE_VALUE;

#else
      int bytesWritten = 0;
      DWORD bytesCount = (DWORD) wxStrlen(szData.c_str());

      int dataLength=szData.size();
      bytesWritten = wxWrite(printHandle, szData.c_str(), bytesCount);

      wxClose(printHandle);
      printHandle = INVALID_HANDLE_VALUE;

#endif            
    }

I assume for Windows the printer name will be something like "LPT1:" or "LPT2:", for Linux I think it's "/dev/lp0".
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

clyde729
Super wx Problem Solver
Super wx Problem Solver
Posts: 426
Joined: Mon May 29, 2006 10:50 pm
Location: Jena, Germany

Post by clyde729 » Fri Feb 29, 2008 7:08 am

In addition: AFAIK this won't work for network printers. If you want to use this, you will need to do it in a third way. You can also invoke the printer by native C-API system commands.

Code: Select all

system("print "+ printername+"\r"); // <-- won't work out of the box
system("lpr -l"+printername+" & \r"); // the same ;-)
OS: Windows XP Home, Compiler: MingW, Version: wxWidgets 2.8.0, IDE: wx-Devcpp

FireMail
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Jun 10, 2005 8:34 am
Location: Austria
Contact:

Post by FireMail » Fri Feb 29, 2008 9:18 am

thanks, thats a nice piece of code i will use.
the printer names arent LTP1 and so on, those are the normal windows printer names, but it should work. kind regards
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GB/CS/CM/IT !d++ s+:-- a-- C++++$ UBL*++++$ P--- L++++$ !E-- !W+++$? !N-- !o K--? w++()$ !O M$ !V !PS? !PE? !Y? !PGP !t !5 !X R+++ tv++ !b? DI D++ G e+++ h++ r++ y+
------END GEEK CODE BLOCK------

Post Reply