ESCPOS打印控制命令

合集下载

打印机ESCPOS 控制指令

打印机ESCPOS 控制指令

HDD-80260打印机ESC/POS控制指令ESC/POS控制指令
HT横向跳格
FF打印并走纸
CAN页模式下删除打印缓冲区内容
DLEDC4nmt实时产生钱箱开启脉冲
ESCFF页模式下打印
ESCSPn设置字符右间距
ESC!n选择打印模式
ESC$nLnH设置绝对打印位置
ESC&yc1c2[x1d1...d(y×x1)]...[xkd1...d(y×xk)]定义用户自定义字符
ESC*mnLnHd1...dk选择位图模式
ESC?n取消用户自定义字符
ESCGn选择/取消双重打印模式
ESCL选择页模式
ESCMn选择字体
ESCRn选择国际字符集
ESCS选择标准模式
ESCTn在页模式下选择打印区域方向
ESCVn选择字符旋转打印
ESCc5n允许/禁止按键
ESCpmt1t2产生钱箱控制脉冲
FSpnm打印下载到FLASH中的位图
FSqn[xLxHyLyHd1...dk]1...[xLxHyLyHd1...dk]n定义Flash位图
GS!n选择字符大小
GS#n定义当前下载图号
GS*xyd1...d(x×y×8)定义下载位图
GS/m打印下载位图
GS:开始/结束宏定义
GSHn选择HRI字符的打印位置
GSWnLnH设置打印区域宽度
GS\nLnH页模式下设置纵向相对位置
GShn选择条码高度
①GSkmd1...dkNUL②GSkmnd1...dn打印条码
FS!n设置汉字字符模式
FS&选择汉字模式
FS.取消汉字模式
FSSn1n2设置汉字字符左右间距。

打印机ESCPOS 控制指令

打印机ESCPOS 控制指令

H D D-80260打印机E S C/P O S控制指令ESC/POS 控制指令HT 横向跳格LF 打印并走纸一行FF 打印并走纸CR 打印并回车CAN 页模式下删除打印缓冲区内容DLE DC4 n m t 实时产生钱箱开启脉冲ESC FF 页模式下打印ESC SP n 设置字符右间距ESC ! n 选择打印模式ESC $ nL nH 设置绝对打印位置ESC % n 选择/取消用户自定义字符ESC & y c1 c2 [x1 d1...d(y ×x1)]...[xk d1...d(y ×xk)] 定义用户自定义字符ESC * m nL nH d1...dk 选择位图模式ESC – n 选择/取消下划线模式ESC 2 设置默认行高ESC 3 n 设置行高ESC = n 选择打印机ESC ? n 取消用户自定义字符ESC @ 初始化打印机ESC D n1...nk NUL 设置横向跳格位置ESC E n 选择/取消加粗模式ESC G n 选择/取消双重打印模式ESC J n 打印并走纸ESC L 选择页模式ESC M n 选择字体ESC R n选择国际字符集ESC S 选择标准模式ESC T n 在页模式下选择打印区域方向ESC V n 选择字符旋转打印ESC W xL xH yL yH dxL dxH dyL dyH 页模式下设置打印区域ESC \ nL nH 设置相对横向打印位置ESC a n 选择字符对齐模式ESC c 5 n 允许/禁止按键ESC d n 打印并向前走纸n 行ESC p m t1 t2 产生钱箱控制脉冲ESC t n 选择国际扩展字符码表ESC { n 选择/取消倒置打印模式FS p n m 打印下载到FLASH 中的位图FS q n [xL xH yL yH d1...dk]1...[xL xH yL yH d1...dk]n 定义Flash 位图GS ! n 选择字符大小GS # n 定义当前下载图号GS $ nL nH 页模式下设置纵向绝对位置GS * x y d1...d(x×y×8) 定义下载位图GS / m 打印下载位图GS : 开始/结束宏定义GS B n 选择/取消黑白反显打印模式GS H n 选择HRI 字符的打印位置GS L nL nH 设置左边距GS P x y 设置横向和纵向移动单位①GS V m②GS V m n 选择切纸模式并切纸GS W nL nH 设置打印区域宽度GS \ nL nH 页模式下设置纵向相对位置GS ^ r t m 执行宏命令GS f n 选择HRI 使用字体GS h n 选择条码高度①GS k m d1...dk NUL②GS k m n d1...dn 打印条码GS v 0 m xL xH yL yH d1....dk 打印光栅位图GS w n 设置条码宽度FS ! n 设置汉字字符模式FS & 选择汉字模式FS - n 选择/取消汉字下划线模式FS . 取消汉字模式FS 2 c1 c2 d1...dk 定义用户自定义汉字FS S n1 n2 设置汉字字符左右间距FS W n 选择/取消汉字倍高倍宽。

使用ESC指令控制POS机小票

使用ESC指令控制POS机小票

使用ESC指令控制POS机小票C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC 指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos 打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。

因此个人建议使用ESC 指令进行打印会更通用。

本类需要调用ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;usingusing System.Text;usingusing System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using /doc/6213217853.html,monBLL;usingnamespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}//////对齐方式///public enum eT extAlignMode{Left = 0,Middle = 1,Right = 2}#endregion//////小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)/// PrintImageFile 或PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用EndPrint执行真正打印动作/// 6 退出程序前调用Close///public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";//////打印并走纸///private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以ID 开头private static String Const_ResponseQueryID = "ID";//////设置图标的指令///private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义//////最大字体大小///public const Int32 Const_MaxFontSize = 8;//////最大走纸距离///public const Int32 Const_MaxFeedLength = 5000;//////最大高宽///public const Int32 Const_MaxImageLength = 480;//////每次通信最多打印的行数///public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;//////无效句柄///public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员//////打印机句柄///private int m_Handle = -1;//////是否已经初始化///private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);//////发送指令/////////private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}//////发送文本格式的指令/////////private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4){return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringT oBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理- 打印图片//////把图片转换为指令字节,图片最大高宽不能超过480////////////public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight) {Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength){//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}//////把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍////////////public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}//////把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印////////////public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height %Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, refallbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();//////当前使用的打印机名称///public String PrinterName{get;private set;}//////单件模式//////public static ReceiptHelper GetInstance(){return m_instance;}//////获取本机安装的所有打印机//////public static List GetPrinterList(){List ret = new List();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}//////打开打印机/////////public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///开始打印,在打印之前必须调用此函数//////public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document"; di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di); if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}//////结束打印,在打印结束之后必须调用此函数//////public Boolean EndPrint(){if (!m_Inited)return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}//////销毁//////public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}//////打印文本.在调用本函数之前必须先调用正确的设置字体、左边距/////////public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D && content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringT oBytes(content); bool bl = SendCommand(bytes);return bl;}//////设置对齐方式/////////public bool SetAlignMode(eTextAlignMode alignmode) {if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left; break;case eTextAlignMode.Middle: code = Const_Align_Middle; break;case eTextAlignMode.Right:code = Const_Align_Right; break;default:code = Const_Align_Left; break;}//注意:先低字节后高字节string str = Const_SetAlign + code; bool bl = SendCommand(str); return bl;}//////设置左边距/////////public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}//////设置粗体/////////public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES;}else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}//////切纸//////public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}//////打印图片/////////public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename); if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}//////打印图片/////////public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}//////打印图片/////////public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height) {if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数= 1B 2A 21 2字节长度+数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16T oBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}//////走纸/////////public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength) {length = Const_MaxFeedLength; }string len = length.ToString("X2"); len = Const_FeedForward + len; bool bl = SendCommand(len); return bl;}//////回退走纸/////////public bool FeedBack(int length) {if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength) {length = Const_MaxFeedLength; }string len = length.ToString("X2"); len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}//////设置字体大小.本函数不可与SetBigFont同时使用//////大小倍率,取值范围1 - 8 ///public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理 ImageProcessorusing System;usingusing System.Linq;using System.Text;using System.Drawing;using /doc/6213217853.html,monBLL; usingusing System.IO;usingusingusingnamespace LaisonTech.MediaLib{//////图片格式///public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}//////转为灰度图像的方式///public enum eGrayMode{///算数平均///ArithmeticAverage = 0,///加权平均///WeightedAverage = 1,}//////比较2个图片的指定区域范围,像素的相同类型///public enum eAreaDifferentType{//////所有像素都相同///AllSame = 0,//////所有像素都不同///AllDifferent = 1,//////部分相同部分不同///Partial = 2,}//////图片文件处理///public class ImageProcessor#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;//////比较结果的图片里,亮度相同部分的填充颜色///public static Color Const_SameBrightnessColor = Color.Black;//////比较结果的图片里,亮度相同部分的填充颜色///public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;//////亮度的中间值public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印//////获取屏幕分辨率/////////public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screenwidth = Screen}//////截图指定控件上显示的内容/////////public static Image CaptureControlImage(Control ctrl) {if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointT oScreen(ctrl.Location); Image ret = new Bitmap(ctrl.Width, ctrl.Height); Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片//////装载图像文件/// /// ///。

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS机打印小票
#region 私有函数
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
//设置粗体 private const string Const_SetBold = "1B 45 "; private const String Const_Bold_YES = "01"; private const String Const_Bold_NO = "00";
//设置对齐方式 private const string Const_SetAlign = "1B 61 "; private const String Const_Align_Left = "30"; private const String Const_Align_Middle = "31"; private const String Const_Align_Right = "32";
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

