ImageVerifierCode 换一换
格式:DOCX , 页数:87 ,大小:40.75KB ,
资源ID:5335839      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5335839.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C使用ESC指令控制POS机打印小票.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

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

1、C使用ESC指令控制POS机打印小票C#使用ESC指令控制POS打印机打印小票 1.前言C#打印小票可以与普通打印机一样,调用PrintDocument实现。也可以发送标注你的ESC指令实现。由于 调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos打印机,并且无法发送控制指令实现pos打印机的切纸、走纸等动作。因此个人建议使用ESC指令进行打印会更通用。本类需要调用 ImageProcessor.cs 2.POS机打印小票ReceiptHelperusing System;using ;using System.Text;using ;using System.

2、Threading;using System.Drawing;using System.Management;using System.IO;using LaisonTech.MediaLib;using LaisonTech.CommonBLL;using ;namespace LaisonTech.MediaLib#region 结构体定义 StructLayout(LayoutKind.Sequential) public struct OVERLAPPED int Internal; int InternalHigh; int Offset; int OffSetHigh; int h

3、Event; ; StructLayout(LayoutKind.Sequential) public struct PRINTER_DEFAULTS public int pDatatype; public int pDevMode; public int DesiredAccess; / / 对齐方式 / public enum eTextAlignMode Left = 0, Middle = 1, Right = 2 #endregion / / 小票打印类/ 使用方法:/ 1 GetPrinterList获取已经安装的所有打印机列表./ Open 打开指定打印机/ 2 控制打印机动作

4、、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令/ 3 调用SetLeft, SetBold, SetAlignMode, SetFontSize . .设置打印参数/ 4 PrintText 打印内容.注意:打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)/ PrintImageFile 或 PrintBitMap打印图片/ 5 控制指令和打印内容都发送完毕后,调用 EndPrint执行真正打印动作 / 6 退出程序前调用Close / public class ReceiptHelper #region 指令定义 private static By

5、te 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_S

6、etAlign = 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_SetBigFontBol

7、d = 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

8、 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 ;#endregio

9、n #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

10、; / / 无效句柄 / 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, ExactSpelli

11、ng = 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, C

12、allingConvention = 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 = CallingConventio

13、n.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, Ent

14、ryPoint = 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 = CallingConve

15、ntion.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(

16、Int32 hPrinter); / / 发送指令 / / / private Boolean SendCommand(Byte cmd) if (m_Handle = Const_InvalidHandle | cmd = null | cmd.Length = cmd.Length); / / 发送文本格式的指令 / / / private Boolean SendCommand(String hexstrcmd) if (m_Handle = Const_InvalidHandle | hexstrcmd = null | hexstrcmd.Length 4) return false

17、; byte mybyte = null; Boolean bl = DataFormatProcessor.HexStringToBytes(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 datah

18、eight) 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)(ima

19、ge.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 datah

20、eight); 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 =

21、 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

22、 打印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整数倍,设置为

23、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 B

24、ytebytelen; Int32 byteidxInCol = 0;/当前列里首个像素,在目标字节数组里的下标 Int32 byteidx = 0;/当前像素在目标数组里的字节下标 Int32 bitidx = 0;/当前像素在目标数组里当前字节里的比特位下标 Int32 pixidxInCol = 0;/当前像素在当前列里的第几个位置 Int32 pixidx = 0;/当前像素在原始图片里的下标 Int32 rowidx = 0; /当前 处理的像素点所在行,不能超过 图像高度 Int32 curprocrows = 0;/当前需要处理的行数量 while (rowidx height)

25、 /按照纵向次序,把当前列的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;

26、 y rowidx + curprocrows; +y) /原始图片里像素位置 pixidx = y * img.Width + colidx; /获取当前像素的亮度值.如果当前像素是黑点,需要把数组里的对应比特位设置为1 if (briarypixidx) bitidx = 7 - pixidxInCol % 8;/最高比特位对应首个像素.最低比特位对应末个像素 byteidx = byteidxInCol + pixidxInCol / 8; /由于最后一段可能不足24行,因此不能使用byteidx+ DataFormatProcessor.SetBitValue(bitidx, true, ref allbitarybyteidx); pixidxInCol+; byteidxInCol += 3;/每列固定24个像素,3个字节 rowidx += Const_OncePrintRowCount; return true; #endregion #region 公开函数 private static ReceiptHelper m_instance = new ReceiptHelper(); /

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1