namespace Inspectron.Epson; /// /// ESC/POS command constants for Epson TM-m30III printer /// public static class EpsonCommands { /// /// ESC @ - Initialize printer /// public static readonly byte[] Initialize = { 0x1B, 0x40 }; /// /// DLE EOT 1 - Transmit printer status /// public static readonly byte[] GetPrinterStatus = { 0x10, 0x04, 0x01 }; /// /// DLE EOT 2 - Transmit offline status /// public static readonly byte[] GetOfflineStatus = { 0x10, 0x04, 0x02 }; /// /// DLE EOT 3 - Transmit error status /// public static readonly byte[] GetErrorStatus = { 0x10, 0x04, 0x03 }; /// /// DLE EOT 4 - Transmit paper roll sensor status /// public static readonly byte[] GetPaperSensorStatus = { 0x10, 0x04, 0x04 }; /// /// GS I 1 - Get printer model ID /// public static readonly byte[] GetPrinterId = { 0x1D, 0x49, 0x01 }; /// /// GS V m - Cut paper (full cut) /// public static readonly byte[] CutPaperFull = { 0x1D, 0x56, 0x00 }; public static readonly byte[] PrintAndFeed = {0x0A }; /// /// GS V m - Cut paper (partial cut) /// public static readonly byte[] CutPaperPartial = { 0x1D, 0x56, 0x01 }; public static readonly byte[] UTF8Encoding = { 0x1B, 0x74, 0xFF }; public static readonly byte[] CP852Encoding = { 0x1B, 0x74, 0x12 }; public static readonly byte[] SelectFontHeader = { 0x1B, 0x4D }; public static readonly byte[] PrintLoadedImage = { 0x1D, 0x28, 0x4C, 0x02, 0x00, 0x30, 0x02 }; public enum PrinterFont { A=0, B= 1, C=2, D= 3, E = 4 } /// /// ESC d n - Print and feed n lines /// /// Number of lines to feed (0-255) /// Command bytes public static byte[] FeedLines(byte lines) { return new byte[] { 0x1B, 0x64, lines }; } /// /// LF - Line feed /// public static readonly byte[] LineFeed = { 0x0A }; /// /// Bit image mode for ESC * command /// public enum BitImageMode : byte { /// 8-dot single-density (90 DPI horizontal) SingleDensity8Dot = 0, /// 8-dot double-density (180 DPI horizontal) DoubleDensity8Dot = 1, /// 24-dot single-density (90 DPI horizontal) SingleDensity24Dot = 32, /// 24-dot double-density (180 DPI horizontal) DoubleDensity24Dot = 33 } /// /// Create ESC * command for bit-image mode printing /// /// Bit image mode (8-dot or 24-dot, single or double density) /// Column format bit image data /// Width in dots (1-2400 depending on printer model) /// Complete ESC * command sequence public static byte[] CreateBitImageCommand(BitImageMode mode, byte[] imageData, int widthDots) { // ESC * m nL nH d1...dk // Format: 0x1B 0x2A m nL nH [data] if (widthDots < 1 || widthDots > 2400) throw new ArgumentOutOfRangeException(nameof(widthDots), "Width must be between 1 and 2400 dots"); byte nL = (byte)(widthDots & 0xFF); byte nH = (byte)((widthDots >> 8) & 0xFF); var commands = new List { 0x1B, // ESC 0x2A, // * (byte)mode, // m (mode) nL, // nL (width low byte) nH // nH (width high byte) }; // Add image data commands.AddRange(imageData); return commands.ToArray(); } /// /// Create GS ( L raster image command sequence for printing images /// /// Monochrome raster data (1 bit per pixel, packed into bytes) /// Image width in pixels /// Image height in pixels /// Complete command sequence to store and print raster image public static byte[] CreateRasterImageCommand(byte[] imageData, int width, int height) { // Calculate dimensions int widthBytes = width; // Build the command sequence var commands = new List(); // Command 1: GS ( L - Store raster format graphics data (Function 112/0x70) // Header: GS ( L pL pH m fn a bx by c xL xH yL yH [data] int payloadSize = 10 + imageData.Length; // 10 bytes of parameters + image data byte pL = (byte)(payloadSize & 0xFF); byte pH = (byte)((payloadSize >> 8) & 0xFF); commands.Add(0x1D); // GS commands.Add(0x28); // ( commands.Add(0x4C); // L commands.Add(pL); // pL commands.Add(pH); // pH commands.Add(0x30); // m = 48 (function type) commands.Add(0x70); // fn = 112 (store raster format data) commands.Add(0x30); // a = 48 (normal) commands.Add(0x01); // bx = 1 (horizontal scaling) commands.Add(0x01); // by = 1 (vertical scaling) commands.Add(0x31); // c = 49 (monochrome, single color) // Width in bytes (xL + xH * 256) byte xL = (byte)(widthBytes & 0xFF); byte xH = (byte)((widthBytes >> 8) & 0xFF); commands.Add(xL); commands.Add(xH); // Height in pixels (yL + yH * 256) byte yL = (byte)(height & 0xFF); byte yH = (byte)((height >> 8) & 0xFF); commands.Add(yL); commands.Add(yH); // Add raster data commands.AddRange(imageData); // Command 2: GS ( L - Print graphics data (Function 50/0x32) commands.Add(0x1D); // GS commands.Add(0x28); // ( commands.Add(0x4C); // L commands.Add(0x02); // pL = 2 (2 bytes follow) commands.Add(0x00); // pH = 0 commands.Add(0x30); // m = 48 (function type) commands.Add(0x32); // fn = 50 (print graphics data) return commands.ToArray(); } }