C# 收银机顾显(客显)及打印小票(58热敏打印机)
最近做winform收银机,设计顾显及打印小票总结。
1、顾显(串口COM1)
只涉及到总计,所以只是简单的功能。
public static ClientDisplayResult Display(decimal total, bool isClear=false)
{
var result = new ClientDisplayResult();
string[] ports = SerialPort.GetPortNames();
if (ports.Length == )
{
result.Result = false;
result.Message = "未检测到串行通信接口";
Logger.Error(result.Message);
return result;
} SerialPort serialPort = new SerialPort();
try
{
// 串行端口 Windows:COM1
serialPort.PortName = "COM1";
//串口波特率 默认:2400
serialPort.BaudRate = ;
//停止位 默认
serialPort.StopBits = StopBits.One;
//数据位 默认
serialPort.DataBits = ; if (!serialPort.IsOpen)
{
serialPort.Open();
}
//清空接收缓冲区数据
serialPort.DiscardInBuffer(); if (isClear)
{
//清屏\f
serialPort.WriteLine(((char)).ToString());
}
else
{
//状态,合计提示灯亮(ESCs2)
var text = ((char)).ToString() + ((char)).ToString() + ((char)).ToString();
serialPort.Write(text);
//显示收款金额(EscQA金额CR)
text = ((char)).ToString() + ((char)).ToString() + ((char)).ToString() + total.ToString() + ((char)).ToString();
serialPort.Write(text);
}
result.Result = true;
return result;
}
catch (Exception ex)
{
Logger.Error("顾显异常",ex);
result.Result = false;
result.Message = ex.Message;
return result;
}
finally
{
serialPort.Close();
serialPort.Dispose();
}
}
2、打印小票(并口打印)及打开钱箱(OpenDrawer方法)
通过Windows API接口
public class LPTControl
{
#region API函数 [StructLayout(LayoutKind.Sequential)]
private struct OVERLAPPED
{
int Internal;
int InternalHigh;
int Offset;
int OffSetHigh;
int hEvent;
}
[DllImport("kernel32.dll")]
private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile); [DllImport("kernel32.dll")]
private static extern bool WriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, out int lpNumberOfBytesWritten, out OVERLAPPED lpOverlapped); [DllImport("kernel32.dll")]
private static extern bool CloseHandle(int hObject); #endregion public enum PrintPosition { Left, Center, Right } private int iHandle; private const int ColWidth = ; private bool Write(string data)
{
if (iHandle != -)
{
int i;
OVERLAPPED x;
byte[] byteData = System.Text.Encoding.Default.GetBytes(data);
return WriteFile(iHandle, byteData, byteData.Length, out i, out x);
}
else
{
return false;
}
}
private bool Write(byte[] data)
{
if (iHandle != - && data.Length > )
{
int i;
OVERLAPPED x;
return WriteFile(iHandle, data, data.Length, out i, out x);
}
else
{
return false;
}
} public bool OpenPort()
{
iHandle = CreateFile("LPT1", 0x40000000, , , , , );
if (iHandle != -)
{
return true;
}
else
{
return false;
}
}
public bool Close()
{
return CloseHandle(iHandle);
} public bool WriteLine(string data, PrintPosition position = PrintPosition.Left)
{
int length = Encoding.Default.GetBytes(data).Length; if (length < ColWidth)
{
if (position == PrintPosition.Right)
{
data = data.PadLeft(ColWidth - (length - data.Length), ' ');
}
else if (position == PrintPosition.Center)
{
data = data.PadLeft(length + (ColWidth - length) / - (length - data.Length), ' ');
}
} bool result = Write(data);
if (result)
{
result = NewRow();
}
return result;
}
private bool NewRow()
{
bool result = false;
var byteData = new byte[] { , , };
return result = Write(byteData);
} public bool PrintEmptyRow(int rowCount = )
{
bool result = false;
string data = string.Empty.PadLeft(ColWidth, ' '); for (int i = ; i < rowCount; i++)
{
result = WriteLine(data);
if (!result) break;
}
return result;
} public bool PrintLine(bool isDoubleLine = false)
{
if (isDoubleLine)
{
return WriteLine("================================");
}
else
{
return WriteLine("--------------------------------");
}
} public bool OpenDrawer()
{
bool result = Write(((char)).ToString() + "p" + ((char)).ToString() + ((char)).ToString() + ((char)).ToString());
return result;
} }
调用:
public static void Print(OrderInfo orderInfo)
{
try
{
if (orderInfo != null)
{
var orderDetailList = GetOrderDetail(orderInfo.OrderId);
if (orderDetailList.IsNullOrEmpty())
{
Logger.Error("订单详情获取异常:OrderId" + orderInfo.OrderId);
return;
}
LPTControl control = new LPTControl();
var result = control.OpenPort();
if (!result)
{
Logger.Error("LPT1端口打开失败");
return;
}
control.WriteLine(orderInfo.SupermarketName, LPTControl.PrintPosition.Center);
control.PrintEmptyRow();
control.WriteLine($"订单号:{orderInfo.OrderId}");
control.WriteLine($"时间:{orderInfo.PayTime}");
control.WriteLine($"收银员:{orderInfo.AccountName}");
control.PrintLine(true);
int i = ;
foreach (var item in orderDetailList)
{
control.WriteLine($"{i}、{item.GoodsName}"); var price = (decimal)item.Price;
var num = $"{item.GoodsCount}*{price.ToString("#0.00")}".PadLeft(, ' ');
var total = (price * item.GoodsCount).ToString("#0.00").PadLeft(, ' '); var text = $" {item.SKUID}{num}{total}";
control.WriteLine(text);
i++;
}
control.PrintLine();
if (orderInfo.OrderType == OrderType.Dispatching)
{
control.WriteLine($"配送费:{orderInfo.DeliveryAmount.ToString("#0.00")}元");
} if (orderInfo.UsedScoreAmount > )
{
control.WriteLine($"积分抵扣:{ orderInfo.UsedScoreAmount}元");
}
var payType = orderInfo.PayWay.GetDescription();
if (orderInfo.OrderBusinessType == OrderBuyType.ToShop)
{
payType = OrderPayWay.CashPay.GetDescription();
}
control.WriteLine($"支付方式:{payType}");
control.WriteLine($"合计:{orderInfo.Total.ToString("#0.00")}元");
control.WriteLine($"应付:{ orderInfo.PayedAmount.ToString("#0.00")}元");
if (orderInfo.UserTotal > )
{
control.WriteLine($"赠送积分:{ orderInfo.UserTotal}");
}
if (orderInfo.OrderType == OrderType.Dispatching)
{
control.PrintLine(true);
control.WriteLine($"联系人:{orderInfo.TrueName}");
control.WriteLine($"手机号:{ orderInfo.DisplayMobile}");
control.WriteLine($"地址:{orderInfo.Address}");
control.WriteLine($"配送时间:{orderInfo.ExpectDispatchTimeText}");
}
control.PrintLine();
control.WriteLine("谢谢惠顾,欢迎下次光临!");
control.PrintEmptyRow();
control.OpenDrawer();
control.Close();
System.Threading.Thread.Sleep();
}
else
{
Logger.Error("订单异常");
}
}
catch (Exception ex)
{
Logger.Error("打印小票异常", ex);
}
}
C# 收银机顾显(客显)及打印小票(58热敏打印机)的更多相关文章
- 【转】C#使用ESC指令控制POS打印机打印小票
.前言 C#打印小票可以与普通打印机一样,调用PrintDocument实现.也可以发送标注你的ESC指令实现.由于 调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的pos ...
- C#使用ESC指令控制POS打印机打印小票
1.前言 C#打印小票可以与普通打印机一样,调用PrintDocument实现.也可以发送标注你的ESC指令实现.由于 调用PrintDocument类时,无法操作使用串口或TCP/IP接口连接的po ...
- ActiveXObject Word.Application 打印小票
javascript 时间格式 Date.prototype.format = function (format) { var o = { "M+": this.getMonth( ...
- C# 打印小票 POS
C# 打印小票 POS 最近在写一个餐饮的收银系统,以前从来没有碰过打印机这玩意.感觉有些无从下手,在前面做报表时,总想找第三方的控件来用用,结果始终不行没搞定.没研究透,催得急没办法还是的动手自己写 ...
- 按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式
按照已有的模板打印小票<二> ——调用windows打印机打印 可设置字体样式 之前写过一篇文章<按照已有的模板输出一(如发票)>,是关于如何给已有的模板赋值.在项目的实践过程 ...
- C# 网络打印机ESC指令打印小票
public void SendSocketMsg(String ip, int port, int times, byte[] data) { try { byte[] mData; ) { mDa ...
- android端StarIO热敏打印机打印小票
最近在做这个热敏打印机打印小票,开始的时候在网上找资料,发现国内基本没有这方面的资料,国外也很少,在此做个打印小票的记录. 这里只记录一些关键点. 使用StarIOPort.searchPrinter ...
- LED客显的类
using System; using System.IO.Ports; namespace Common { public class LedHelper { ; private static st ...
- 关于web页面JApplet打印小票
版权所有 做这个的例子太少,我把我做的示例亮出来 一.先说说需要的版本 1.我用的浏览器只有ie: 火狐只支持52版本以下,并且是java7.java8.chrome不支持 2.applet客户端打印 ...
随机推荐
- windows下使用 ApiGen 生成php项目的开发文档
之前使用 PHPDocument 生成过开发文档,但是界面看着不爽,遂尝试了 ApiGen 生成,不得不说界面看着舒服多了,下面说说安装和使用的方法. ApiGen官网: http://www.api ...
- ffmpeg默认输出中文为 UTF-8
在使用ffmpeg 进行对音视频文件解码输出信息的时候会出现乱码. 从网上找到了说ffmpeg默认格式 为 utf-8 如果vs工程使用的的 Unicode 则需要将 utf-8转 Unicode 才 ...
- 迷你MVVM框架 avalonjs 1.2发布
avalon1.2 带来了许多新特性,让开发更轻松!详见如下: 升级路由系统与分页组件. 对ms-duplex的绑定值进行增强,以前只能prop或prop.prop2,现在可以prop["x ...
- Linux安装centos7
安装 选择安装centos7,按回车 进入到安装界面: 选择我要自定义分区,然后点击左上角done: 然后自定义分区(swap分区一般为内存的2倍,我这里用的虚拟机截的图,所以内存给的少,具体按照自己 ...
- iOS对HTTPS证书链的验证原理
今天看到所在的某个开发群问https原理,之前做HTTPS ,下面简单说下原理.希望能帮助你理解. HTTPS从最终的数据解析的角度,与HTTP相同.HTTPS将HTTP协议数据包放到SSL/TSL层 ...
- Vue.js基础知识
<!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml&q ...
- Windows环境和Linux环境下Redis主从复制配置
Windows环境下和Linux环境下配置Redis主从复制基本上一样,都是更改配置文件.Windows环境下修改的配置文件是:redis.windows.conf.redis.windows-ser ...
- sqlserver 2017 linux还原windows备份时的路径问题解决
windows的备份由于路径问题,在Linux上会报错 File 'YourDB_Product' cannot be restored to 'Z:\Microsoft SQL Server\MSS ...
- 避免Block中的强引用环
[避免Block中的强引用环] In manual reference counting mode, __block id x; has the effect of not retaining x. ...
- C# Redis Server分布式缓存编程(二)(转)
出处;http://www.cnblogs.com/davidgu/p/3263485.html 在Redis编程中, 实体和集合类型则更加有趣和实用 namespace Zeus.Cache.Red ...