8

I was able to print text but when it comes to barcode it not showing or just showing irregular text.

Here is my source code

//barcode 128
                byte[] formats  = {(byte) 0x1d, (byte) 0x6b, (byte) 0x73,(byte) 0x0d};
                byte[] contents = content.getBytes();

                byte[] bytes    = new byte[formats.length + contents.length];

                System.arraycopy(formats, 0, bytes, 0, formats.length );
                System.arraycopy(contents, 0, bytes, formats.length, contents.length);


                usbCtrl.sendByte(bytes, dev);

                usbCtrl.sendByte(LineFeed(), dev);

but the result barcode is not showing, am i missing something

Please help me

EDIT

I found the ESC/POS code :

GS k m d1...dk NUL or GS k m n d1...d k

But when I try this, still got same result

Mochamad Taufik Hidayat
  • 1,264
  • 3
  • 21
  • 32

1 Answers1

6

The GS k POS code has two versions (as you already discovered):

GS k    - print one dimensional barcode  
   m    - barcode mode selector  
   [d]k - data bytes
   NUL  - terminator

This version works only for pure ASCII data since it uses a 0x00 (NUL) as terminator.

GS k    - print one dimensional barcode  
   m    - barcode mode selector  
   n    - content length in bytes
   [d]k - data bytes

This version uses an additional length byte n to indicate the data part (it's also only suitable for certain codings including CODE128).

Your code has a stray 0x0d in the command bytes and may also be using the wrong format.

If you plan to print pure ASCII data format the command like this:

byte[] formats  = {(byte) 0x1d, (byte) 0x6b, (byte) 0x49};
byte[] contents = content.getBytes();

byte[] bytes    = new byte[formats.length + contents.length + 1];

System.arraycopy(formats, 0, bytes, 0, formats.length );
System.arraycopy(contents, 0, bytes, formats.length, contents.length);

// add a terminating NULL
bytes[formats.length + contents.length] = (byte) 0x00;

Or the more secure version since it also has the expected data length:

byte[] contents = content.getBytes();
// include the content length after the mode selector (0x49)
byte[] formats  = {(byte) 0x1d, (byte) 0x6b, (byte) 0x49, (byte)content.length};

byte[] bytes    = new byte[formats.length + contents.length];

System.arraycopy(formats, 0, bytes, 0, formats.length );
System.arraycopy(contents, 0, bytes, formats.length, contents.length);

If neither of the two work then your printer may simply not support CODE128.

The 5890 is a common enough specification and there are lots of cheap "drop-in" replacements on the market which leave out the more complex barcode implementations and only include simple codings like EAN8, EAN13 etc.

Shirkrin
  • 3,993
  • 1
  • 29
  • 35
  • ok tomorrow I will try your code hope it's working, thanks anyway – Mochamad Taufik Hidayat Feb 08 '16 at 02:36
  • I'm back at work in a few hours - I'll double check with our own CODE128 functions :) – Shirkrin Feb 08 '16 at 05:30
  • 1
    OK, minor edit: it's hex 0x49 (decimal 73) for CODE128 printout. – Shirkrin Feb 08 '16 at 08:08
  • 1
    So here the result :You're code is working fine on printer type ZJ-5802DD Bluetooth previously not working too but now it can, so I think my USB printer is not support to print barcode, anyway very big thanks for help me a lot and clear explanation, I'm so glad when your code working, maybe I must change my printer USB :D – Mochamad Taufik Hidayat Feb 09 '16 at 02:57