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

上传人:b****2 文档编号:2455531 上传时间:2022-10-29 格式:DOCX 页数:87 大小:41.01KB
下载 相关 举报
C#使用ESC指令控制POS机打印小票.docx_第1页
第1页 / 共87页
C#使用ESC指令控制POS机打印小票.docx_第2页
第2页 / 共87页
C#使用ESC指令控制POS机打印小票.docx_第3页
第3页 / 共87页
C#使用ESC指令控制POS机打印小票.docx_第4页
第4页 / 共87页
C#使用ESC指令控制POS机打印小票.docx_第5页
第5页 / 共87页
点击查看更多>>
下载资源
资源描述

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

《C#使用ESC指令控制POS机打印小票.docx》由会员分享,可在线阅读,更多相关《C#使用ESC指令控制POS机打印小票.docx(87页珍藏版)》请在冰豆网上搜索。

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

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

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

1.前言

C#打印小票可以与普通打印机一样,调用PrintDocument实现。

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

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

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

本类需要调用ImageProcessor.cs

25035844@

2.POS机打印小票ReceiptHelper

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Runtime.InteropServices;

usingSystem.Threading;

usingSystem.Drawing;

usingSystem.Management;

usingSystem.IO;

usingLaisonTech.MediaLib;

usingLaisonTech.CommonBLL;

usingMicrosoft.Win32.SafeHandles;

namespaceLaisonTech.MediaLib

{

#region结构体定义

[StructLayout(LayoutKind.Sequential)]

publicstructOVERLAPPED

{

intInternal;

intInternalHigh;

intOffset;

intOffSetHigh;

inthEvent;

};

[StructLayout(LayoutKind.Sequential)]

publicstructPRINTER_DEFAULTS

{

publicintpDatatype;

publicintpDevMode;

publicintDesiredAccess;

}

///

///对齐方式

///

publicenumeTextAlignMode

{

Left=0,

Middle=1,

Right=2

}

#endregion

///

///小票打印类

///使用方法:

///1GetPrinterList获取已经安装的所有打印机列表.

///Open打开指定打印机

///2控制打印机动作、执行打印内容之前,必须先调用StartPrint,准备向打印机发送控制指令

///3调用SetLeft,SetBold,SetAlignMode,SetFontSize......设置打印参数

///4PrintText打印内容.注意:

打印该行内容后会自动换行(本类会在该行内容末尾添加一个换行符)

///PrintImageFile或PrintBitMap打印图片

///5控制指令和打印内容都发送完毕后,调用EndPrint执行真正打印动作

///6退出程序前调用Close

///

publicclassReceiptHelper

{

#region指令定义

privatestaticByte[]Const_Init=newbyte[]{0x1B,0x40,

0x20,0x20,0x20,0x0A,

0x1B,0x64,0x10};

//设置左边距

privateconststringConst_SetLeft="1D4C";

 

//设置粗体

privateconststringConst_SetBold="1B45";

privateconstStringConst_Bold_YES="01";

privateconstStringConst_Bold_NO="00";

//设置对齐方式

privateconststringConst_SetAlign="1B61";

privateconstStringConst_Align_Left="30";

privateconstStringConst_Align_Middle="31";

privateconstStringConst_Align_Right="32";

//设置字体大小,与SetBigFont不能同时使用

privateconststringConst_SetFontSize="1D21";

//设置是否大字体,等同于SetFontSize=2

//privateconstStringConst_SetBigFontBold="1B2138";

//privateconstStringConst_SetBigFontNotBold="1B2130";

//privateconstStringConst_SetCancelBigFont="1B2100";

///

///打印并走纸

///

privatestaticByte[]Const_Cmd_Print=newbyte[]{0x1B,0x4A,0x00};

//走纸

privateconststringConst_FeedForward="1B4A";

privateconststringConst_FeedBack="1B6A";

//切纸

privatestaticByte[]Const_SetCut=newbyte[]{0x1D,0x56,0x30};

//查询打印机状态

privatestaticByte[]Const_QueryID=newbyte[]{0x1D,0x67,0x61};

//回复帧以ID开头

privatestaticStringConst_ResponseQueryID="ID";

///

///设置图标的指令

///

privatestaticByte[]Const_SetImageCommand=newByte[]{0x1B,0x2A,0x21};

#endregion

#region常量定义

///

///最大字体大小

///

publicconstInt32Const_MaxFontSize=8;

///

///最大走纸距离

///

publicconstInt32Const_MaxFeedLength=5000;

///

///最大高宽

///

publicconstInt32Const_MaxImageLength=480;

///

///每次通信最多打印的行数

///

publicconstInt32Const_OncePrintRowCount=24;

publicconstInt32Const_BrightnessGate=100;

///

///无效句柄

///

publicconstInt32Const_InvalidHandle=-1;

#endregion

#region私有成员

///

///打印机句柄

///

privateintm_Handle=-1;

///

///是否已经初始化

///

privateBooleanm_Inited=false;

 

#endregion

#region私有函数

[DllImport("winspool.Drv",EntryPoint="OpenPrinterA",SetLastError=true,CharSet=CharSet.Auto,ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolOpenPrinter([MarshalAs(UnmanagedType.LPStr)]stringszPrinter,

outInt32hPrinter,IntPtrpd);

[DllImport("winspool.Drv",EntryPoint="StartDocPrinterA",SetLastError=true,CharSet=CharSet.Ansi,ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolStartDocPrinter(Int32hPrinter,Int32level,[In,MarshalAs(UnmanagedType.LPStruct)]DOCINFOAdi);

[DllImport("winspool.Drv",EntryPoint="EndDocPrinter",SetLastError=true,ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolEndDocPrinter(Int32hPrinter);

[DllImport("winspool.Drv",EntryPoint="StartPagePrinter",SetLastError=true,ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolStartPagePrinter(Int32hPrinter);

[DllImport("winspool.Drv",EntryPoint="EndPagePrinter",SetLastError=true,ExactSpelling=true,CallingConvention=CallingConvention.StdCall)]

publicstaticexternboolEndPagePrinter(Int32hPrinter);

[DllImport("winspool.Drv",EntryPoint="WritePrin

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

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

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