bitrate16 / peripage-python

Python module and documentation for direct printing on Peripage thermal printers via bluetooth
GNU General Public License v3.0
77 stars 10 forks source link

Problem on Raspberry #3

Open KataPi opened 3 years ago

KataPi commented 3 years ago

Hello, I am using @eliasweingaertner 's script on raspberry. I'm not a programmer... I am running the script through a nodejs server which creates a png and sends it to print. Both the old script (without svg support) and the new one (with svg support) works well.

I would like to integrate the script directly into my nodejs ( as issue ) but, for now, I just call it.

I installed your package with dependencies, on raspberry it can be installed only via pip3 command. (Maybe an error in info hcitools + scan)

It doesn't work for me. I also tried pairing the printer from the desktop.

Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/.local/bin/ppa6", line 10, in <module>
    sys.exit(main())
  File "/home/pi/.local/lib/python3.7/site-packages/ppa6/__main__.py", line 39, in main
    printer.connect()
  File "/home/pi/.local/lib/python3.7/site-packages/ppa6/__init__.py", line 94, in connect
    self.sock.connect((self.mac, 1))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: timed out

what am I doing wrong? Thank you Andrew

bitrate16 commented 3 years ago

Can you please tell me which version of raspberry pi do you have? Later, i will perform some tests on RPi4 and RPi Zero and suggest a fix.

KataPi commented 3 years ago

Rpi 3

miaiam commented 3 years ago

I'm having the same error on my raspberry pi 3. It works without any errors when I remove the line "self.sock.settimeout(self.timeout)" in the init.py file in the connect function (line 93). But I don't understand what the issue is and why it works when the line is removed

bitrate16 commented 3 years ago

It works without any errors when I remove the line "self.sock.settimeout(self.timeout)" in the init.py file in the connect function (line 93). But I don't understand what the issue is and why it works when the line is removed

This issue may get fixed by commenting out the line of code because of the bluetooth service/device manually configures timeout and may not support settimeout. But anyway, i need to check it.

You can change the timeout by specifying it in the constructor like in this python sample:

import ppa6
printer = ppa6.Printer('MAC', ppa6.PrinterType. your printer type A6 or A6p, timeout=10.0)
printer.connect()
printer.printASCII('Hello')
printer.disconnect()

And check if it still fails to connect. This sample will set timeout to 10 seconds and attempt to connect and print a text. If the problem persists, i'll check some docs about PyBluez on RPi3

KataPi commented 3 years ago
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ppa6
>>> printer = ppa6.Printer('xxxx', ppa6.PrinterType.A6, timeout=10.0)
>>> printer.connect()
Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/ppa6-python/ppa6/__init__.py", line 94, in connect
    self.sock.connect((self.mac, 1))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: timed out
>>> 

I also tried to edit the file via nano: error ...

KataPi commented 3 years ago

from this:

sudo nano /etc/bluetooth/main.conf

Un-comment both lines:

discoverabletimeout=0

and

pairabletimeout=0

same error...

bitrate16 commented 3 years ago

Currently i'm trying to reproduce the bug on RPi Zero and RPi 4. RPi 4 seems to randomly fail in bluetooth connections. Try to run the following code and check if it connects.

import socket
import bluetooth

sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect(('MAC', 1))
sock.settimeout(10.0)
sock.close()

In addition you can restart the bluetooth adapter and service with sudo hciconfig hci0 reset sudo systemctl restart bluetooth (hci0 in case of RPi4. Get the name of your adapter with hciconfig)

Also you can turn off/on the printer and make another attempt of connection. Printer sometimes hangs itself and stops responding to connections.

If the connection will work, i will post a commit with .connect and .settimeout lines swapped because this fixed connection failture on RPI Zero.

KataPi commented 3 years ago

It works for me. in the same shell i tried to load your script too and i got a new error ... maybe that's normal. I don't know if it will be useful ....

Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> import bluetooth
>>> sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
>>> sock.connect(('xxxx', 1))
>>> sock.settimeout(10.0)
>>> sock.close()
>>> 
>>> import ppa6
>>> printer = ppa6.Printer('xxxx', ppa6.PrinterType.A6, timeout=10.0)
>>> printer.connect()
Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.error: (77, 'File descriptor in bad state')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.7/site-packages/ppa6/__init__.py", line 94, in connect
    self.sock.connect((self.mac, 1))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: [Errno 77] File descriptor in bad state
>>>

If I first set .settimeout and then connect I get an error

Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> import bluetooth
>>> sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
>>> sock.settimeout(10.0)
>>> sock.connect(('xxxx', 1))
Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.error: (77, 'File descriptor in bad state')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: [Errno 77] File descriptor in bad state
>>> 
bitrate16 commented 3 years ago

in the same shell i tried to load your script too and i got a new error ... maybe that's normal.

It's normal because fixed code is not committed yet. As i see, the problem is caused by calling .settimeout before .connect which puts socket in the invalid state, so i will push a test fix

hvfrancesco commented 3 years ago

I'm experiencing the same issue on my Ubuntu 20.04 laptop, waiting to test the fix

bitrate16 commented 3 years ago

@KataPi @hvfrancesco Pushed the test fix Try to install it with

git clone https://github.com/bitrate16/ppa6-python
cd ppa6-python
pip install . --user

And check if issue persists

import ppa6
printer = ppa6.Printer('MAC', ppa6.PrinterType. your printer type A6 or A6p, timeout=10.0)
printer.connect()
printer.printASCII('Hello')
printer.disconnect()
KataPi commented 3 years ago

Hi Bitrate16, Now it works for me. It print but poor quality... Jerky print

This is Elias script IMG_20210413_181207

This is yours IMG_20210413_181220

This is the original 0

bitrate16 commented 3 years ago

@KataPi You can try to change the print concentration using -c 1

python -m ppa6 -m <MAC> -c 1 -p <A6p or A6> -i image.png

And you will get almost the same print result. In other case i can later add tweakable brightness and contrast

hvfrancesco commented 3 years ago

the fix works perfectly, thanks (default timeout is a bit short and sometimes does raise an issue, but as you can change it at instantiation or afterwards is perfectly ok). great work!

KataPi commented 3 years ago

Hello, Sorry for the delay. I tried the -c1 command but the quality is still low. The noise is also not smooth but feels jerky. Does it only happen to me?

IMG_20210415_132758

bitrate16 commented 3 years ago

@KataPi i'll look over this issue, but later