打印机ESCPOS控制指令

打印机ESCPOS控制指令

打印机E S C P O S控制指令集团企业公司编码:(LL3698-KKI1269-TM2483-LUI12689-ITT289-H D D-80260打印机E S C/P O S控制指令ESC/POS控制指令HT横向跳格如果下一个跳格位置没有设置,该指令被忽略。

如果下一个横向跳格位置超越打印区域,将当前位置设置为[打印宽度+1]。

水平跳格位置由指令ESCD来设置。

如果接收到此命令时当前位置在[打印宽度+1],打印机执行当前行缓冲区满动作并且将打印位置移到下一行的起始位置。

默认值跳格位置是每8个标准ASCII字符(12×24)字符跳一格。

当前行缓冲区满时,打印机执行下列动作:标准模式下,打印机打印当前行内容并将打印位置置于下一行的起始位置。

页模式下,打印机进行换行并将打印位置置于下一行的起始位置。

LF打印并走纸一行该指令将当前位置置于行首。

FF打印并走纸打印后清除缓冲区的内容。

由ESCW指令设置的打印区域被重置为默认设置。

打印机不执行切纸动作。

该指令将当前位置置于行首。

该指令只在页模式下有效。

CR打印并回车设置打印位置为行首。

串行接口模式下该指令被忽略。

并口模式下该指令是否有效由打印机配置决定。

CAN页模式下删除打印缓冲区内容该指令只在页模式下有效。

如果先前设定区域与当前区域有重叠部分,重叠部分也被删除。

DLEDC4nmt实时产生钱箱开启脉冲当打印机正在执行钱箱开启命令(ESCp或DELDC4)时,该命令被忽略。

串口模式下,打印机接收该指令后立即执行。

并口模式下,打印机忙的时候该指令不执行。

如果打印数据中包含和此指令相同的数据,则这些数据将被当作该指令执行。

用户必须考虑到这种情况。

该指令尽量不要插在2个或更多字节的指令序列中。

即使打印机被ESC=(选择外设)命令设置为禁止,该指令依然有效。

ESCFF页模式下打印该指令只在页模式下有效。

打印后不清除打印缓冲区的内容、ESCT和ESCW设置和字符的位置。

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos打印机,并且无法发送控制指令实现pos 打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs25035844@2.POS机打印小票ReceiptHelperusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;using Microsoft.Win32.SafeHandles;namespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数///4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符) /// PrintImageFile 或 PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用 EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与 SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于 SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以 ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In,MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4) {return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理 - 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength) {//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage); }//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height % Const_OncePrintRowCount; }width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8 allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, refallbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode){if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES; }else{str = Const_SetBold + Const_Bold_NO; }bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength ||width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数 = 1B 2A 21 2字节长度 +数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值范围 1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理ImageProcessorusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using monBLL;using System.Drawing.Imaging;using System.IO;using System.Drawing.Drawing2D;using System.Windows.Forms;using AForge.Imaging.Filters;namespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域范围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screen.PrimaryScreen.Bounds.Height;width = Screen.PrimaryScreen.Bounds.Width;}///<summary>///截图指定控件上显示的内容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上方法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message); myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename) {FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename){Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat)case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

(完整版)ESC打印控制命令集

(完整版)ESC打印控制命令集

ESC打印控制命令集概述LANPOS5896打印机提供了ESC/POS打印命令集。

各个命令的描述形式如下:打印命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:该命令功能和使用说明例子:为了更容易理解该命令会列出一些例子。

打印命令LF 打印并换行格式:ASCII:LF十进制:10十六进制:0A说明:打印行缓冲器里的内容并向前走纸一行。

当行缓冲器空时只向前走纸一行。

ESC J 打印并走纸N点行格式:ASCII:ESC J n十进制:27 74 n十六进制:1B 4A n说明:打印行缓冲器里的内容,并向前走纸n点行(即n/203英寸)。

n=0~255。

该命令只本行打印有效,不改变ESC 2,ESC 3命令设置的行间距值。

示例:行间距设置命令ESC 2 设置字符行间距为1/6英寸格式:ASCII:ESC 2十进制:27 50十六进制:1B 32说明:设置行间距为1/6英寸ESC 3 设置行间距为n点行(n/203英寸)格式:ASCII:ESC 3十进制:27 51十六进制:1B 33说明:设置行间距为n点行。

n=0~255。

LANPOS5896打印机的每点距为1/203英寸,即该命令设置行间距为n/203英寸。

默认值为n=30。

示例:字符打印命令ESC !设置字符打印方式格式:ASCII:ESC !N十进制:27 33 N十六进制:1B 21 N说明:ESC !n是综合性的字符打印方式设置命令,用于选择打印字符的大小。

打印参数n的每位定义为:n的默认值为0,即字符不放大。

ESC SO 设置字符倍宽打印格式:ASCII:ESC SO十进制:27 14十六进制:1B 0E说明:在一行内该命令之后的所有字符均以正常宽度的2倍打印;该命令可以用回车或DC4命令删除。

ESC DC4 取消字符倍宽打印格式:ASCII:ESC DC4十进制:27 20十六进制:1B 14说明:执行些命令后,字符恢复正常宽度打印。

C使用ESC指令控制POS机打印小票

C使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos打印机,并且无法发送控制指令实现pos 打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;using ;using System.Text;using ;using System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;using ;namespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数///4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符) /// PrintImageFile 或 PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用 EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与 SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于 SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以 ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In,MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4) {return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理 - 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength) {//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage); }//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height % Const_OncePrintRowCount; }width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8 allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, refallbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if ( < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode){if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES; }else{str = Const_SetBold + Const_Bold_NO; }bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength ||width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数 = 1B 2A 21 2字节长度 +数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值范围 1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理ImageProcessorusing System;using ;using System.Linq;using System.Text;using System.Drawing;using monBLL;using ;using System.IO;using ;using ;using ;namespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域范围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = ;width = ;}///<summary>///截图指定控件上显示的内容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上方法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message); myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename) {FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename){Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat)case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

ESC打印机的控制命令

ESC打印机的控制命令

ESC打印机的控制命令ESC打印机的控制命令计算机能够完成数值计算、数据处理、图象设计、过程控制等工作都是在一种或多种计算机语言的指挥下进行的。

同样,打印机各种部件的运行和动作,各种打印动作的实现、在各种介质上打印出图形和字符,也是由一套能为打印机所识别的语言(命令)来进行控制。

通常将能完成上述功能的命令的集合成为打印机控制命令(Print Control Command)为了更好的熟知打印机控制命令,我们需先行了解打印机的数据处理流程。

二、打印机的控制命令目前主流的打印机控制命令主要有以下三种:EPSON公司的ESC PK命令集HP公司的PCL命令集Adobe公司的PostScript(简称PS)命令集ESC PK命令集是针式打印机(尤其是滚筒方式打印)领域事实上的工业标准;PCL命令集是中、低档激光打印机、喷墨机印机领域的工业标准;PS命令集则是高档排版、出版领域的应用标准。

1、名词解释1)控制命令:对打印机进行各种处理和控制动作,并为打印机所识别的命令序列。

