Sending USB commands with Python

Discussion in 'Python' started by Adam W., Aug 29, 2012.

  1. Adam W.

    Adam W. Guest

    So I'm trying to get as low level as I can with my Dymo label printer, and this method described the PDF http://sites.dymo.com/Documents/LW450_Series_Technical_Reference.pdf seems to be it.

    I'm unfamiliar with dealing with the USB interface and would greatly appreciate it if someone could tell me how to send and receive these commands with Python. Perhaps if you were feeling generous and wanted to write a bit of sample code, sending the "Get Printer Status" command and receiving the response (page 17 of the PDF) would be perfect to get me on my way.

    Thanks,
    Adam
     
    Adam W., Aug 29, 2012
    #1
    1. Advertising

  2. On Tue, 28 Aug 2012 17:04:49 -0700 (PDT), "Adam W."
    <> declaimed the following in
    gmane.comp.python.general:

    > So I'm trying to get as low level as I can with my Dymo label printer, and this method described the PDF http://sites.dymo.com/Documents/LW450_Series_Technical_Reference.pdf seems to be it.
    >
    > I'm unfamiliar with dealing with the USB interface and would greatly appreciate it if someone could tell me how to send and receive these commands with Python. Perhaps if you were feeling generous and wanted to write a bit of sample code, sending the "Get Printer Status" command and receiving the response (page 17 of the PDF) would be perfect to get me on my way.
    >


    1) what OS?

    2) does the printer appear as a serial port by the OS? Or as a
    printer device?

    If it appears as a serial port, you may want to obtain the pyserial
    (or whatever the name is this year) package. For USB, you may need one
    of the variant USB packages (pywinusb, pyusb, etc.) With luck you won't
    need to use admin privileges to access the port as a raw device vs
    having to go through an OS driver...


    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Aug 29, 2012
    #2
    1. Advertising

  3. Adam W.

    hamilton Guest

    On 8/28/2012 8:54 PM, Dennis Lee Bieber wrote:
    > 2) does the printer appear as a serial port by the OS? Or as a
    > printer device?


    The OP posted the link to the manual.

    If your not going to at least look it over, .........


    USB Printer Interface

    The LabelWriter 450 series printers all communicate with the host
    computer using a full-speed USB 2.0 interface. This interface also
    operates with USB Version 1.1 or later. The printers implement the
    standard USB Printer Class Device interface for communications (see
    http://www.usb.org/developers/devclass/).

    hamilton

    PS: Page 14
     
    hamilton, Aug 29, 2012
    #3
  4. Adam W.

    alex23 Guest

    On Aug 29, 1:03 pm, hamilton <> wrote:
    > The OP posted the link to the manual.
    > If your not going to at least look it over, .........


    Speaking for myself, I _don't_ go out of my way to read extra material
    to help someone with a problem here. If it's worth mentioning, mention
    it in the question.
     
    alex23, Aug 29, 2012
    #4
  5. Adam W.

    hamilton Guest

    On 8/28/2012 11:04 PM, alex23 wrote:
    > On Aug 29, 1:03 pm, hamilton <> wrote:
    >> The OP posted the link to the manual.
    >> If your not going to at least look it over, .........

    >
    > Speaking for myself, I _don't_ go out of my way to read extra material


    But, you will give advice that has no value.


    Anything you post here from now on will be suspect.

    hamilton
     
    hamilton, Aug 29, 2012
    #5
  6. Adam W.

    Adam W. Guest

    On Wednesday, August 29, 2012 2:45:17 AM UTC-4, Tim Roberts wrote:
    > Which operating system are you using? If you are on Windows, then the
    >
    > operating system has already loaded a printer driver for this device.
    >
    >
    > The libusb or libusbx libraries can be used to talk to USB devices. There
    >
    > is a Python binding. On Windows, you still need to have a driver, but the
    >
    > libusbx instructions can help you find an install one.
    >


    I am on Windows and have installed a driver using libusb-win32. Using http://pyusb.sourceforge.net/docs/1.0/tutorial.html as a template, this is my code so far:

    import usb.core
    import usb.util

    dev = usb.core.find(idVendor=0x0922, idProduct=0x0021)

    # set the active configuration. With no arguments, the first
    # configuration will be the active one
    dev.set_configuration()

    # get an endpoint instance
    cfg = dev.get_active_configuration()
    interface_number = cfg[(0,0)].bInterfaceNumber
    alternate_settting = usb.control.get_interface(dev,interface_number)
    intf = usb.util.find_descriptor(
    cfg, bInterfaceNumber = interface_number,
    bAlternateSetting = 0
    )

    ep = usb.util.find_descriptor(
    intf,
    # match the first OUT endpoint
    custom_match = \
    lambda e: \
    usb.util.endpoint_direction(e.bEndpointAddress) == \
    usb.util.ENDPOINT_OUT
    )

    assert ep is not None


    I had to manually set bAlternateSetting to 0 for it to run and add dev to usb.control.get_interface(dev,interface_number).

    Trying to do the status thing mentioned before, in the interpreter I did:

    >>> ep.write('A')

    2

    And the manual says 2 is not a valid option... So something isn't adding up.
     
    Adam W., Aug 29, 2012
    #6
  7. On Wed, 29 Aug 2012 05:47:00 -0700 (PDT), "Adam W."
    <> declaimed the following in
    gmane.comp.python.general:

    >
    > Trying to do the status thing mentioned before, in the interpreter I did:
    >
    > >>> ep.write('A')

    > 2
    >


    Don't the commands require an <esc> character? "\x1BA" (or
    "\x1B\x41")

    OTOH, if the <esc> is issued behind the scenes,

    > And the manual says 2 is not a valid option... So something isn't adding up.


    ... and you do not need to issue some sort of read(), page 17 of the
    printer manual you linked would translate to

    not ready, top of form, not out of paper, not jammed, not in error

    Granted, page 10 implies that the printer will never show "not
    ready"

    However -- reading the pyUSB source code

    -=-=-=-=-
    def write(self, data, timeout = None):
    r"""Write data to the endpoint.

    The parameter data contains the data to be sent to the endpoint
    and
    timeout is the time limit of the operation. The transfer type
    and
    endpoint address are automatically inferred.

    The method returns the number of bytes written.

    For details, see the Device.write() method.
    """
    return self.device.write(self.bEndpointAddress, data,
    self.interface, timeout)
    -=-=-=-=-

    indicates that the "2" you are seeing is the "number of bytes written";
    you need to issue a read request to retrieve the returned printer
    status.
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Aug 29, 2012
    #7
  8. Adam W.

    Adam W. Guest

    On Wednesday, August 29, 2012 4:09:49 PM UTC-4, Dennis Lee Bieber wrote:
    >
    > Don't the commands require an <esc> character? "\x1BA" (or
    > "\x1B\x41")
    >
    > OTOH, if the <esc> is issued behind the scenes,


    I'm not sure which esc char it is asking for, I don't think libusb is providing its own, and it seems like the one you suggested isn't what it wants either..

    > ... and you do not need to issue some sort of read()
    > the "2" you are seeing is the "number of bytes written";
    >
    > you need to issue a read request to retrieve the returned printer
    >
    > status.
    >


    You are correct about the 2 being the number of bytes written. However when I issue a read command I get:

    >>> ep.write('\x1BA')

    4
    >>> ep.read(1)

    Traceback (most recent call last):
    File "<pyshell#75>", line 1, in <module>
    ep.read(1)
    File "C:\Python32\lib\site-packages\usb\core.py", line 301, in read
    return self.device.read(self.bEndpointAddress, size, self.interface, timeout)
    File "C:\Python32\lib\site-packages\usb\core.py", line 654, in read
    self.__get_timeout(timeout)
    File "C:\Python32\lib\site-packages\usb\backend\libusb01.py", line 483, in bulk_read
    timeout)
    File "C:\Python32\lib\site-packages\usb\backend\libusb01.py", line 568, in __read
    timeout
    File "C:\Python32\lib\site-packages\usb\backend\libusb01.py", line 384, in _check
    raise USBError(errmsg, ret)
    usb.core.USBError: [Errno None] b'libusb0-dll:err [_usb_setup_async] invalid endpoint 0x02\n'

    Avoiding the read command all together I should be able to write "<esc> E" and have it feed some paper, which it is not doing, so obviously there is more to uncover. That said I feel this endeavor has evolved and is no longer pertinent to the Python group so I will let you guys off the hook on this(although responses/suggestions are still welcome).

    Thanks for all your help!
     
    Adam W., Aug 29, 2012
    #8
  9. On Wed, 29 Aug 2012 14:21:30 -0700 (PDT), "Adam W."
    <> declaimed the following in
    gmane.comp.python.general:

    > You are correct about the 2 being the number of bytes written. However when I issue a read command I get:
    >
    > >>> ep.write('\x1BA')

    > 4


    That's interesting -- as if each byte you send is expanding into a
    pair of bytes.

    > >>> ep.read(1)

    > Traceback (most recent call last):
    > File "<pyshell#75>", line 1, in <module>
    > ep.read(1)
    > File "C:\Python32\lib\site-packages\usb\core.py", line 301, in read
    > return self.device.read(self.bEndpointAddress, size, self.interface, timeout)
    > File "C:\Python32\lib\site-packages\usb\core.py", line 654, in read
    > self.__get_timeout(timeout)
    > File "C:\Python32\lib\site-packages\usb\backend\libusb01.py", line 483, in bulk_read
    > timeout)
    > File "C:\Python32\lib\site-packages\usb\backend\libusb01.py", line 568, in __read
    > timeout
    > File "C:\Python32\lib\site-packages\usb\backend\libusb01.py", line 384, in _check
    > raise USBError(errmsg, ret)
    > usb.core.USBError: [Errno None] b'libusb0-dll:err [_usb_setup_async] invalid endpoint 0x02\n'
    >
    > Avoiding the read command all together I should be able to write "<esc> E" and have it feed some paper, which it is not doing, so obviously there is more to uncover. That said I feel this endeavor has evolved and is no longer pertinent to the Python group so I will let you guys off the hook on this (although responses/suggestions are still welcome).
    >


    Your original code shows you matching to an "OUT" endpoint... Could
    you need an "IN" to read the return values?

    Unfortunately it will be a few weeks before Amazon ships "USB
    Complete" so most of the setup hassle is currently just noise to me.
    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
     
    Dennis Lee Bieber, Aug 29, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cameron Simpson

    Re: Sending USB commands with Python

    Cameron Simpson, Aug 29, 2012, in forum: Python
    Replies:
    0
    Views:
    206
    Cameron Simpson
    Aug 29, 2012
  2. Dennis Lee Bieber

    Re: Sending USB commands with Python

    Dennis Lee Bieber, Aug 29, 2012, in forum: Python
    Replies:
    8
    Views:
    385
    Cameron Simpson
    Aug 30, 2012
  3. Cameron Simpson

    Re: Sending USB commands with Python

    Cameron Simpson, Aug 30, 2012, in forum: Python
    Replies:
    0
    Views:
    193
    Cameron Simpson
    Aug 30, 2012
  4. Replies:
    8
    Views:
    226
  5. Setia Budi

    Send commands to USB device in Python

    Setia Budi, Feb 11, 2014, in forum: Python
    Replies:
    1
    Views:
    101
Loading...

Share This Page