From: Marcin C. <mar...@gm...> - 2020-03-31 18:34:04
Attachments:
pEpkey.asc
|
I have an ESC printer. I made a simple script to send data to the device, but after changing the content of the script - 1x the previous version of the subtitles is printed, and then, when called again, the current one. As if the data from the USB was cached somewhere. How can I make some FLUSH? |test.py usb_= Connector(showUsbDevices=False) usb_.send(b'I LOVE YOU') | connector: |class Connector: def __init__(self, idVendor=0x0123, idProduct=0x1234, showUsbDevices=False): self.idVendor = idVendor self.idProduct = idProduct if showUsbDevices: pass self.device = self.FindAndConnect() if self.device is not None: #if self.device.is_kernel_driver_active(0): #self.device.detach_kernel_driver(0) #self.device.detach_kernel_driver(1) self.device.reset() self.device.set_configuration() self.cfg = self.device.get_active_configuration() data_itfs = list(usb.util.find_descriptor(self.cfg, find_all=True,custom_match=lambda e: (e.bInterfaceClass == 0xA))) intf = data_itfs[0] self.device.set_interface_altsetting(intf) itf_num = intf.bInterfaceNumber print ("inf descriptor:===============", intf) print("numer:===============",itf_num) self.messageOut = usb.util.find_descriptor(intf, custom_match=lambda e: not (e.bEndpointAddress & 0x80)) self.messageIn = usb.util.find_descriptor(intf, custom_match=lambda e: (e.bEndpointAddress & 0x80)) #print(">>>>>>>>>>>>>>>>>>>Message Out",self.messageOut) #print(">>>>>>>>>>>>>>>>>>>Message In",self.messageIn) #print(repr(self.cfg)) def __del__(self): if self.device is not None: usb.util.dispose_resources(self.device) def send(self, data): #print ("endpoint_out",self.messageOut) if self.device is not None: print(data.decode("IBM852")) self.messageOut.write(data) #self.device.write(1,data,100) #dane = self.messageIn.read(300) #print("IN|->",dane) def FindAndConnect(self): device=usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) if device is None: raise ValueError('Not found idVendor 0x%04x i idProduct 0x%04x' % (self.idVendor,self.idProduct)) print('Printer found idVendor 0x%04x i idProduct 0x%04x.... ' %(self.idVendor,self.idProduct)) return device | so when I run a test script that says I LOVE YOU I get ILOVE YOU when I change the inscription to I HATE YOU and run the script another copy is printed I LOVE YOU and only the next start-up gives: I HATE YOU What is it? Where's the bug? |
From: Kevin G. <ele...@my...> - 2021-01-31 12:58:34
|
Hello, I am re-using a subject line from the mailing list archive, by user Marcin, in an attempt to append to that thread. https://sourceforge.net/p/pyusb/mailman/pyusb-users/thread/15e9538f-386f-a637-ce2b-5eea3d04bd76%40gmail.com/ I had a simlar issue recently with a simple test application which opened a device and sent some OUT packets (to 512 size endpoint) thus: ..open device.. for i in range(n): dev.write(2,bytearray(os.urandom(512)) ..close device.. On windows, this worked fine for any 'n' but on Linux only even 'n' worked correctly. For odd 'n' one packet was always dropped/stuck. As Marcin suggested, ending with dev.reset() seemed to flush this stuck packet and fixed the issue. But using reset in this way seems wrong. After opening the device calling dev.clear_halt(2) Seemed to fix the issue. Regards Kevin |
From: Kevin G. <ele...@my...> - 2021-01-31 13:36:52
|
On 2021-01-31 12:58, Kevin Grant wrote: > Hello, > > I am re-using a subject line from the mailing list archive, by user > Marcin, in an attempt to append to that thread. > > https://sourceforge.net/p/pyusb/mailman/pyusb-users/thread/15e9538f-386f-a637-ce2b-5eea3d04bd76%40gmail.com/ > > I had a simlar issue recently with a simple test application which > opened a device and sent some OUT packets (to 512 size endpoint) thus: > > ..open device.. > for i in range(n): > dev.write(2,bytearray(os.urandom(512)) > ..close device.. > > On windows, this worked fine for any 'n' but on Linux only even 'n' > worked correctly. For odd 'n' one packet was always dropped/stuck. > As Marcin suggested, ending with dev.reset() seemed to flush this stuck > packet and fixed the issue. > But using reset in this way seems wrong. > After opening the device calling > dev.clear_halt(2) > Seemed to fix the issue. > > Regards > Kevin Ah, perhaps a better solution is to write a zero-length packet to signify end of data stream... ..open device.. for i in range(n): dev.write(2,bytearray(os.urandom(512)) dev.write(0x02, bytearray()) #ZLP end of data ..close device.. Regards Kevin |
From: Xiaofan C. <xia...@gm...> - 2021-02-01 09:31:48
|
On Sun, Jan 31, 2021 at 9:37 PM Kevin Grant <ele...@my...> wrote: > > On 2021-01-31 12:58, Kevin Grant wrote: > > > Hello, > > > > I am re-using a subject line from the mailing list archive, by user > > Marcin, in an attempt to append to that thread. > > > > https://sourceforge.net/p/pyusb/mailman/pyusb-users/thread/15e9538f-386f-a637-ce2b-5eea3d04bd76%40gmail.com/ > > > > I had a simlar issue recently with a simple test application which > > opened a device and sent some OUT packets (to 512 size endpoint) thus: > > > > ..open device.. > > for i in range(n): > > dev.write(2,bytearray(os.urandom(512)) > > ..close device.. > > > > On windows, this worked fine for any 'n' but on Linux only even 'n' > > worked correctly. For odd 'n' one packet was always dropped/stuck. > > As Marcin suggested, ending with dev.reset() seemed to flush this stuck > > packet and fixed the issue. > > But using reset in this way seems wrong. > > After opening the device calling > > dev.clear_halt(2) > > Seemed to fix the issue. > > > > Regards > > Kevin > > Ah, perhaps a better solution is to write a zero-length packet to > signify end of data stream... > > ..open device.. > for i in range(n): > dev.write(2,bytearray(os.urandom(512)) > > dev.write(0x02, bytearray()) #ZLP end of data > > ..close device.. > It may be that Linux and Windows may handle the things a bit differently. Still ZLP may help for bulk endpoint. https://www.beyondlogic.org/usbnutshell/usb4.shtml -- Xiaofan |
From: Tormod V. <lis...@gm...> - 2020-04-01 07:10:19
|
On Tue, Mar 31, 2020 at 8:34 PM Marcin Chuć wrote: > > I have an ESC printer. I made a simple script to send data to the device, but after changing the content of the script - 1x the previous version of the subtitles is printed, and then, when called again, the current one. As if the data from the USB was cached somewhere. How can I make some FLUSH? > > usb_= Connector(showUsbDevices=False) > usb_.send(b'I LOVE YOU') Hi Marcin, If you look at other code for communicating with such printers, you will see that there is a lot of printer control codes and protocol on top of the USB communication. With just sending these few text characters, you are probably causing a flush of the existing buffer and at the same time filling it up again with your new text. See for instance this hint: https://github.com/mike42/escpos-php/blob/development/src/Mike42/Escpos/Printer.php#L988 Regards, Tormod |
From: Marcin C. <mar...@gm...> - 2020-04-01 08:18:18
Attachments:
pEpkey.asc
|
Yes, i see. But it's in PHP. What about python 3.7? Native pyusb function? W dniu 01.04.2020 o 09:09, Tormod Volden pisze: > On Tue, Mar 31, 2020 at 8:34 PM Marcin Chuć wrote: >> I have an ESC printer. I made a simple script to send data to the device, but after changing the content of the script - 1x the previous version of the subtitles is printed, and then, when called again, the current one. As if the data from the USB was cached somewhere. How can I make some FLUSH? >> >> usb_= Connector(showUsbDevices=False) >> usb_.send(b'I LOVE YOU') > Hi Marcin, > > If you look at other code for communicating with such printers, you > will see that there is a lot of printer control codes and protocol on > top of the USB communication. With just sending these few text > characters, you are probably causing a flush of the existing buffer > and at the same time filling it up again with your new text. > > See for instance this hint: > https://github.com/mike42/escpos-php/blob/development/src/Mike42/Escpos/Printer.php#L988 > > Regards, > Tormod > > > _______________________________________________ > pyusb-users mailing list > pyu...@li... > https://lists.sourceforge.net/lists/listinfo/pyusb-users |
From: Marcin C. <mar...@gm...> - 2020-04-01 08:57:36
Attachments:
pEpkey.asc
|
I found it! def__del__(self): ifself.device isnotNone: self.device.reset() ############### <<<< THIS IS THE KEY>>>> (added to all code) usb.util.dispose_resources(self.device) W dniu 01.04.2020 o 09:09, Tormod Volden pisze: > On Tue, Mar 31, 2020 at 8:34 PM Marcin Chuć wrote: >> I have an ESC printer. I made a simple script to send data to the device, but after changing the content of the script - 1x the previous version of the subtitles is printed, and then, when called again, the current one. As if the data from the USB was cached somewhere. How can I make some FLUSH? >> >> usb_= Connector(showUsbDevices=False) >> usb_.send(b'I LOVE YOU') > Hi Marcin, > > If you look at other code for communicating with such printers, you > will see that there is a lot of printer control codes and protocol on > top of the USB communication. With just sending these few text > characters, you are probably causing a flush of the existing buffer > and at the same time filling it up again with your new text. > > See for instance this hint: > https://github.com/mike42/escpos-php/blob/development/src/Mike42/Escpos/Printer.php#L988 > > Regards, > Tormod > > > _______________________________________________ > pyusb-users mailing list > pyu...@li... > https://lists.sourceforge.net/lists/listinfo/pyusb-users |