Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


opening /dev/lp0 for output

Posted on 2004-10-29
Medium Priority
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");
    // must be open if we got this far.  write the alarm message
    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 ;{)
Question by:holyfeld
  • 3
  • 2
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 12449747
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.

Author Comment

ID: 12463622
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 ;{)
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 12463949
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.

Author Comment

ID: 12464924
"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 ;{)
LVL 44

Accepted Solution

Karl Heinz Kremer earned 1000 total points
ID: 12465268
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).

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

578 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