How can I access ink levels of printers programmatically?

后端 未结 4 666
南笙
南笙 2021-02-02 04:44

Okay, this is a Windows specific question.

I need to be able to access the ink levels of a printer connected to a computer. Possibly direct connection, or a network conn

4条回答
  •  野的像风
    2021-02-02 04:56

    An alternative approach could be using ipp. While most of the printers I tried support both, I found one which only worked with ipp and one that only worked for me with snmp.

    Simple approach with ipptool:

    • Create file colors.ipp:
    {
        VERSION 2.0
        OPERATION Get-Printer-Attributes
    
        GROUP operation-attributes-tag
        ATTR charset "attributes-charset" "utf-8"
        ATTR naturalLanguage "attributes-natural-language" "en"
        ATTR uri "printer-uri" $uri
        ATTR name "requesting-user-name" "John Doe"
        ATTR keyword "requested-attributes" "marker-colors","marker-high-levels","marker-levels","marker-low-levels","marker-names","marker-types"
    }
    
    • Run:
    ipptool -v -t ipp://192.168.2.126/ipp/print colors.ipp
    
    • The response:
    "colors.ipp":
        Get-Printer-Attributes:
            attributes-charset (charset) = utf-8
            attributes-natural-language (naturalLanguage) = en
            printer-uri (uri) = ipp://192.168.2.126/ipp/print
            requesting-user-name (nameWithoutLanguage) = John Doe
            requested-attributes (1setOf keyword) = marker-colors,marker-high-levels,marker-levels,marker-low-levels,marker-names,marker-types
        colors                                                               [PASS]
            RECEIVED: 507 bytes in response
            status-code = successful-ok (successful-ok)
            attributes-charset (charset) = utf-8
            attributes-natural-language (naturalLanguage) = en-us
            marker-colors (1setOf nameWithoutLanguage) = #00FFFF,#FF00FF,#FFFF00,#000000,none
            marker-high-levels (1setOf integer) = 100,100,100,100,100
            marker-levels (1setOf integer) = 6,6,6,6,100
            marker-low-levels (1setOf integer) = 5,5,5,5,5
            marker-names (1setOf nameWithoutLanguage) = Cyan Toner,Magenta Toner,Yellow Toner,Black Toner,Waste Toner Box
            marker-types (1setOf keyword) = toner,toner,toner,toner,waste-toner
    

    marker-levels has current toner/ink levels, marker-high-levels are maximus (so far I've only seen 100s here), marker-names describe meaning of each field (tip: for colors you may want to strip everything after first space, many printers include cartridge types in this field).

    Note: the above is with cups 2.3.1. With 2.2.1 I had to specify the keywords as one string instead ("marker-colors,marker-h....). Or it can be left altogether, then all keywords are returned.

    More on available attributes (may differ between printers): https://www.cups.org/doc/spec-ipp.html
    More on executing ipp calls (including python examples): https://www.pwg.org/ipp/ippguide.html

提交回复
热议问题