opening /dev/lp0 for output

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 ;{)
holyfeldAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karl Heinz KremerCommented:
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
holyfeldAuthor Commented:
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
Karl Heinz KremerCommented:
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
holyfeldAuthor Commented:
"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
Karl Heinz KremerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.