Coder Social home page Coder Social logo

shantanubhadoria / perl-printer-escpos Goto Github PK

View Code? Open in Web Editor NEW
6.0 5.0 3.0 969 KB

Module for universal support of all Dot Matrix, Thermal POS printers supporting ESCPOS Specification

Home Page: https://metacpan.org/pod/Printer::ESCPOS

License: MIT License

Perl 100.00%
printer receipt-printer network-printer escpos escpos-printer usb-printers serial

perl-printer-escpos's Introduction

NAME

Printer::ESCPOS - Interface for all thermal, dot-matrix and other receipt printers that support ESC-POS specification.

Requires Perl 5.10+ Travis status CPAN Testers result Distribution kwalitee Gratipay

VERSION

version 1.006

SYNOPSIS

If you are just starting up with POS RECEIPT Printers, you must first refer to Printer::ESCPOS::Manual to get started.

Printer::ESCPOS provides four different types of printer connections to talk to a ESCPOS printer. As of v0.012 driverType Serial, Network, File and USB are all implemented in this module. USB driverType is not supported prior to v0.012.

USB Printer

USB driverType allows you to talk to your Printer using the vendorId and productId values for your printer. These can be retrieved using lsusb command

 shantanu@shantanu-G41M-ES2L:~/github$ lsusb
 . . .
 Bus 003 Device 002: ID 1504:0006
 . . .

The output gives us the vendorId 0x1504 and productId 0x0006

For USB Printers Printer::ESCPOS uses a default endPoint of 0x01 and a default timeout of 1000, however these can be specified manually in case your printer requires a different value.

    use Printer::ESCPOS;

    my $vendorId  = 0x1504;
    my $productId = 0x0006;
    my $device = Printer::ESCPOS->new(
        driverType     => 'USB',
        vendorId       => $vendorId,
        productId      => $productId,
    );

    use GD;
    my $img = newFromGif GD::Image('header.gif') || die "Error $!";
    $device->printer->image($img); # Takes a GD image object

    $device->printer->qr("Don't Panic!"); # Print a QR Code

    $device->printer->printAreaWidth(5000);
    $device->printer->text("Print Area Width Modified\n");
    $device->printer->printAreaWidth(); # Reset to default
    $device->printer->text("print area width reset\n");
    $device->printer->tab();
    $device->printer->underline(1);
    $device->printer->text("underline on\n");
    $device->printer->invert(1);
    $device->printer->text("Inverted Text\n");
    $device->printer->justification('right');
    $device->printer->text("Right Justified\n");
    $device->printer->upsideDown(1);
    $device->printer->text("Upside Down\n");
    $device->printer->cutPaper();

    $device->printer->print(); # Dispatch the above commands from module buffer to the Printer.

Network Printer

For Network Printers $port is 9100 in most cases but might differ depending on how you have configured your printer

    use Printer::ESCPOS;

    my $printer_id = '192.168.0.10';
    my $port       = '9100';
    my $device = Printer::ESCPOS->new(
        driverType => 'Network',
        deviceIp   => $printer_ip,
        devicePort => $port,
    );

    # These commands won't actually send anything to the printer but will store all the
    # merged data including control codes to module buffer.
    $device->printer->printAreaWidth(7000);
    $device->printer->text("Print Area Width Modified\n");
    $device->printer->printAreaWidth(); # Reset to default
    $device->printer->text("print area width reset\n");
    $device->printer->tab();
    $device->printer->underline(1);
    $device->printer->text("underline on\n");
    $device->printer->invert(1);
    $device->printer->text("Inverted Text\n");
    $device->printer->justification('right');
    $device->printer->text("Right Justified\n");
    $device->printer->upsideDown(1);
    $device->printer->text("Upside Down\n");
    $device->printer->cutPaper();

    $device->printer->print(); # Dispatch the above commands from module buffer to the Printer.
                               # This command takes care of read text buffers for the printer.

Serial Printer

Use the Serial driverType for local printer connected on serial port(or a printer connected via a physical USB port in USB to Serial mode), check syslog(Usually under /var/log/syslog) for what device file was created for your printer when you connect it to your system(For plug and play printers). You may also use a Windows port name like 'COM1', 'COM2' etc. as deviceFilePath param when running this under windows. The Device::SerialPort claims to support this syntax. (Drop me a email if you are able to make it work in windows as I have not tested it out yet)

    use Printer::ESCPOS;
    use Data::Dumper; # Just to get dumps of status functions supported for Serial driverType.

    my $path = '/dev/ttyACM0';
    $device = Printer::ESCPOS->new(
        driverType     => 'Serial',
        deviceFilePath => $path,
    );

    say Dumper $device->printer->printerStatus();
    say Dumper $device->printer->offlineStatus();
    say Dumper $device->printer->errorStatus();
    say Dumper $device->printer->paperSensorStatus();

    $device->printer->bold(1);
    $device->printer->text("Bold Text\n");
    $device->printer->bold(0);
    $device->printer->text("Bold Text Off\n");

    $device->printer->print();

