6

How can I check if cups printer is out of paper using commands only (no gui).

EDIT: Is it possible to check that using python?

EDIT2: I tried it with different printer (EPSON TM-m30). When I try to print without paper it stuck in Processing.

printer-state = processing
printer-state-message = Rendering completed
printer-state-reasons = none

But I still cant get info about paper.

I am not sure if it can help you, I created json file from some python code which reads printer attribues: https://pastebin.com/u97U9TnQ

Update

We bought another printer (Star Micronics TUP542), the printer has out of paper sensor: Near End Paper Sensor, but I can't find any manual how to work with this sensor (sensor is plugged into printer). Does cups have some command for it? Or do I need to use some different tool?

I tried to get attributes using python, here is the result (json): https://pastebin.com/Z3Tiz5g9

BTW, printer-state is always 3, with or without paper. Is it supposed to change when printer is out of paper? I think printer-state-reasons should say something about missing paper, but there is always ['none']

Update 2

Maybe I should try ESC/POS commands (here). I found some codes on page 157 Transmission of status.

n = 1, 49: Sends paper detector status

But I am not sure what does mean n = 1, 49 Is it string or decimal or hex values? Why there is comma between 1 and 49.

I tried this python code:

command = [0x1D, 0x72, 1]
dest="/dev/usb/lp1"

with open(dest,"wb") as file:
    file.write(bytearray(command))

with open(dest,"rb") as file:
    data = file.read() # hangs
    print(data)

But read function hangs and never returns.

PS: I also tried different values ([0x1D, 0x72, 49], [0x1D, 0x72, 1, 49], [0x1D, 0x72, 1, 0x49]), but without success.

By the way, when I use sudo cat /dev/usb/lp1 it prints lot of ###.

When I use hexdump after cat I see this repeating sequence: 23 06 00 00 00 00 00 00 00

0

2 Answers 2

1

Have you looked at lpstat?

For example, get a list of printers:

~$ lpstat -s

device for Canon_MX920_series: dnssd://Canon%20MX920%20series._ipp._tcp.local/?uuid=00000000-0000-1000-8000-xxxxxxxxxxxx

Then query the print in question:

~$ lpstat -p "Canon_MX920_series"

printer Canon_MX920_series is idle.  enabled since Tue 18 Jan 2022 10:49:40 AM CST
scheduler is running
5
  • But does that report on paper out? Commented Jan 20, 2022 at 12:46
  • @roaima It will report printer failures should one occur. I did not specifically check what happens when the printer is out of paper.
    – A. Que
    Commented Jan 20, 2022 at 14:07
  • What does the lpstat command report if you manually remove the paper from that printer? Would it be the same for all printers?
    – Kusalananda
    Commented Jan 20, 2022 at 15:28
  • when paper is out and trying to print something it writes Sending data to printer, but this is common message when I try to print with or without paper. But without paper message Sending data to printer stays forever.
    – T0maas
    Commented Jan 20, 2022 at 17:13
  • @T0maas It seems you are correct. Being out of paper does not count as a fault. My answer will not work.
    – A. Que
    Commented Jan 20, 2022 at 17:29
0

You can use the the subprocess module using Python to run the lpstat command and parse the output to determine the printer's status.

Here is an example of how you can do this:

import subprocess

def is_printer_out_of_paper(printer_name):
    # Run the lpstat command to get the printer's status
    output = subprocess.run(["lpstat", "-p", printer_name, "-l"], capture_output=True).stdout.decode()

    # Check if the output contains the "out of paper" message
    return "out of paper" in output

# Example usage
if is_printer_out_of_paper("My Printer"):
    print("Printer is out of paper")
else:
    print("Printer has paper")

In this example, My Printer is the name of the printer, and the lpstat command is used to get the printer's status. The -p option specifies the printer name, and the -l option causes the lpstat command to list the printer's status in long format. The output of the lpstat command is captured using the capture_output option and is then decoded to a string using the stdout.decode() method. The string is then searched for the "out of paper" message to determine if the printer is out of paper.

1
  • The given code will say that any non-existing printer has paper. The answer also needlessly uses Python to run a shell command. The reason (I suppose) that the user wanted to use Python is because they believe they need to do something more complicated than to just use lpstat, but the answer does not consider the absurdity of running lpstat via Python.
    – Kusalananda
    Commented Dec 19, 2022 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .