Solved

opening /dev/lp0 for output

Posted on 2004-10-29
685 Views
Last Modified: 2013-12-15
I'm creating a QT application running on RH9.  I want to send a message to the printer when certain events happen. This is the code. strMsg is a QString.

  // now print the message to the printer
  int fid = open("/dev/lp0", O_WRONLY);
  if (fid < 0)
    {
    QMessageBox::warning(this, "We Got A Problem Boss",
                        "Can't write alarms to the printer!\n");
    }
  else
    {
    // must be open if we got this far.  write the alarm message
    strMsg.append("\n");
    int intCharsOut = write(fid,strMsg, strMsg.length());
    qDebug("Wrote %d chars",intCharsOut);
    close( fid );
    qDebug("Closed the file");
    }

This code works fine ... one time.  When the code tries to print the second message, it fails to open /dev/lp0.

What changes need to be made so I can print multiple messages?

don ;{)
0
Question by:holyfeld
    5 Comments
     
    LVL 44

    Expert Comment

    by:Karl Heinz Kremer
    You may want to open the device file only once when your program starts, then write the messages to the printer, and close the file again when your program is done.
    0
     

    Author Comment

    by:holyfeld
    This works, sort of.  It does solve the "failure to open /dev/lp0" problem.  However, it also prevents lp0 from receiving print jobs from the print manager.  

    ls -l lp0 returns
    crw-rw-rw-    1 root     lp         6,   0 Jan 30  2003 lp0

    do I need to set some other access/privilege?

    don ;{)
    0
     
    LVL 44

    Expert Comment

    by:Karl Heinz Kremer
    Because you are always in conflict with a print spooler when you write directly to the printer device, it's a bad idea to do this in the first place. What if you write a line, then give up the device, the printer spooler sends a job, then you open the device and print another line? Do you want your logs intermixed with normal print jobs?

    I don't know how the printer port is actually controlled (haven't done this for probably ten years), but I can lookup some information for you.
    0
     

    Author Comment

    by:holyfeld
    "Because you are always in conflict with a print spooler when you write directly to the printer device, it's a bad idea to do this in the first place."

    I agree.  The user will be back tomorrow.  I'll share the "news" with him and see what he says.

    Can you point me to th information on who the printer port is controlled. I'm replacing a DOS application that does the open/write/close sequence with no problem.  They're using ?DesqView? and occasionally go to another window to print reports.

    don ;{)
    0
     
    LVL 44

    Accepted Solution

    by:
    There is a man page that describes the ioctl interface for the lp device (online version can be found e.g. here: http://jamesthornton.com/linux/man/lp.4.html).
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now