File(Direct to Device File) Printer

A File driverType is similar to the Serial driverType in all functionality except that it doesn't support the status functions for the printer. i.e. you will not be able to use printerStatus, offlineStatus, errorStatus or paperSensorStatus functions

    use Printer::ESCPOS;

    my $path = '/dev/usb/lp0';
    $device = Printer::ESCPOS->new(
        driverType     => 'File',
        deviceFilePath => $path,
    );

    $device->printer->bold(1);
    $device->printer->text("Bold Text\n");
    $device->printer->bold(0);
    $device->printer->text("Bold Text Off\n");

    $device->printer->print();

DESCRIPTION

You can use this module for all your ESC-POS Printing needs. If some of your printer's functions are not included, you may extend this module by adding specialized funtions for your printer in it's own subclass. Refer to Printer::ESCPOS::Roles::Profile and Printer::ESCPOS::Profiles::Generic

ATTRIBUTES

driverType

"Required attribute". The driver type to use for your printer. This can be File, Network, USB or Serial. If you choose File or Serial driver, you must provide the deviceFilePath, for Network driverType you must provide the printerIp and printerPort, For USB driverType you must provide vendorId and productId.

USB driver type:

my $vendorId  = 0x1504;
my $productId = 0x0006;
my $device = Printer::ESCPOS->new(
    driverType => 'USB'
    vendorId   => $vendorId,
    productId  => $productId,
);

Network driver type:

my $printer_id = '192.168.0.10';
my $port       = '9100';
my $device = Printer::ESCPOS->new(
    driverType => 'Network',
    deviceIp   => $printer_ip,
    devicePort => $port,
);

Serial driver type:

my $path = '/dev/ttyACM0';
$device = Printer::ESCPOS->new(
    driverType     => 'Serial',
    deviceFilePath => $path,
);

File driver type:

my $path = '/dev/usb/lp0';
$device = Printer::ESCPOS->new(
    driverType     => 'File',
    deviceFilePath => $path,
);

profile

There are minor differences in ESC POS printers across different brands and models in terms of specifications and extra features. For using special features of a particular brand you may create a sub class in the name space Printer::ESCPOS::Profiles::* and load your profile here. I would recommend extending the Generic Profile( Printer::ESCPOS::Profiles::Generic ). Use the following classes as examples. Printer::ESCPOS::Profiles::Generic Printer::ESCPOS::Profiles::SinocanPSeries

Note that your driver class will have to implement the Printer::ESCPOS::Roles::Profile Interface. This is a Moo::Role and can be included in your class with the following line.

use Moo;
with 'Printer::ESCPOS::Roles::Profile';

By default the generic profile is loaded but if you have written your own Printer::ESCPOS::Profile::* class and want to override the generic class pass the profile Param during object creation.

my $device = Printer::ESCPOS->new(
    driverType => 'Network',
    deviceIp   => $printer_ip,
    devicePort => $port,
    profile    => 'USERCUSTOM'
);

The above $device object will use the Printer::ESCPOS::Profile::USERCUSTOM profile.

deviceFilePath

File path for UNIX device file. e.g. "/dev/ttyACM0", or port name for Win32 (untested) like 'COM1', COM2' etc. This is a mandatory parameter if you are using File or Serial driverType. I haven't had a chance to test this on windows so if you are able to successfully use this with a serial port on windows, drop me a email to let me know that I got it right :)

portName

Win32 serial port name

deviceIP

Contains the IP address of the device when its a network printer. The module creates IO:Socket::INET object to connect to the printer. This can be passed in the constructor.

devicePort

Contains the network port of the device when its a network printer. The module creates IO:Socket::INET object to connect to the printer. This can be passed in the constructor.

baudrate

When used as a local serial device you can set the baudrate of the printer too. Default (38400) will usually work, but not always.

serialOverUSB

Set this value to 1 if you are connecting your printer using the USB Cable but it shows up as a serial device and you are using the Serial driver.

vendorId

This is a required param for USB driverType. It contains the USB printer's Vendor ID when using USB driverType. Use lsusb command to get this value for your printer.

productId

This is a required param for USB driverType. It contains the USB printer's product Id when using USB driverType. Use lsusb command to get this value for your printer.

endPoint

This is a optional param for USB driverType. It contains the USB endPoint for Device::USB to write to if the value is not 0x01 for your printer. Get it using the following command:

