Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Printer Emulator Connection Issue #637

Open
1 task done
rodhoff opened this issue Apr 18, 2024 · 6 comments
Open
1 task done

Printer Emulator Connection Issue #637

rodhoff opened this issue Apr 18, 2024 · 6 comments

Comments

@rodhoff
Copy link

rodhoff commented Apr 18, 2024

I have:

  • searched open and closed issues for duplicates

Bug description

As I don't have a thermal printer, I've opted in for using an emulator. I'm currently using VSPE to emulate a pair of COM ports (COM2 and COM5), and then running the emulator for Bematech MP-4200. It runs perfectly. I've used WinTFI2 RAT to send commands to the printer, and it worked fine.

Problem is, for some reason, when I try to send text to print from my python code, I receive errors saying I'm not allowed to use the port.
This kind of confused me, because the printer is connected as expected in the port COM2, but when I access it to send data it says I'm not allowed.

As for the code, I've tried this approach:

from escpos import SerialConnection
from escpos.impl.epson import GenericESCPOS

conn = SerialConnection.create('COM2:9600,8,1,N')
printer = GenericESCPOS(conn)
printer.init()
printer.text('----> Printer Test')

If I run it with the printer emulator off, I get no message. If the printer emulated is on, I receive the following error message:

serial.serialutil.SerialException: could not open port 'COM2': PermissionError(13, 'Access denied.', None, 5)

Could you please help to understand why the emulator can't receive the connection?

@belono
Copy link
Contributor

belono commented Apr 27, 2024

Hi @rodhoff !

Well, I think you are not using our python-escpos library but some other escpos library written in python.

Anyway, your issue looks like a permission problem.
Did you try to run your script as an administrator?

Sadly, I can't help you much more.

@rodhoff
Copy link
Author

rodhoff commented Apr 28, 2024

Hi @belono !

Sorry for the mistake, I'm trying several approaches to understand why it's not working. I have tried to run this sample code for the python-escpos even though:

from escpos.printer import Serial
p = Serial(devfile='COM2',
           baudrate=9600,
           bytesize=8,
           parity='N',
           stopbits=1,
           timeout=1.00,
           dsrdtr=True)

p.text("Hello World\n")
p.qr("You can readme from your smartphone")
p.cut()

And I got the same error message, even running from an elevated powershell:

(c:\Users\rodri\dev\pycoupon\v-fatura) C:\Users\rodri\dev\pycoupon [main +3 ~1 -0 !]> python serial-test-3.py
Traceback (most recent call last):
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\printer\serial.py", line 136, in open
    self.device: Optional[serial.Serial] = serial.Serial(
                                           ^^^^^^^^^^^^^^
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\serial\serialwin32.py", line 33, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\serial\serialutil.py", line 244, in __init__
    self.open()
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\serial\serialwin32.py", line 64, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM2': PermissionError(13, 'Acesso negado.', None, 5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\rodri\dev\pycoupon\serial-test-3.py", line 10, in <module>
    p.text("Hello World\n")
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\escpos.py", line 863, in text
    self.magic.write(str(txt))
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\magicencode.py", line 282, in write
    self.write_with_encoding(encoding, to_write)
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\magicencode.py", line 304, in write_with_encoding
    self.driver._raw(
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\printer\serial.py", line 163, in _raw
    assert self.device
           ^^^^^^^^^^^
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\escpos.py", line 146, in device
    self.open()
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\printer\serial.py", line 50, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\rodri\dev\pycoupon\v-fatura\Lib\site-packages\escpos\printer\serial.py", line 150, in open
    raise DeviceNotFoundError(
escpos.exceptions.DeviceNotFoundError: Device not found (Unable to open serial printer on COM2:
could not open port 'COM2': PermissionError(13, 'Access denied.', None, 5))

I'm using VSPE to emulate the COM2 - COM5 pair and the Bematech printer emulator.

@rodhoff
Copy link
Author

rodhoff commented Apr 29, 2024

As I'm using VSPE, and emulated a pair of com ports (COM2<=>COM5) I believe it will use COM2 to emulate the printer, and would listen for requests from COM5... When I changed COM2 to COM5, I don't get any error - but don't get anything printed as well...

@belono
Copy link
Contributor

belono commented Apr 29, 2024

It seems that Bematech doesn't support ESC/POS commands, so you can't print with this library on a Bematech printer.

Did you choose a Bematech emulator because you have to support a printer of that specific model?

EDIT:
Sorry, seems that I was wrong. --> See chapter 4

Some Bematech printers have dual command set support ESCBema/ESCPOS, so you could print with the help of python-escpos.

@rodhoff
Copy link
Author

rodhoff commented May 3, 2024

Thanks @belono !

No specific reason for using Bematech, it's only that I've found it first. Is there any other reasonable one which you know I could use?

@belono
Copy link
Contributor

belono commented May 4, 2024

Depending on your needs, there are some tools more or less related to our library that you can use to check your code is working.

  1. If you just want to check you are sending well-formatted commands to python-escpos you simply can make use of our Dummy printer connector and check the raw output it would be sent to your printer.
    Documentation of the Dummy printer connector

  2. A more similar approach to a virtual printer where you can use a real connection to a 'virtual network device' and see some simple output is the escpos-printer-simulator that is quite straightforward to use. Just download the binary and run it in java, it starts a server at a port that listens to escpos commands and renders to a window the raw output you send using our Network printer connector.

  3. If you would like to see some final output with limited support to images and some complex layout then esc2html would be your choice. It could render an html representation of the Dummy.output(). To tell the truth I've never used this tool but, if I'm not wrong, just save the output to a binary file and open it up with esc2html to get an html file that you can then open up in a web browser.

Surely, there are more and maybe better tools to simulate an escpos printer but these are the ones I know.
More suggestions are welcome from my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants