Public
Snippet $170 authored by Owo Sugiana

Barcode di Dotmatrix

barcode_dotmatrix.py
def cetak_barcode_128(data_angka):
    # Header ESC/P2 untuk Barcode
    ESC = b'\x1b'
    header = ESC + b'(B'
    
    # Parameter Konfigurasi:
    k = 8   # Code 128
    m = 2   # Lebar modul (2 atau 3 untuk Dot Matrix)
    s = 0   # Space adjustment
    v1 = 40 # Tinggi barcode (40/180 inch)
    v2 = 0  # High byte untuk tinggi
    c = 0   # Check digit otomatis oleh printer
    
    # Gabungkan parameter awal (k, m, s, v1, v2, c) = 6 bytes
    # Beberapa manual printer hanya butuh k, m, s, v1, v2 (5 bytes)
    # Kita gunakan standar 6 bytes untuk ESC/P2 modern
    param_list = [k, m, s, v1, v2, c]
    params = bytes(param_list)
    
    # Hitung nL dan nH (Total bytes setelah nH)
    total_bytes = len(params) + len(data_angka)
    nL = total_bytes % 256
    nH = total_bytes // 256
    
    # Susun perintah lengkap
    full_command = header + bytes([nL, nH]) + params + data_angka.encode('ascii')
    
    return full_command

# Contoh Penggunaan:
data = "1234567890123456789012" # 22 Digit
perintah_printer = cetak_barcode_128(data)

print(f"Total Byte dikirim: {len(perintah_printer)}")
# Kirim perintah_printer ke port /dev/usb/lp0 atau LPT1