shantanu@shantanu-G41M-ES2L:~$ sudo lsusb -vvv -d 1504:0006 | grep bEndpointAddress | grep OUT
        bEndpointAddress     0x01  EP 1 OUT

Replace 1504:0006 with your own printer's vendor id and product id in the above command.

timeout

Timeout for bulk write functions for the USB printer. Optional param.

printer

Use this attribute to send commands to the printer

$device->printer->setFont('a');
$device->printer->text("blah blah blah\n");

USAGE

Refer to the following manual to get started with Printer::ESCPOS

Quick usage summary in steps:

  1. Create a device object $device by providing parameters for one of the supported printer types. Call $device->printer->init to initialize the printer.

  2. call text() and other Text formatting functions on $device->printer for the data to be sent to the printer. Make sure to end it all with a linefeed $device->printer->lf().

  3. Then call the print() method to dispatch the sequences from the module buffer to the printer

    $device->printer->print()

Note: While you may call print() after every single command code, this is not advisable as some printers tend to choke up if you send them too many print commands in quick succession. To avoid this, aggregate the data to be sent to the printer with text() and other text formatting functions and then send it all in one go using print() at the very end.

NOTES

  • In Serial mode if the printer prints out garbled characters instead of proper text, try specifying the baudrate parameter when you create the printer object. The default baudrate is set at 38400

    $device = Printer::ESCPOS->new( driverType => 'Serial', deviceFilePath => $path, baudrate => 9600, );

  • For ESC-P codes refer the guide from Epson http://support.epson.ru/upload/library_file/14/esc-p.pdf

SEE ALSO

SUPPORT

Bugs / Feature Requests

Please report any bugs or feature requests through github at https://github.com/shantanubhadoria/perl-printer-escpos/issues. You will be notified automatically of any progress on your issue.

Source Code

This is open source software. The code repository is available for public review and contribution under the terms of the license.

https://github.com/shantanubhadoria/perl-printer-escpos

git clone git://github.com/shantanubhadoria/perl-printer-escpos.git

AUTHOR

Shantanu Bhadoria [email protected] https://www.shantanubhadoria.com

CONTRIBUTORS

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Shantanu Bhadoria.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

perl-printer-escpos's People

Contributors

ilya33 avatar sonntagd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

perl-printer-escpos's Issues

Moo over Moose

have you considered using the lighter weight Moo / Moo::Role / Type::Tiny over Moose / Moose::Role / TypeConstraints?

Cash Drawer not opening

We have a pos printer connected to a cash drawer, it works fine for a year until it prints abnormally (paper not feeding properly thus receipts are not printed before it cut).

We replace with a different brand of pos printer, printing of receipts are ok but it would not open the cash drawer.
I suspect that the control codes is different, I wanted to try to change the control code but I cannot find where to change from the ESCPOS source code.
Can you point me which source file to amend?

Thank you.

Error : Negative repeat count does nothing message when using USB or Serial connection method

We've been using Printer::ESCPOS module but we could not make it to work on USB or Serial connection.

Whenever we tried printing to a POS printer interface to a PC using USB or Serial cable, we always encounter "Negative repeat count does nothing at Serial.pm on line 93

@chunks = unpack "a$n" x ( ( length($buffer) / $n ) - 1 ) . "a*", $buffer;

sub print {
my ( $self, $raw ) = @_;
my @chunks;

my $buffer = $self->_buffer;
if ( defined $raw ) {
    $buffer = $raw;
}
else {
    $self->_buffer('');
}

my $n = 8;    # Size of each chunk in bytes
$n = 64 if ( $self->serialOverUSB );

@chunks = unpack "a$n" x ( ( length($buffer) / $n ) - 1 ) . "a*", $buffer;
for my $chunk (@chunks) {
    $self->_connection->write($chunk);
    if ( $self->serialOverUSB ) {
        $self->_connection->read();
    }
    else {
        usleep(10000)
          ; # Serial Port is annoying, it doesn't tell you when it is ready to get the next chunk
    }
}

}

and 102 for USB,pm.
USB.pm error will be on line 102 (for loop), code snippet below

sub print {
my ( $self, $raw ) = @_;
my @chunks;

my $buffer = $self->_buffer;
if ( defined $raw ) {
    $buffer = $raw;
}
else {
    $self->_buffer('');
}

my $n = 2**14;    # Size of each chunk in bytes
@chunks = unpack "a$n" x ( ( length($buffer) / $n ) - 1 ) . "a*", $buffer;
for my $chunk (@chunks) {
    $self->_connection->bulk_write( $self->endPoint, $chunk,
        $self->timeout );
    usleep(10000)
      ; # USB Port is sometimes annoying, it doesn't always tell you when it is ready to get the next chunk
}

}

How to fix the issue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.