Barcode printer source code

Technical assistance & information for projects
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Barcode printer source code

Post by Neo » Sat Nov 28, 2009 7:05 am

Programme your own POS system - Part 4

This is tested in TM-T88. Following example will print will print barcode "ROBOT.LK" with CODE 39. It is easy to change the barcode type to EAN, UPC, etc... by referring the ESC/POS manual.

Code: Select all

Private Const GS As Byte = &H1D

Private Function getBarcodeStr(bar_height As Byte, bar_width As Byte, bar_font As Byte, bar_pos As Byte, msg As String) As String

    printBarcode = Chr$(GS) & "h" & Chr$(bar_height) 'Bardcode Hieght
    printBarcode = printBarcode & Chr$(GS) & "w" & Chr$(bar_width) 'Barcide Width
    printBarcode = printBarcode & Chr$(GS) & "f" & Chr$(bar_font) 'Font for HRI characters
    printBarcode = printBarcode & Chr$(GS) & "H" & Chr$(bar_pos) 'Position of HRI characters
    printBarcode = printBarcode & Chr$(GS) & "k" & Chr$(69) & Chr$(20) 'Print Barcode Symbology 39
    printBarcode = printBarcode & msg & Chr$(0) & vbCrLf
    printBarcode = printBarcode & Chr$(GS) & "d" & Chr$(3) & vbCrLf
    printBarcode = printBarcode & Chr$(GS) & "@"

End Function


Sub printROBOT.LKORG()
    Str = getBarcodeStr(80, 1, 0, 2, "*ROBOT.LK*")
    ret = IODR.WriteData(Str, Len(Str))
End Sub
Please make sure that you post a reply to say thanks if it works okay for you. :)

See also:
ESC/POS printer source code at https://robot.lk/viewtopic.php?f=39&t=874
ESC/POS Customer Display source code at https://robot.lk/viewtopic.php?f=39&t=875
Point of Sale printer/drwer and Customer Display source code at https://robot.lk/viewtopic.php?f=39&t=876
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Barcode printer source code

Post by Neo » Sun Nov 29, 2009 12:34 am

This is a code written in Delphi that claims to work perfectly on Epson TMT-88 barcode printer.

To get the barcode chars

Code: Select all

{**
*    @param a ean13 barcode numeric value
*    @return the escpos code for the barcode print
*    Description uses escpos code, return code needed to print a ean13 barcode
*}
function TPrintEscPosToPort.getBarcodeEscPosCode(l_ean13:String):String;
    var
        l_return:String;
begin
    l_return :=  CHR(29) + 'k' + CHR(67) + CHR(12);
    l_return := l_return +  l_ean13; // Print bar code
    l_return := l_return +  l_ean13; // Print bar code number under thge barcode

    Result :=  l_return
end;
to print to a printer:

Code: Select all

{**
*    @param Printer Name, Item be printed, Cut the papers after the cut, #no of copies to print
*    @return boolen, true if it printed
*    Description prints a test page to the tysso printer
*}
function TPrintEscPosToPort.escPosPrint(const l_printer, l_textToPrint :String;l_cutPaper:Boolean=true;l_copies:integer=1): Boolean;
    var
        l_pPort,l_pName,l_tmp:String;
        i,x:integer;
        PrinterFile: TextFile;
begin
    // set result to false so any thing other then a good print will be false
    Result:= FALSE;

    try
        //Find if the printer exists, else set to defult -1
        i := Printer.Printers.IndexOf(l_printer);
        if (i > -1) then
        begin
            Printer.PrinterIndex := i;
            l_pName := Printer.Printers[i]; //Get the printer name (incase its the defult and not the one passed)
            l_pPort :=   Self.getPrinterPort(l_pName) ; // get the port name from the reg
        end;

        // If true add headers and footers to the passed text
        if (Self.aPrintHeadersFooters) then
        begin
            l_tmp := Self.getHeader()
                 +  l_textToPrint + Self.GetFooter();
        end
        else
        begin
            l_tmp := l_textToPrint;
        end;

      //Send the Document To the printer
      try
          for x:= 1 to l_copies do //Print multi-copies
          Begin              
              //Assign the file to a tmp file in the printer port
              if (length(trim(l_pPort)) > 0) then AssignFile(PrinterFile,l_pPort)
              else
              begin                         
                   //only use if we cant get the port 
                   //(may look bad as ctrl codes are still in place)
                   AssignPrn(PrinterFile);
                   l_tmp := Self.stripEscPos(l_tmp);
              end;

              Rewrite(PrinterFile);

              try
                  //Send the passed Text to the printer 
                  WriteLn(PrinterFile,l_tmp);

                  if (Self.aPrinterReset) then 
                       WriteLn(PrinterFile,escReset);  // Reset the printer alignment

                  if (l_cutPaper) then         
                       WriteLn(PrinterFile,escFeedAndCut); //Cut the paper if needed
              finally
                  CloseFile(PrinterFile);
                  Result:= true;
              end;
          end;
      except
      end;
    except
    end;
end;
Here is a lots of control code constants from the code above, hopefully the names are descriptive enough.

Code: Select all

const
     escNewLine   = chr(10);  // New line (LF line feed)
     escUnerlineOn   = chr(27) + chr(45) + chr(1);  // Unerline On
     escUnerlineOnx2 = chr(27) + chr(45) + chr(2);  // Unerline On x 2
     escUnerlineOff  = chr(27) + chr(45) + chr(0);  // Unerline Off
     escBoldOn       = chr(27) + chr(69) + chr(1);  // Bold On
     escBoldOff      = chr(27) + chr(69) + chr(0);  // Bold Off
     escNegativeOn   = chr(29) + chr(66) + chr(1);  // White On Black On'
     escNegativeOff  = chr(29) + chr(66) + chr(0);  // White On Black Off
     esc8CpiOn       = chr(29) + chr(33) + chr(16); // Font Size x2 On
     esc8CpiOff      = chr(29) + chr(33) + chr(0);  // Font Size x2 Off
     esc16Cpi        = chr(27) + chr(77) + chr(48); // Font A  -  Normal Font
     esc20Cpi        = chr(27) + chr(77) + chr(49); // Font B - Small Font
     escReset        = chr(27) + chr(64); //chr(27) + chr(77) + chr(48); // Reset Printer
     escFeedAndCut   = chr(29) + chr(86) + chr(65); // Partial Cut and feed

     escAlignLeft    = chr(27) + chr(97) + chr(48); // Align Text to the Left
     escAlignCenter  = chr(27) + chr(97) + chr(49); // Align Text to the Center
     escAlignRight   = chr(27) + chr(97) + chr(50); // Align Text to the Right
Post Reply

Return to “Project Assistance”