2)仿真;控制打印机按照某种控制命令进行工作,就称为这个控制命令的仿真。

3)驱动程序;转换文件成为某种特定控制命令描述的文档,并进行输出的程序。

注:驱动程序就是打印控制命令的形体表现,其本质就是按照某一控制命令的格式,将计算机的数据进行翻译和组织,并形成相应的字符和位图的序列送往打印机。

2、命令分类打印机控制命令的种类可分为二大类:基本控制命令和扩充命令。

1)基本命令(Basic Control Code)基本命令其实就是ASCII码中规定的具有打印功能的控制命令,它的特点是均用一个ASCII码(一个字节)代表,使用比较方便。

它的数值范围如下:ASCII: 00H ~ 20H2)扩充命令(Extension Control Command)基本命令仅提供标准和基本的控制功能。

要实现字型大小、各种修饰功能、行与列间距控制、格式设定等较复杂的控制功能,就必须使用扩充命令。

机ESCPOS控制指令

机ESCPOS控制指令

H D D-80260打印机E S C/P O S控制指令ESC/POS控制指令HT横向跳格LF打印并走纸一行FF打印并走纸CR打印并回车CAN页模式下删除打印缓冲区内容DLEDC4nmt实时产生钱箱开启脉冲ESCFF页模式下打印ESCSPn设置字符右间距ESC!n选择打印模式ESC$nLnH设置绝对打印位置ESC%n选择/取消用户自定义字符ESC&yc1c2[x1d1...d(y×x1)]...[xkd1...d(y×xk)]定义用户自定义字符ESC*mnLnHd1...dk选择位图模式ESC–n选择/取消下划线模式ESC2设置默认行高ESC3n设置行高ESC=n选择打印机ESCn取消用户自定义字符ESC@初始化打印机ESCDn1...nkNUL设置横向跳格位置ESCEn选择/取消加粗模式ESCGn选择/取消双重打印模式ESCJn打印并走纸ESCL选择页模式ESCMn选择字体ESCRn选择国际字符集ESCS选择标准模式ESCTn在页模式下选择打印区域方向ESCVn选择字符旋转打印ESCWxLxHyLyHdxLdxHdyLdyH页模式下设置打印区域ESC\nLnH设置相对横向打印位置ESCan选择字符对齐模式ESCc5n允许/禁止按键ESCdn打印并向前走纸n行ESCpmt1t2产生钱箱控制脉冲ESCtn选择国际扩展字符码表ESC{n选择/取消倒置打印模式FSpnm打印下载到FLASH中的位图FSqn[xLxHyLyHd1...dk]1...[xLxHyLyHd1...dk]n定义Flash位图GS!n选择字符大小GS#n定义当前下载图号GS$nLnH页模式下设置纵向绝对位置GS*xyd1...d(x×y×8)定义下载位图GS/m打印下载位图GS:开始/结束宏定义GSBn选择/取消黑白反显打印模式GSHn选择HRI字符的打印位置GSLnLnH设置左边距GSPxy设置横向和纵向移动单位①GSVm②GSVmn选择切纸模式并切纸GSWnLnH设置打印区域宽度GS\nLnH页模式下设置纵向相对位置GS^rtm执行宏命令GSfn选择HRI使用字体GShn选择条码高度①GSkmd1...dkNUL②GSkmnd1...dn打印条码GSv0mxLxHyLyHd1....dk打印光栅位图GSwn设置条码宽度FS!n设置汉字字符模式FS&选择汉字模式FS-n选择/取消汉字下划线模式FS.取消汉字模式FS2c1c2d1...dk定义用户自定义汉字FSSn1n2设置汉字字符左右间距FSWn选择/取消汉字倍高倍宽。

爱普生EPSONESCPOS指令集说明

爱普生EPSONESCPOS指令集说明

爱普生【EPSON】ESC/POS指令集说明英文模式下的命令代码功能CR回车ESC !设置打印方式ESC %选择或取消用户自定义字符集ESC &定义用户自定义字符集ESC *设置位映射方式ESC @初始化打印机ESC ~LED ON/OFFESC <返回行首ESC 2选择行间距为1/6英寸ESC 3设置行进为最小间距ESC BEL蜂鸣器ON/OFFESC C设置单页长度ESC c0选择打印页ESC c1选择行间距ESC c3选择纸结束信号输出ESC c4选择打印纸及检测器(终止打印)ESC c5禁止/使能面板开关ESC c6禁止/使能ON-LINE开关ESC d打印及N行进纸ESC D设置TAB位置ESC e打印病退回N行ESC f设单页等待时间ESC F选择或取消单页退纸区ESC i全切割ESC J以最小间距进行打印和进纸ESC K以最小间距进行打印和退纸ESC l选择或取消倒过来的字符ESC m局部切割ESC o印章ESC p产生指定脉冲ESC q释放纸ESC r选择打印颜色ESC R选择国际字符子集ESC SP设置右边界ESC t选择字符码表ESC U选择或取消单向打印ESC V发送打印机状态ESC z设置或取消两页并行打印FF打印送出单页HT水平TABLF换行RS流水TAB中文模式下的命令代码功能FS – n设置中文字符下划线模式开关FS ! n选择中文字体FS &选择中文字符模式FS .取消中文模式---------------------------------------------------------------------------------------- EPSON打印控制指令集打印命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:该命令功能和使用说明。

HT水平制表格式:ASCII:HT十进制:09十六进制:09说明:打印位置进行到下一水平制表位置。

分享epson打印控制命令(ESCPOS)

分享epson打印控制命令(ESCPOS)

分享epson打印控制命令(ESCPOS).txt你站在那不要动!等我飞奔过去!雨停了天晴了女人你慢慢扫屋我为你去扫天下了你是我的听说现在结婚很便宜,民政局9块钱搞定,我请你吧你个笨蛋啊遇到这种事要站在我后面!跟我走总有一天你的名字会出现在我家的户口本上。

分享epson打印控制命令(ESC/POS)供参考在 Delphi / VCL组件开发及应用打印控制命令5.1.打印控制命令5.1.1 概述BS-210BM 提供了ESC/POS命令集。

下面将对每个命令进行说明。

每个命令的说明包括格式和解释两部分。

格式部分提供了命令的ASCII码、十进制码和十六进码三种格式,解释部分描述命令的功能。

三种命令的格式是等价的,用户可按照需要选择任一种。

5.1.2 各个命令的描述格式如下:打印命令功能格式: ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:该命令功能和使用说明。

5.1.3 命令说明HT 水平制表格式: ASCII:HT十进制:09十六进制:09说明:打印位置进行到下一水平制表位置。

如果当前打印位置超过了最后一个水平制表位置,则HT命令不被执行。

水平制表位置由 ESC D命令设置。

LF 打印并换行格式: ASCII:LF十进制:10十六进制:0A说明:打印存放在行编辑缓存中的一行数据并按当前设定的行距向前走纸一行。

ESC2,ESC3指令可设定行距的长短。

如果行编辑缓存空,则只按当前设定的行距向前走纸一行。

CR 打印回车格式: ASCII:CR十进制:13十六进制:0D说明:当打印在行编辑缓存中的一行数据之后,不进行走纸操作。

ESC SP 设置字符右间距格式: ASCII:ESC SP n十进制:27 32 n十六进制:1B 20 n说明:以半点为设定单位(1/144英寸),设置字符右边间距为n个半点距。

默认值n=0。

ESC !设置字符打印方式格式: ASCII:ESC !n十进制:27 33 n十六进制:1B 21 n说明:0≤n≤255ESC !n是综合性的字符打印方式设置命令,用于选择打印字符的大小和下划线。

ESCPOS打印控制命令

ESCPOS打印控制命令

ESC/POS打印控制命令1、打印命令一览表命令速查命令说明打印命令LF 打印并换行ESC J 打印并走纸n点行格式设置命令ESC 2 设置行间距1/6英寸ESC 3 n 设置行间距为n点行(n/137英寸) ESC a n 设置对齐方式字符设置命令ESC ! n 设置打印字符格式ESC SO 设置字符倍宽打印ESC DC4 取消字符倍宽打印ESC { n 设置/取消字符上下倒置ESC B n 设置/取消字符反白打印ESC % n 设置/取消用户自定义字符ESC & 设定用户自定义字符ESC ? 取消用户自定义字符ESC R n 选择国际字符集ESC t n 选择字符代码页图形设置命令ESC * 打印一点行图形GS * 下装点图设置GS / 打印下装点图钱箱控制命令ESC p 钱箱控制按键控制命令ESC c 5 允许/禁止按键开关初始化命令ESC @ 打印机初始化状态传输命令ESC v 向主机传送打印机状态ESC u 向主机传送周边设置状态GS a 允许/禁止状态自动上传条码设置命令GS H 选择HRI打印方式GS h 设置条码高度GS w 设置条码横向宽度GS k 打印条码位置和页模式命令ESC L 设置页模式FF 打印,并返回到标准模式ESC FF 打印页模式的数据CAN 清除页缓冲区的数据ESC W … 设置页位置和大小ESC $ nL nH 设置页模式的绝对X位置GS $ nL nH 设置页模式的绝对Y位置ESC \ nL nH 设置页模式的相对X位置GS \ nL nH 设置页模式的相对Y位置GS L nL nH 设置页左边距GS W nL nH 设置页宽切纸模式命令ESC i 切纸但不走纸2、打印命令详解概述POS-58打印机使用ESC/POS打印命令集。

打印命令按以下格式说明:命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:命令功能和使用说明。

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs25035844@qq.2.POS机打印小票ReceiptHelperusing System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;using Microsoft.Win32.SafeHandles;namespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印容.注意:打印该行容后会自动换行(本类会在该行容末尾添加一个换行符) /// PrintImageFile 或PrintBitMap打印图片/// 5 控制指令和打印容都发送完毕后,调用EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 }; //走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30}; //查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61}; //回复帧以ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 }; #endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartDocPrinter(Int32 hPrinter, Int32 level, [In,MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2){return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4) {return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region部处理- 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过围,或宽度超过围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height >Const_MaxImageLength){//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth / (float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width / (float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes, ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height %Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, ref allbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters){ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode){if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES;}else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height);bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数= 1B 2A 21 2字节长度+ 数据容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值围1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理ImageProcessor using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using monBLL;using System.Drawing.Imaging;using System.IO;using System.Drawing.Drawing2D;using System.Windows.Forms;using AForge.Imaging.Filters;namespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White; public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screen.PrimaryScreen.Bounds.Height;width = Screen.PrimaryScreen.Bounds.Width;}///<summary>///截图指定控件上显示的容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上方法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message);myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename) {FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename){Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat){case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

使用ESC指令控制POS机小票

使用ESC指令控制POS机小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC 指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos 打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;usingusing System.Text;usingusing System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;usingnamespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)]public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)]public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess;}///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)/// PrintImageFile 或 PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用 EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与 SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于 SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 };//走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30};//查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61};//回复帧以 ID 开头private static String Const_ResponseQueryID = "ID";///<summary>///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 };#endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽///</summary>public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(Int32hPrinter, Int32level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2) {return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///</summary>///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4){return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理 - 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight){Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height >Const_MaxImageLength){//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth /(float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width /(float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes,ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null){return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null){return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height %Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height){curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, ref allbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance(){return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters) {ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle);bl = EndDocPrinter(m_Handle);return bl;}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle);m_Handle = -1;return true;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode) {if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES;}else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap){if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height); bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数 = 1B 2A 21 2字节长度 +数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值范围 1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理 ImageProcessorusing System;usingusing System.Linq;using System.Text;using System.Drawing;using monBLL;usingusing System.IO;usingusing System.Windows.Forms;usingnamespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat{Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域范围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screenwidth = Screen}///<summary>///截图指定控件上显示的内容///</summary>///<param name="ctrl"></param>///<returns></returns>public static Image CaptureControlImage(Control ctrl){if (ctrl == null){return null;}Control parent = ctrl;if (ctrl.Parent != null){parent = ctrl.Parent;}Point screenPoint = parent.PointToScreen(ctrl.Location);Image ret = new Bitmap(ctrl.Width, ctrl.Height);Graphics g = Graphics.FromImage(ret);g.CopyFromScreen(screenPoint.X, screenPoint.Y,0, 0, ctrl.Size);g.DrawImage(ret, 0, 0);return ret;}#endregion#region装载图片///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Image LoadImage(String filename){//Boolean bl = FileProcessor.FileExist(filename);//if (!bl)//{// return null;//}//Bitmap image = (Bitmap)Bitmap.FromFile(filename);//return image;//以上方法会导致图片文件被锁定,无法删除移动Byte[] photodata = null;Boolean bl = FileProcessor.FileExist(filename);if (!bl){return null;}bl = FileProcessor.ReadFileBytes(filename, out photodata);if (!bl){return null;}MemoryStream ms = null;Image myImage = null;try{ms = new MemoryStream(photodata);myImage = Bitmap.FromStream(ms);ms.Close();}catch (System.Exception ex){Console.WriteLine("LoadImage error:" + ex.Message);myImage = null;}return myImage;}///<summary>///装载图像文件///</summary>///<param name="filename"></param>///<returns></returns>public static Bitmap LoadBitImage(String filename){Bitmap ret = (Bitmap)LoadImage(filename);return ret;}///<summary>///保存图片到指定路径///</summary>///<param name="img"></param>///<param name="filename"></param>///<returns></returns>public static Boolean SaveImage(Image img, String filename){FileProcessor.DeleteFile(filename);if (img == null){return false;}//获取保存图片的路径,如果路径不存在,新建String folder = FileProcessor.GetDirectoryName(filename);if (!FileProcessor.DirectoryExist(folder)){FileProcessor.CreateDirectory(folder);}img.Save(filename);Boolean bl = FileProcessor.FileExist(filename);return bl;}#endregion#region转换图片格式///<summary>///转换图片格式///</summary>///<param name="bmpfilename"></param>///<param name="jpgfilename"></param>///<returns></returns>public static Boolean BmpToJpg(String bmpfilename, String jpgfilename) {Boolean bl = ChangeFileFormat(bmpfilename, jpgfilename, ePictureFileFormat.Jpeg);return bl;}///<summary>///转换图片格式///</summary>///<param name="srcfilename"></param>///<param name="destfilename"></param>///<param name="destformat"></param>///<returns></returns>public static Boolean ChangeFileFormat(String srcfilename, String destfilename, ePictureFileFormat destformat){Boolean bl = FileProcessor.FileExist(srcfilename);if (!bl){return false;}Image image = Image.FromFile(srcfilename);ImageFormat IFMT = null;switch (destformat){case ePictureFileFormat.Bmp:IFMT = ImageFormat.Bmp;break;case ePictureFileFormat.Gif:IFMT = ImageFormat.Gif;break;case ePictureFileFormat.Icon:IFMT = ImageFormat.Icon;break;case ePictureFileFormat.Jpeg:IFMT = ImageFormat.Jpeg;break;case ePictureFileFormat.Png:IFMT = ImageFormat.Png;break;default:IFMT = ImageFormat.Jpeg;break;}image.Save(destfilename, IFMT);image.Dispose();bl = FileProcessor.FileExist(destfilename);if (!bl){return false;}Int32 filelen = FileProcessor.GetFileLength(destfilename);return (filelen > 0);}///<summary>///变成黑白图///</summary>///<param name="srcbitmap">原始图</param>///<param name="mode">模式。

