Files
Print_server/Inspectron.Epson/EpsonCommands.cs
2026-01-13 09:06:47 +01:00

194 lines
6.3 KiB
C#

namespace Inspectron.Epson;
/// <summary>
/// ESC/POS command constants for Epson TM-m30III printer
/// </summary>
public static class EpsonCommands
{
/// <summary>
/// ESC @ - Initialize printer
/// </summary>
public static readonly byte[] Initialize = { 0x1B, 0x40 };
/// <summary>
/// DLE EOT 1 - Transmit printer status
/// </summary>
public static readonly byte[] GetPrinterStatus = { 0x10, 0x04, 0x01 };
/// <summary>
/// DLE EOT 2 - Transmit offline status
/// </summary>
public static readonly byte[] GetOfflineStatus = { 0x10, 0x04, 0x02 };
/// <summary>
/// DLE EOT 3 - Transmit error status
/// </summary>
public static readonly byte[] GetErrorStatus = { 0x10, 0x04, 0x03 };
/// <summary>
/// DLE EOT 4 - Transmit paper roll sensor status
/// </summary>
public static readonly byte[] GetPaperSensorStatus = { 0x10, 0x04, 0x04 };
/// <summary>
/// GS I 1 - Get printer model ID
/// </summary>
public static readonly byte[] GetPrinterId = { 0x1D, 0x49, 0x01 };
/// <summary>
/// GS V m - Cut paper (full cut)
/// </summary>
public static readonly byte[] CutPaperFull = { 0x1D, 0x56, 0x00 };
public static readonly byte[] PrintAndFeed = {0x0A };
/// <summary>
/// GS V m - Cut paper (partial cut)
/// </summary>
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
}
/// <summary>
/// ESC d n - Print and feed n lines
/// </summary>
/// <param name="lines">Number of lines to feed (0-255)</param>
/// <returns>Command bytes</returns>
public static byte[] FeedLines(byte lines)
{
return new byte[] { 0x1B, 0x64, lines };
}
/// <summary>
/// LF - Line feed
/// </summary>
public static readonly byte[] LineFeed = { 0x0A };
/// <summary>
/// Bit image mode for ESC * command
/// </summary>
public enum BitImageMode : byte
{
/// <summary>8-dot single-density (90 DPI horizontal)</summary>
SingleDensity8Dot = 0,
/// <summary>8-dot double-density (180 DPI horizontal)</summary>
DoubleDensity8Dot = 1,
/// <summary>24-dot single-density (90 DPI horizontal)</summary>
SingleDensity24Dot = 32,
/// <summary>24-dot double-density (180 DPI horizontal)</summary>
DoubleDensity24Dot = 33
}
/// <summary>
/// Create ESC * command for bit-image mode printing
/// </summary>
/// <param name="mode">Bit image mode (8-dot or 24-dot, single or double density)</param>
/// <param name="imageData">Column format bit image data</param>
/// <param name="widthDots">Width in dots (1-2400 depending on printer model)</param>
/// <returns>Complete ESC * command sequence</returns>
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<byte>
{
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();
}
/// <summary>
/// Create GS ( L raster image command sequence for printing images
/// </summary>
/// <param name="imageData">Monochrome raster data (1 bit per pixel, packed into bytes)</param>
/// <param name="width">Image width in pixels</param>
/// <param name="height">Image height in pixels</param>
/// <returns>Complete command sequence to store and print raster image</returns>
public static byte[] CreateRasterImageCommand(byte[] imageData, int width, int height)
{
// Calculate dimensions
int widthBytes = width;
// Build the command sequence
var commands = new List<byte>();
// 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();
}
}