Sending USB commands with Python

A

Adam W.

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
 
D

Dennis Lee Bieber

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...
 
H

hamilton

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
 
A

alex23

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.
 
H

hamilton

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
 
A

Adam W.

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:
2

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

Dennis Lee Bieber

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

2

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

OTOH said:
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.
 
A

Adam W.

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:
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!
 
D

Dennis Lee Bieber

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

4

That's interesting -- as if each byte you send is expanding into a
pair of bytes.
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top