打印机控制指令

打印机控制指令

HDD-80260打印机ESC/POS 控制指令ESC/POS 控制指令HT 横向跳格LF 打印并走纸一行FF 打印并走纸CR 打印并回车CAN 页模式下删除打印缓冲区内容DLE DC4 n m t 实时产生钱箱开启脉冲ESC FF 页模式下打印ESC SP n 设置字符右间距ESC ! n 选择打印模式ESC $ nL nH 设置绝对打印位置ESC % n 选择/取消用户自定义字符ESC & y c1 c2 [x1 d1...d(y ×x1)]...[xk d1...d(y ×xk)] 定义用户自定义字符ESC * m nL nH d1...dk 选择位图模式ESC – n 选择/取消下划线模式ESC 2 设置默认行高ESC 3 n 设置行高ESC = n 选择打印机ESC n 取消用户自定义字符ESC @ 初始化打印机ESC D n1...nk NUL 设置横向跳格位置ESC E n 选择/取消加粗模式ESC G n 选择/取消双重打印模式ESC J n 打印并走纸ESC L 选择页模式ESC M n 选择字体ESC R n选择国际字符集ESC S 选择标准模式ESC T n 在页模式下选择打印区域方向ESC V n 选择字符旋转打印ESC W xL xH yL yH dxL dxH dyL dyH 页模式下设置打印区域ESC \ nL nH 设置相对横向打印位置ESC a n 选择字符对齐模式ESC c 5 n 允许/禁止按键ESC d n 打印并向前走纸n 行ESC p m t1 t2 产生钱箱控制脉冲ESC t n 选择国际扩展字符码表ESC { n 选择/取消倒置打印模式FS p n m 打印下载到FLASH 中的位图FS q n [xL xH yL yH d1...dk]1...[xL xH yL yH d1...dk]n 定义Flash 位图GS ! n 选择字符大小GS # n 定义当前下载图号GS $ nL nH 页模式下设置纵向绝对位置GS * x y d1...d(x×y×8) 定义下载位图GS / m 打印下载位图GS : 开始/结束宏定义GS B n 选择/取消黑白反显打印模式GS H n 选择HRI 字符的打印位置GS L nL nH 设置左边距GS P x y 设置横向和纵向移动单位①GS V m②GS V m n 选择切纸模式并切纸GS W nL nH 设置打印区域宽度GS \ nL nH 页模式下设置纵向相对位置GS ^ r t m 执行宏命令GS f n 选择HRI 使用字体GS h n 选择条码高度①GS k m d1...dk NUL②GS k m n d1...dn 打印条码GS v 0 m xL xH yL yH d1....dk 打印光栅位图GS w n 设置条码宽度FS ! n 设置汉字字符模式FS & 选择汉字模式FS - n 选择/取消汉字下划线模式FS . 取消汉字模式FS 2 c1 c2 d1...dk 定义用户自定义汉字FS S n1 n2 设置汉字字符左右间距FS W n 选择/取消汉字倍高倍宽。

c使用esc指令控制pos机打印小票

c使用esc指令控制pos机打印小票

C#使用ESC指令控制POS打印机打印小票1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。

也可以发送标注你的ESC 指令实现。

由于调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos 打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。

因此个人建议使用ESC指令进行打印会更通用。

本类需要调用 ImageProcessor.cs2.POS机打印小票ReceiptHelperusing System;usingusing System.Text;usingusing System.Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using monBLL;usingnamespace LaisonTech.MediaLib{#region结构体定义[StructLayout(LayoutKind.Sequential)] public struct OVERLAPPED{int Internal;int InternalHigh;int Offset;int OffSetHigh;int hEvent;};[StructLayout(LayoutKind.Sequential)] public struct PRINTER_DEFAULTS{public int pDatatype;public int pDevMode;public int DesiredAccess; }///<summary>///对齐方式///</summary>public enum eTextAlignMode{Left = 0,Middle = 1,Right = 2}#endregion///<summary>///小票打印类/// 使用方法:/// 1 GetPrinterList获取已经安装的所有打印机列表./// Open 打开指定打印机/// 2 控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/// 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize ... ...设置打印参数/// 4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)/// PrintImageFile 或 PrintBitMap打印图片/// 5 控制指令和打印内容都发送完毕后,调用 EndPrint执行真正打印动作/// 6 退出程序前调用Close///</summary>public class ReceiptHelper{#region指令定义private static Byte[] Const_Init = new byte[] { 0x1B, 0x40,0x20, 0x20, 0x20, 0x0A,0x1B, 0x64,0x10};//设置左边距private const string Const_SetLeft = "1D 4C ";//设置粗体private const string Const_SetBold = "1B 45 ";private const String Const_Bold_YES = "01";private const String Const_Bold_NO = "00";//设置对齐方式private const string Const_SetAlign = "1B 61 ";private const String Const_Align_Left = "30";private const String Const_Align_Middle = "31";private const String Const_Align_Right = "32";//设置字体大小,与 SetBigFont 不能同时使用private const string Const_SetFontSize = "1D 21 ";//设置是否大字体,等同于 SetFontSize = 2//private const String Const_SetBigFontBold = "1B 21 38";//private const String Const_SetBigFontNotBold = "1B 21 30";//private const String Const_SetCancelBigFont = "1B 21 00";///<summary>///打印并走纸///</summary>private static Byte[] Const_Cmd_Print = new byte[] { 0x1B, 0x4A, 0x00 }; //走纸private const string Const_FeedForward = "1B 4A ";private const string Const_FeedBack = "1B 6A ";//切纸private static Byte[] Const_SetCut = new byte[] { 0x1D, 0x56, 0x30}; //查询打印机状态private static Byte[] Const_QueryID = new byte[] { 0x1D, 0x67, 0x61}; //回复帧以 ID 开头private static String Const_ResponseQueryID = "ID";///设置图标的指令///</summary>private static Byte[] Const_SetImageCommand = new Byte[] { 0x1B, 0x2A, 0x21 };#endregion#region常量定义///<summary>///最大字体大小///</summary>public const Int32 Const_MaxFontSize = 8;///<summary>///最大走纸距离///</summary>public const Int32 Const_MaxFeedLength = 5000;///<summary>///最大高宽public const Int32 Const_MaxImageLength = 480;///<summary>///每次通信最多打印的行数///</summary>public const Int32 Const_OncePrintRowCount = 24;public const Int32 Const_BrightnessGate = 100;///<summary>///无效句柄///</summary>public const Int32 Const_InvalidHandle = -1;#endregion#region私有成员///<summary>///打印机句柄///</summary>private int m_Handle = -1;///<summary>///是否已经初始化///</summary>private Boolean m_Inited = false;#endregion#region私有函数[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,out Int32 hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(Int32hPrinter, Int32level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool EndDocPrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool StartPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool EndPagePrinter(Int32 hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool WritePrinter(Int32 hPrinter, Byte[] pBytes, Int32 dwCount, out Int32 dwWritten);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] public static extern bool ClosePrinter(Int32 hPrinter);///<summary>///发送指令///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(Byte[] cmd){if (m_Handle == Const_InvalidHandle || cmd == null || cmd.Length < 2) {return false;}int writelen = 0;Boolean bl = WritePrinter(m_Handle, cmd, cmd.Length, out writelen);if (!bl) return false;return (writelen >= cmd.Length);}///<summary>///发送文本格式的指令///<param name="cmd"></param>///<returns></returns>private Boolean SendCommand(String hexstrcmd){if (m_Handle == Const_InvalidHandle || hexstrcmd == null || hexstrcmd.Length < 4){return false;}byte[] mybyte = null;Boolean bl = DataFormatProcessor.HexStringToBytes(hexstrcmd, out mybyte);bl = SendCommand(mybyte);return bl;}#endregion#region内部处理 - 打印图片///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImage(Bitmap image,ref Byte[] bitarray,ref Int32 datawidth,ref Int32 dataheight) {Int32 newwidth = 0;Int32 newheight = 0;Bitmap destimage = image;Boolean bl = false;//如果高度超过范围,或宽度超过范围,需要进行缩小if (image.Width > Const_MaxImageLength || image.Height > Const_MaxImageLength){//按照高度和宽度,较大的那一边,进行缩放if (image.Width > image.Height){newwidth = Const_MaxImageLength;newheight = (Int32)(image.Height * newwidth /(float)image.Width);}else{newheight = Const_MaxImageLength;newwidth = (Int32)(newheight * image.Width /(float)image.Height);}bl = ImageProcessor.ResizeImage(image, newwidth, newheight, ref destimage);}//把数据转换为字节数组bl = GetBitArray(image, ref bitarray, ref datawidth, ref dataheight);return bl;}///<summary>///把图片转换为指令字节,图片最大高宽不能超过480///如果图片的高度不是24的整数倍,则修改为24的整数倍///</summary>///<param name="image"></param>///<param name="bmpbytes"></param>///<returns></returns>public static Boolean LoadImageFromFile(String imagefilename, ref Byte[] bmpbytes,ref Int32 width, ref Int32 height){Bitmap img = ImageProcessor.LoadBitImage(imagefilename);if (img == null)return false;}Boolean bl = LoadImage(img, ref bmpbytes, ref width, ref height);return bl;}///<summary>///把图片转换为位图数组,每个字节的每个比特位,对应当前像素是否需要打印///</summary>///<param name="img"></param>///<param name="allbitary"></param>///<returns></returns>public static Boolean GetBitArray(Bitmap img,ref Byte[] allbitary, ref Int32 width, ref Int32 height){if (img == null)return false;}//ESC指令格式规定://1 打印图片时,每条指令最多只打印24行;不足24行的,也要用全0填充满数据字节//2 打印24行数据时,按照光栅模式纵向获取数据// 即先获取所有x=0的点(第0列)转换为3个字节;// 再获取所有x=1的点转换为3个字节;...直到获取到最右侧一列的点//3 打印完当前24行数据后,再获取后续24行的数据内容,直到所有的数据获取完毕//获取亮度数组Boolean[] briary = null;Boolean bl = ImageProcessor.ToBooleanArray(img, Const_BrightnessGate, ref briary);if (!bl){return false;}height = img.Height;//如果图像高度不是24整数倍,设置为24的整数倍if (height % Const_OncePrintRowCount != 0){height = height + Const_OncePrintRowCount - height %Const_OncePrintRowCount;}width = img.Width;//如果图像宽度不是8的整数倍,设置为8的整数倍if (width % 8 != 0){width = width + 8 - width % 8;}Int32 bytelen = height * width / 8;//每个像素对应1个比特位,因此总字节数=像素位数/8allbitary = new Byte[bytelen];Int32 byteidxInCol = 0;//当前列里首个像素,在目标字节数组里的下标Int32 byteidx = 0;//当前像素在目标数组里的字节下标Int32 bitidx = 0;//当前像素在目标数组里当前字节里的比特位下标Int32 pixidxInCol = 0;//当前像素在当前列里的第几个位置Int32 pixidx = 0;//当前像素在原始图片里的下标Int32 rowidx = 0; //当前处理的像素点所在行,不能超过图像高度Int32 curprocrows = 0;//当前需要处理的行数量while (rowidx < height){//按照纵向次序,把当前列的24个数据,转换为3个字节for (Int32 colidx = 0; colidx < img.Width; ++colidx){//如果当前还剩余超过24行没处理,处理24行if (rowidx + Const_OncePrintRowCount <= img.Height) {curprocrows = Const_OncePrintRowCount;}else{//已经不足24行,只处理剩余行数curprocrows = img.Height - rowidx;}pixidxInCol = 0; //本列里从像素0开始处理for (Int32 y = rowidx; y < rowidx + curprocrows; ++y){//原始图片里像素位置pixidx = y * img.Width + colidx;//获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1if (briary[pixidx]){bitidx = 7 - pixidxInCol % 8;//最高比特位对应首个像素.最低比特位对应末个像素byteidx = byteidxInCol + pixidxInCol / 8; //由于最后一段可能不足24行,因此不能使用byteidx++DataFormatProcessor.SetBitValue(bitidx, true, ref allbitary[byteidx]);}pixidxInCol++;}byteidxInCol += 3;//每列固定24个像素,3个字节}rowidx += Const_OncePrintRowCount;}return true;}#endregion#region公开函数private static ReceiptHelper m_instance = new ReceiptHelper();///<summary>///当前使用的打印机名称///</summary>public String PrinterName{get;private set;}///<summary>///单件模式///</summary>///<returns></returns>public static ReceiptHelper GetInstance() {return m_instance;}///<summary>///获取本机安装的所有打印机///</summary>///<returns></returns>public static List<String> GetPrinterList(){List<String> ret = new List<String>();if (PrinterSettings.InstalledPrinters.Count < 1){return ret;}foreach (String printername in PrinterSettings.InstalledPrinters) {ret.Add(printername);}return ret;}///<summary>///打开打印机///</summary>///<param name="printername"></param>///<returns></returns>public Boolean Open(String printername){if (m_Inited){return true;}Boolean bl = OpenPrinter(printername.Normalize(), out m_Handle, IntPtr.Zero);m_Inited = (bl && m_Handle != 0);return true;}///<summary>///开始打印,在打印之前必须调用此函数///</summary>///<returns></returns>public Boolean StartPrint(){if (!m_Inited){return false;}DOCINFOA di = new DOCINFOA();di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";//Start a document.Boolean bl = StartDocPrinter(m_Handle, 1, di);if (!bl){return false;}// Start a page.bl = StartPagePrinter(m_Handle);return bl;}///<summary>///结束打印,在打印结束之后必须调用此函数///</summary>///<returns></returns>public Boolean EndPrint(){if (!m_Inited){return false;}Boolean bl = EndPagePrinter(m_Handle); bl = EndDocPrinter(m_Handle);}///<summary>///销毁///</summary>///<returns></returns>public Boolean Close(){if (!m_Inited){return true;}m_Inited = false;//关闭设备句柄ClosePrinter(m_Handle); m_Handle = -1;}///<summary>///打印文本.在调用本函数之前必须先调用正确的设置字体、左边距///</summary>///<param name="content"></param>///<returns></returns>public Boolean PrintText(String content){if (!m_Inited){return false;}byte[] bytes = null;if (content.Length < 1){content = " ";}if (content[content.Length - 1] != (char)0x0D &&content[content.Length - 1] != (char)0x0A){content = content + (char)0x0A;}bytes = DataFormatProcessor.StringToBytes(content);bool bl = SendCommand(bytes);return bl;}///<summary>///设置对齐方式///</summary>///<param name="left"></param>///<returns></returns>public bool SetAlignMode(eTextAlignMode alignmode) {if (!m_Inited){return false;}String code = String.Empty;switch (alignmode){case eTextAlignMode.Left:code = Const_Align_Left;break;case eTextAlignMode.Middle:code = Const_Align_Middle;break;case eTextAlignMode.Right:code = Const_Align_Right;break;default:code = Const_Align_Left;break;}//注意:先低字节后高字节string str = Const_SetAlign + code;bool bl = SendCommand(str);return bl;}///<summary>///设置左边距///</summary>///<param name="left"></param>///<returns></returns>public bool SetLeft(int left){if (!m_Inited){return false;}//注意:先低字节后高字节String hexstr = left.ToString("X4");string str = Const_SetLeft + hexstr.Substring(2, 2) + hexstr.Substring(0, 2);bool bl = SendCommand(str);return bl;}///<summary>///设置粗体///</summary>///<param name="bold"></param>///<returns></returns>public Boolean SetBold(Boolean bold){if (!m_Inited){return false;}//注意:先低字节后高字节String str = String.Empty;if (bold){str = Const_SetBold + Const_Bold_YES; }else{str = Const_SetBold + Const_Bold_NO;}bool bl = SendCommand(str);return bl;}///<summary>///切纸///</summary>///<returns></returns>public bool Cut(){if (!m_Inited){return false;}bool bl = SendCommand(Const_SetCut);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintImageFile(String imgfilename){if (!m_Inited){return false;}Bitmap img = ImageProcessor.LoadBitImage(imgfilename);if (img == null){return false;}Boolean bl = PrintBitmap(img);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmap(Bitmap bitmap) {if (!m_Inited){return false;}if (bitmap == null ||bitmap.Width > Const_MaxImageLength ||bitmap.Height > Const_MaxImageLength){return false;}Byte[] bitary = null;Int32 width = 0;Int32 height = 0;Boolean bl = GetBitArray(bitmap, ref bitary, ref width, ref height); bl = PrintBitmapBytes(bitary, bitmap.Width, bitmap.Height);return bl;}///<summary>///打印图片///</summary>///<param name="bitmap"></param>///<returns></returns>public bool PrintBitmapBytes(Byte[] imgbitarray, Int32 width, Int32 height){if (!m_Inited){return false;}Int32 bytes = width * height / 8;//检查是否尺寸符合要求if (width > Const_MaxImageLength || height > Const_MaxFeedLength || width < 1 || height < 1 ||imgbitarray == null){return false;}//每次获取24行的数据进行发送,这24行的字节数Int32 blockbytes = width * Const_OncePrintRowCount / 8;if (blockbytes < 1){return false;}Boolean bl = false;//一共需要发送的块数量Int32 blocks = imgbitarray.Length / blockbytes;//每次发送的数据字节数 = 1B 2A 21 2字节长度 + 数据内容Byte[] cmdbytes = new Byte[5 + blockbytes];//指令Array.Copy(Const_SetImageCommand, cmdbytes, 3);//数据长度,即每行的点数DataFormatProcessor.Int16ToBytes(width, ref cmdbytes, 3);//数据内容for (Int32 blockidx = 0; blockidx < blocks; ++blockidx){Array.Copy(imgbitarray, blockidx * blockbytes, cmdbytes, 5, blockbytes);//发送当前指令bl = SendCommand(cmdbytes);if (!bl) return false;//休眠20毫秒Thread.Sleep(20);//发送打印指令bl = SendCommand(Const_Cmd_Print);if (!bl) return false;}return bl;}///<summary>///走纸///</summary>///<param name="length"></param>///<returns></returns>public bool Feed(int length){if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength) {length = Const_MaxFeedLength; }string len = length.ToString("X2"); len = Const_FeedForward + len;bool bl = SendCommand(len);return bl;}///<summary>///回退走纸///</summary>///<param name="length"></param>///<returns></returns>public bool FeedBack(int length) {if (!m_Inited){return false;}if (length < 1)length = 1;if (length > Const_MaxFeedLength){length = Const_MaxFeedLength;}string len = length.ToString("X2");len = Const_FeedBack + len;bool bl = SendCommand(len);return bl;}///<summary>///设置字体大小.本函数不可与SetBigFont同时使用///</summary>///<param name="sizerate">大小倍率,取值范围 1 - 8</param>///<returns></returns>public bool SetFontSize(Int32 sizerate){if (!m_Inited){return false;}if (sizerate < 1){sizerate = 1;}if (sizerate > Const_MaxFontSize){sizerate = Const_MaxFontSize;}sizerate--;String sizecodestr = Const_SetFontSize + sizerate.ToString("X1") + sizerate.ToString("X1");bool bl = SendCommand(sizecodestr);return bl;}#endregion}}3.图像处理 ImageProcessor using System;usingusing System.Linq;using System.Text;using System.Drawing;using monBLL; usingusing System.IO;usingusing System.Windows.Forms; usingnamespace LaisonTech.MediaLib{///<summary>///图片格式///</summary>public enum ePictureFileFormat {Bmp = 0,Gif = 1,Icon = 2,Jpeg = 3,Png = 4,}///<summary>///转为灰度图像的方式///</summary>public enum eGrayMode{///<summary>///算数平均///</summary>ArithmeticAverage = 0,///<summary>///加权平均///</summary>WeightedAverage = 1,}///<summary>///比较2个图片的指定区域范围,像素的相同类型///</summary>public enum eAreaDifferentType{///<summary>///所有像素都相同///</summary>AllSame = 0,///<summary>///所有像素都不同///</summary>AllDifferent = 1,///<summary>///部分相同部分不同///</summary>Partial = 2,}///<summary>///图片文件处理///</summary>public class ImageProcessor{#region常量定义public const Byte Const_BrightnessWhite = 255;public const Byte Const_BrightnessBlack = 0;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_SameBrightnessColor = Color.Black;///<summary>///比较结果的图片里,亮度相同部分的填充颜色///</summary>public static Color Const_DifferentBrightnessColor = Color.White;public const Byte Const_BlackBrightness = 0;public const Byte Const_WhiteBrightness = 255;public const Int32 Const_MaxBrightness = 255;public const Int32 Const_MinBrightness = -255;///<summary>///亮度的中间值///</summary>public const Int32 Const_MiddleBrightness = 128;#endregion#region屏幕截图,打印///<summary>///获取屏幕分辨率///</summary>///<param name="width"></param>///<param name="height"></param>public static void GetScreenSize(ref Int32 width, ref Int32 height) {height = Screenwidth = Screen。

EPSON系列票据打印机指令集

EPSON系列票据打印机指令集

esc/pos打印指令(一) Post By:2005-5-16 1:46:00EPSON指令集打印控制命令5.1.打印控制命令5.1.1 概述BS-210BM 提供了ESC/POS命令集。

下面将对每个命令进行说明。

每个命令的说明包括格式和解释两部分。

格式部分提供了命令的ASCII码、十进制码和十六进码三种格式,解释部分描述命令的功能。

三种命令的格式是等价的,用户可按照需要选择任一种。

5.1.2 各个命令的描述格式如下:打印命令功能格式: ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:该命令功能和使用说明。

5.1.3 命令说明HT 水平制表格式: ASCII:HT十进制:09十六进制:09说明:打印位置进行到下一水平制表位置。

如果当前打印位置超过了最后一个水平制表位置,则HT命令不被执行。

水平制表位置由 ESC D命令设置。

LF 打印并换行格式: ASCII:LF十进制:10十六进制:0A说明:打印存放在行编辑缓存中的一行数据并按当前设定的行距向前走纸一行。

ESC2,ESC3指令可设定行距的长短。

如果行编辑缓存空,则只按当前设定的行距向前走纸一行。

CR 打印回车格式: ASCII:CR十进制:13十六进制:0D说明:当打印在行编辑缓存中的一行数据之后,不进行走纸操作。

ESC SP 设置字符右间距格式: ASCII:ESC SP n十进制:27 32 n十六进制:1B 20 n说明:以半点为设定单位(1/144英寸),设置字符右边间距为n个半点距。

默认值n=0。

ESC !设置字符打印方式格式: ASCII:ESC !n十进制:27 33 n十六进制:1B 21 n说明:0≤n≤255ESC !n是综合性的字符打印方式设置命令,用于选择打印字符的大小和下划线。

打印参数n的每位定义为:位功能值0 10 字模选择 7×9 5×71 无定义2 无定义3 无定义4 倍高取消设定5 倍宽取消设定6 无定义7 下划线取消设定默认值n=0,ESC % 允许/禁止用户自定义字符格式: ASCII:ESC % n十进制:27 37 n十六进制:1B 25 n说明:参数n为一个字节,只有最低位有效。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

ESC/POS打印控制命令1、打印命令一览表命令速查命令说明打印命令LF 打印并换行ESC J 打印并走纸n点行格式设置命令ESC 2 设置行间距1/6英寸ESC 3 n 设置行间距为n点行(n/137英寸) ESC a n 设置对齐方式字符设置命令ESC ! n 设置打印字符格式ESC SO 设置字符倍宽打印ESC DC4 取消字符倍宽打印ESC { n 设置/取消字符上下倒置ESC B n 设置/取消字符反白打印ESC % n 设置/取消用户自定义字符ESC & 设定用户自定义字符ESC ? 取消用户自定义字符ESC R n 选择国际字符集ESC t n 选择字符代码页图形设置命令ESC * 打印一点行图形GS * 下装点图设置GS / 打印下装点图钱箱控制命令ESC p 钱箱控制按键控制命令ESC c 5 允许/禁止按键开关初始化命令ESC @ 打印机初始化状态传输命令ESC v 向主机传送打印机状态ESC u 向主机传送周边设置状态GS a 允许/禁止状态自动上传条码设置命令GS H 选择HRI打印方式GS h 设置条码高度GS w 设置条码横向宽度GS k 打印条码位置和页模式命令ESC L 设置页模式FF 打印,并返回到标准模式ESC FF 打印页模式的数据CAN 清除页缓冲区的数据ESC W … 设置页位置和大小ESC $ nL nH 设置页模式的绝对X位置GS $ nL nH 设置页模式的绝对Y位置ESC \ nL nH 设置页模式的相对X位置GS \ nL nH 设置页模式的相对Y位置GS L nL nH 设置页左边距GS W nL nH 设置页宽切纸模式命令ESC i 切纸但不走纸2、打印命令详解概述POS-58打印机使用ESC/POS打印命令集。

打印命令按以下格式说明:命令功能格式:ASCII:以标准ASCII字符序列表示十进制:以十进制数字序列表示十六进制:以十六进制数字序列表示说明:命令功能和使用说明。

举例:命令的使用举例。

2.1 打印命令LF 打印并换行格式:ASCII:LF十进制:10十六进制:0A说明:打印行缓冲器里的内容并向前走纸一行。

当行缓冲器为空时只向前走纸一行。

在页模式下:输出行缓冲器里的内容,光标定位到下一行。

ESC J n 打印并走纸n点行格式:ASCII:ESC J n十进制:27 74 n十六进制:1B 4A n说明:n = 0-255。

打印行缓冲区里的内容,并向前走纸n点行。

该命令只对本行有效,不改变ESC 2,ESC 3命令设置的行间距值。

2.2 行间距设置命令ESC 2 设置行间距为1/6英寸格式:ASCII:ESC 2十进制:27 50十六进制:1B 32说明:设置行间距为1/6英寸=203/6=34点ESC 3 n 设置行间距为n点行格式:ASCII:ESC 3 n十进制:27 51 n十六进制:1B 33 n说明:n = 0-255设置行间距为n点行。

默认值行间距是30点。

ESC a n 设置输出对齐方式格式:ASCII:ESC a n十进制:27 97 n十六进制:1B 61 n说明:该命令只对标准模式有效0 ≤ m ≤ 2 或 48 ≤ m ≤ 50左对齐: n=0,48居中对齐: n=1,49右对齐: n=2,502.3 字符设置命令ESC ! n 设置打印字符模式格式:ASCII:ESC ! n十进制:27 33 n十六进制:1B 21 n说明:用于设置打印字符的方式。

默认值是0位0:设置打印字体(0:24x24中文 1:16x16中文)位1:保留位2:保留位3:1:字体加粗位4:1:双倍高度位5:1:双倍宽度位6:1:删除线位7:1:下划线ESC SO 设置打印字符双倍宽度格式:ASCII:ESC SO十进制:27 14十六进制:1B 0E说明:该命令之后所有字符均以正常宽度的2倍打印;该命令可以用回车或者DC4命令删除。

ESC DC4 字符宽度恢复正常格式:ASCII:ESC DC4十进制:27 20十六进制:1B 14说明:命令执行后,字符恢复正常宽度打印ESC { n 设置/取消字符上下倒置格式:ASCII:ESC { n十进制: 27 123 n 十六进制: 1B 7B n 说明:n=1:设置字符上下倒置 n=0:取消字符上下倒置 默认:0ESC B n 设置/取消字符反白打印格式:ASCII : ESC B n 十进制: 27 66 n 十六进制: 1B 42 n 说明:n=1:设置字符反白打印 n=0:取消字符反白打印 默认:0ESC % n 允许/禁止用户自定义字符格式:ASCII : ESC % n 十进制: 27 37 n 十六进制: 1B 25 n说明:n=1:选择用户自定义字符集; n=0:选择内部字符集(默认)ESC & s n m w 设置用户自定义字符 格式: ASCII : ESC & s n m w d1 d2 … dx十进制: 27 38 s n w m d1 d2 … dx 十六进制: 1B 26 s n w m d1 d2 … dx说明: 用于设置用户自定义字符。

s=2或3,32≤ n ≤ m < 127 s: 表示纵向字节数,=2(高度16点)或=3(高度24点) w: 字符宽度0~8(s=2),0~12(s=3) n: 表示自定义字符的起始ASCII m: 表示自定义字符的终止ASCII dx:字符点阵数据,x=s×w s=3 dx 格式如下:d1 d4 d7d2 d5 d8d3 d6 d9 d36s=2 d1 d3 d5d2 d4 d6 d16ESC ? n 取消用户自定义字符 格式: ASCII : ESC ? n十进制: 27 37 n 十六进制: 1B 25 ndx位7位6 位5位4 位3位2位1 位0说明:命令用于取消用户自定义的字符,字符取消后,使用系统的字符。

ESC R n 选择国际字符集格式:ASCII:ESC R n十进制:27 82 n十六进制:1B 52 n说明:选择国际字符集。

国际字符集设置如下:0:USA 5:Sweden 10:Denmark II1:France 6:Italy 11:Spain II2:Germany 7:Spain1 12:Latin America3:U.K. 8:Japan 13:Korea4:Denmark 1 9:NorwayESC t n 选择字符代码页格式:ASCII:ESC t n十进制:27 116 n十六进制:1B 74 n说明:选择字符代码页,字符代码页用于选择0x80~0xfe的打印字符。

字符代码页参数如下:0:437 4:860 8:8621:850 5:861 9:936(中文,缺省)2:852 6:8633:857 7:8582.4 图形打印命令ESC * m n1 n2 d1 d2…dk 设定点图命令格式:ASCII:ESC * m n1 n2 d1 d2 … dk十进制:27 42 m n1 n2 d1 d2 … dk十六进制:1B 2A m n1 n2 d1 d2 … dk说明:设定打印点图m =0,1,32,33。

n1=0-255n2=0-3dx=0-255k = n1+256*n2 (m=0,1)k = (n1+256*n2)*3 (m=32,33)m用于选择点图方式。

0:高度8点,水平方向需放大一倍1: 高度8点,水平方向不需放大31:高度24点,水平方向需放大一倍32:高度24点,水平方向不需放大点图顺序请参照自定义字符命令GS / n 打印下装点图格式:ASCII:GS / n十进制:29 47 n十六进制:1D 2F n说明:打印位图由GS *命令定义命令用于打印下装位图。

n=0~3、48~51。

n=0~3、48~51: 选择点图方式n 点图方式纵向点密度横向点密度0 正常方式203DPI 203DPI1 双倍宽度方式203DPI 101DPI2 双倍高度方式101DPI 203DPI3 倍高倍宽方式101DPI 101DPIn=65~68:打印预下装位图1~4POS-58可以预下装4个位图,每个位图大小为4K字节。

如果下装的位图大于4K,则会覆盖下一个位图。

GS * n1 n2 d1…dk 下装位图,用于位图打印格式:ASCII:GS * n1 n2 d1 … dk十进制:29 42 n1 n2 d1 … dk十六进制:1D 2A n1 n2 d1 … dk说明:该命令用于定义下装点图n1=1~48(宽度),n2=1~255(高度),n1×n2 < 1200, k=n1×n2×8下装位图一直有效,直到重新启动或重新定义。

位图顺序请参照自定义字符。

2.5 钱箱控制命令ESC p m n1 n2 钱箱控制格式:ASCII:ESC p m n1 n2十进制:27 112 m n1 n2十六进制:1B 70 m n1 n2说明:命令执行后,打印机将打开钱箱m:用于指定要控制的钱箱,PD02只支持一个钱箱n1,n2:钱箱控制参数,PD02不起作用2.6 按键控制命令ESC c 5 n 允许/禁止按键开关命令格式:ASCII:ESC c 5 n十进制:27 99 53 n十六进制:1B 63 35 n说明:n=1,禁止按键n=0,允许按键(默认)2.7 初始化命令ESC @ 打印机初始化格式:ASCII:ESC @十进制:27 64十六进制:1B 40说明:ESC @命令初始化打印机。

清除打印缓冲区恢复默认值选择字符打印方式删除用户自定义字符2.8 状态传输命令ESC v 向主机传送打印机状态格式:ASCII:ESC v十进制:27 118十六进制:1B 76说明:向主机传送一个字节打印机状态。

仅对串口型打印机有效。

发送字节位定义如下:位功能值12 无纸 13 机构故障 14 0 056 加热片温度过高 17允许/禁止状态自动上送GS a n格式:ASCII:GS a n十进制:29 97 n十六进制:1D 61 n说明:仅对串口型机型有效。

n定义如下:位功能值0 10 固定为012 禁止/允许状态自动上送禁止允许3-45 禁止/允许ERROR置BUSYRTS=BUSY禁止允许6-7当有效时,打印机发现状态改变,则自动发送状态到主机。

ESC u 向主机传送周边设备状态格式:ASCII:ESC u十进制:27 117十六进制:1B 75说明:向主机传送周边设备状态,仅对串口型打印机有效。

发送字节位定义位0: 钱箱开(0)/闭电平(1)位4: 恒为0本POS打印机不支持周边设备读取,将丢弃该命令。

相关文档
最新文档