-2

I need to print barcode to escpos printer and the command should to follow this rule: $1D $6B m d1...dk $00.

for example: if i wan to print CODE 39 the command is: write("\x1d\x6b\x04\x43\x4f\x44\x45\x20\x33\x39\x00")

my problem is:

  1. how to convert CODE 39 become \x43\x4f\x44\x45\x20\x33\x39 ?
  2. why this command not work write("\x1d\x6b\x04CODE 39\x00") ?

UPDATE

I use this RawHelperPrinter to print string to my escpos printer. This helper provide two method:

  1. SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  2. SendStringToPrinter(string szPrinterName, string szString)

Question

I can print hard coded barcode (CODE 39) with this command:

SendStringToPrinter("My ESCPOS Printer", "\x1d\x6b\x04\x43\x4f\x44\x45\x20\x33\x39\x00");

but how to print dynamic barcode ?

UPDATE 2

as @kunif mentioned, i need to send binary data to create barcode. here is my code to achieve it:

string data = "CODE 39";
byte[] buffer = Encoding.UTF8.GetBytes(data);
IntPtr ptr = Marshal.StringToCoTaskMemAnsi(buffer);
SendStringToPrinter("My ESCPOS Printer", "\x1d\x6b\x04");
SendBytesToPrinter("My ESCPOS Printer", ptr, buffer.Length);
SendStringToPrinter("My ESCPOS Printer", "\x00");

but it's still not working, any ide ?

Abu Dawud
  • 11
  • 5
  • It seems that you should define a byte array and write it without specifying it in the escape expression string. – kunif May 23 '22 at 00:44
  • @kunif Hi kunif, already try it using `Marshal.StringToCoTaskMemAnsi` to convert byte array to IntPtr and use `SendBytesToPrinter` to print the barcode. but it's still not working. *i'm already update my question* – Abu Dawud May 23 '22 at 01:36
  • Please refer to the ESC/POS command manual of the vendor or model of your printer or these EPSON documents to create the command. [GS k](https://reference.epson-biz.com/modules/ref_escpos/index.php?content_id=128) – kunif May 23 '22 at 04:28
  • @kunif when i want to print using ascii command can i do with this syntax `SendStringToPrinter("My ESCPOS Printer", "GS k CODE 39");` ? or it's wrong command ? – Abu Dawud May 23 '22 at 05:54
  • `GS` is a symbol for explanation, and you must actually specify binary data. You also need barcode type data and data indicating the end of the character string. – kunif May 23 '22 at 06:11
  • oh sorry, i know it. `GS k` and `\x1d\6b` is the same command. but i'm still confused why this command isn't work: `\x1d\x6b\x04CODE 39\x00` to print barcode of CODE 39 ? – Abu Dawud May 23 '22 at 06:13
  • Perhaps `\x04C` in `\x04CODE` was interpreted as `\x4C`. Why don't you try `\x1d\x6b\x04\x43ODE 39\x00`? – kunif May 23 '22 at 06:20
  • i need `CODE 39` to be dynamic. btw i'm already updated my question. thanks – Abu Dawud May 23 '22 at 06:25
  • Is it because one print request is divided into multiple methods? Why not request data like I commented with just one method? – kunif May 23 '22 at 06:42
  • I don't know what you are concerned about or what you don't understand, but the barcode string part can be any data within the range allowed by the printer's paper width and CODE39. If the characters that can be used in the hexadecimal strings 0-9 and A-F are concatenated in the escape string, they also need to be converted to the escape string so that the character delimiters do not shift. – kunif May 23 '22 at 11:49

1 Answers1

0

As I wrote in the comment, the escape string \x04CODE is 0x4c, 0x4f, 0x44, 0x45 and as a result \x04C seems to be evaluated as \x4C.
For example, you can check how it is converted by such a console program.

using System;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            byte[] buffer = Encoding.ASCII.GetBytes("\x1d\x6b\x04CODE 39\x00");
            Console.WriteLine(BitConverter.ToString(buffer));

            buffer = Encoding.ASCII.GetBytes("\x1d\x6b\x04\x43ODE 39\x00");
            Console.WriteLine(BitConverter.ToString(buffer));

            string data = "CODE 39";
            buffer = new byte[] { 0x1d, 0x6b, 0x04 };
            buffer = buffer.Concat(Encoding.ASCII.GetBytes(data)).ToArray();
            buffer = buffer.Concat(Encoding.ASCII.GetBytes("\x00")).ToArray();
            Console.WriteLine(BitConverter.ToString(buffer));

            Console.ReadLine();
        }
    }
}

Also, in the process written in UPDATE 2, if one ESC/POS print command is requested by dividing it into multiple methods, it seems may not be regarded as one continuous print command.
This is because even if the name is RawPrinterHelper, it is possible to add Prefix/Suffix data that performs preprocessing and cleanup before and after the data.

So you will need to create one ESC/POS print command as one byte array and request printing with one method.
Since it is a byte array print request, you will probably use SendBytesToPrinter().

By the way, in CODE39 barcodes, it is common to add a start/stop code * before and after the barcode data.
Since there is such an article, please refer to it when dealing with data.
Code 39 barcodes with no start/stop characters

kunif
  • 4,060
  • 2
  • 10
  • 30
  • yes you right. and finally i use this syntax to print barcode: `string barcode = String.Format("\x1d\x6b\x04{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}\x00", (char)buffer[0], (char)buffer[1], (char)buffer[2], (char)buffer[3], (char)buffer[4],(char)buffer[5], (char)buffer[6], (char)buffer[7],(char)buffer[8], (char)buffer[9]);` – Abu Dawud May 24 '22 at 04:23