今天为大家介绍.Net下POS机小票打印功能的实现,在实际生产环境下,我们的门店使用的小票机大部分是LPT接口,但是也有一部分新的小票机采用的是USB接口的。我们在开发门店零售管理系统时,在小票打印模块,我们就提供了两种不同的打印方式,下面就介绍这两种小票打印方式 是如何实现的。
![aa55b13f425fc5bb93b548ed52306644.png](https://i-blog.csdnimg.cn/blog_migrate/9e463b1d2f18f17512bed0a67f6cdadf.jpeg)
POS小票并口打印
LPT并口打印
实现一个并口打印类 POSPrinter,我们需要调用WinAPI创建打印文件,.Net使用WinAPI,需要使用DllImport方式导入DLL即可,具体实现方式如下:
const int OPEN_EXISTING = 3;
string prnPort = "LPT1";
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);
public POSPrinter()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.prnPort = ConfigurationManager.AppSettings["PosTitck"].ToString(); //获取系统配置的小票机端口号
}
public POSPrinter(string prnPort)
{
this.prnPort = prnPort;//打印机端口
}
我们需要实现一个商品小票打印方法,来处理小票打印请求。
///
/// 打印小票
///
/// 商品明细
/// 小票流水号
/// 总金额
/// 总数量
/// 客户名称
/// 现金额
/// 会员卡号
/// 找零
/// 支付宝付款
/// 微信付款
///
public string PrintLine(DataTable dt, string posCode, decimal dSum, decimal dCount, string custName, string cashYe, string strVipCard, string cashCardNo, decimal dZl,
decimal aliYe, decimal weYe)
![b8970bf5193444dfef0f98970958e1ba.png](https://i-blog.csdnimg.cn/blog_migrate/07334b2f9c78ca6c546339c7aa038509.jpeg)
PrintLine
一般并口的小票机是一头是连接钱箱的,小票打印完成后,会自动弹出钱箱。需要下面的代码进行弹出操作
![b705d7e6111fd8298439e0a88773e447.png](https://i-blog.csdnimg.cn/blog_migrate/22a73d113ec6bff5fd6e05d141517ec5.jpeg)
OpenMoney
以上就是LPT并口打印类的代码,在自己项目里,只要稍作修改下即可使用。关于USB接口打印小票,我并未使用PrintPreviewDialog.Net自带的打印组件实现,而是采用了GridReport++实现的,下篇文章,会给大家介绍如何使用GridReport++实现小票打印。
关注「程序君」,分享实用的编程开发技巧,也欢迎大家留言,一起学习交流分享,另外码字不易,请点赞支持。十分感谢!
如果有朋友需要本文的源码,请单独私信 程序君 留下本人邮箱,我会尽快通过邮件发送给你。