• C# POS机客显


    最近做winform收银机,设计顾显及打印小票总结。

    1、顾显(串口COM1)

    只涉及到总计,所以只是简单的功能。

    public static ClientDisplayResult Display(decimal total, bool isClear=false)
            {
                var result = new ClientDisplayResult();
                string[] ports = SerialPort.GetPortNames();
                if (ports.Length == 0)
                {
                    result.Result = false;
                    result.Message = "未检测到串行通信接口";
                    Logger.Error(result.Message);
                    return result;
                }
    
                SerialPort serialPort = new SerialPort();
                try
                {
                    // 串行端口 Windows:COM1  
                    serialPort.PortName = "COM1";
                    //串口波特率 默认:2400
                    serialPort.BaudRate = 2400;
                    //停止位 默认
                    serialPort.StopBits = StopBits.One;
                    //数据位 默认
                    serialPort.DataBits = 8;
    
                    if (!serialPort.IsOpen)
                    {
                        serialPort.Open();
                    }
                    //清空接收缓冲区数据
                    serialPort.DiscardInBuffer();
    
                    if (isClear)
                    {
                        //清屏\f
                        serialPort.WriteLine(((char)12).ToString());
                    }
                    else
                    {
                        //状态,合计提示灯亮(ESCs2)
                        var text = ((char)27).ToString() + ((char)115).ToString() + ((char)50).ToString();
                        serialPort.Write(text);
                        //显示收款金额(EscQA金额CR)
                        text = ((char)27).ToString() + ((char)81).ToString() + ((char)65).ToString() + total.ToString() + ((char)13).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();
                }
            }

    =====下面是客显功能较全===============

        /// <summary>
        /// 客显
        /// </summary>
        public class CustomerDisplay
        {
            #region 成员变量
     
            private string spPortName;
            private int spBaudRate;
            private StopBits spStopBits;
            private int spDataBits;
     
            #endregion --成员变量
     
            #region 属性
            /// <summary>
            /// 客显发送类型
            /// </summary>
            public CustomerDispiayType DispiayType { get; set; }
            #endregion --属性
     
            #region 构造函数
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="_spPortName">端口名称(COM1,COM2,COM3...)</param>
            /// <param name="_spBaudRate">通信波特率(2400,9600....)</param>
            /// <param name="_spStopBits">停止位</param>
            /// <param name="_spDataBits">数据位</param>
            public CustomerDisplay(string _spPortName, int _spBaudRate, string _spStopBits, int _spDataBits)
            {
                this.spBaudRate = _spBaudRate;
                this.spDataBits = _spDataBits;
                this.spPortName = _spPortName;       
                this.spStopBits = (StopBits)(Enum.Parse(typeof(StopBits), _spStopBits));
            }
            #endregion --构造函数
     
            #region Method
            #region 公共方法
     
            /// <summary>
            /// 数据信息展现
            /// </summary>
            /// <param name="data">发送的数据(清屏可以为null或者空)</param>
            public void DisplayData(string data)
            {
                SerialPort serialPort = new SerialPort();
                serialPort.PortName = spPortName;
                serialPort.BaudRate = spBaudRate;
                serialPort.StopBits = spStopBits;
                serialPort.DataBits = spDataBits;
                serialPort.Open();
     
                //先清屏
                serialPort.WriteLine(((char)12).ToString());
                //指示灯
                string str = ((char)27).ToString() + ((char)115).ToString()+((char)(int)this.DispiayType).ToString();   
                serialPort.WriteLine(str);
     
                //发送数据
                if (!string.IsNullOrEmpty(data))
                {
                    serialPort.WriteLine(((char)27).ToString() + ((char)81).ToString() + ((char)65).ToString() + data + ((char)13).ToString());
                }
     
                serialPort.Close();
            }
     
            #endregion --公共方法
            #endregion --Method
        }
     
        /// <summary>
        /// 客显类型
        /// </summary>
        public enum CustomerDispiayType
        {
            /// <summary>
            /// 清屏
            /// </summary>
            Clear,
            /// <summary>
            /// 单价
            /// </summary>
            Price,
            /// <summary>
            /// 合计
            /// </summary>
            Total,
            /// <summary>
            /// 收款
            /// </summary>
            Recive,
            /// <summary>
            /// 找零
            /// </summary>
            Change
        }       

    调用案例

     class Program
        {
            static void Main(string[] args)
            {
               
                while (true)
                {
                    Console.Write("清屏");
                    CustomerDisplay disliay = new CustomerDisplay("COM2", 9600, StopBits.One, 8);
     
                    disliay.DispiayType = CustomerDispiayType.Clear;
                    disliay.DisplayData(null);              
                    System.Threading.Thread.Sleep(1000);
     
                    Console.Write("找零");
                 
                    disliay.DispiayType = CustomerDispiayType.Change;
                    disliay.DisplayData("230.34");
                    System.Threading.Thread.Sleep(1000);
     
                    Console.Write("收款");
                    disliay.DispiayType = CustomerDispiayType.Recive;
                    disliay.DisplayData("10.34");
                    
                    System.Threading.Thread.Sleep(1000);
     
                    Console.Write("总计");
                    disliay.DispiayType = CustomerDispiayType.Total;
                    disliay.DisplayData("99230.34");
              
                    System.Threading.Thread.Sleep(1000);
     
                    Console.Write("单价");
                    disliay.DispiayType = CustomerDispiayType.Price;
                    disliay.DisplayData("18.34");
                    
                    System.Threading.Thread.Sleep(2000);
     
                }
     
     
                Console.Read();
     
            }
    通常情况下指示灯排列号:
     /*
              *当 n=0 , 四种字符 全暗。
              *当 n=1, “单价”字符 亮,“多谢惠顾”亮,其它三种暗。
              *当 n=2, “合计”字符 亮,“多谢惠顾”亮,其它三种暗。
              *当 n=3, “收款”字符 亮,“多谢惠顾”亮,其它三种暗。
              *当 n=4, “找零”字符 亮,“多谢惠顾”闪烁,其它三种暗
          */

    转 : https://www.cnblogs.com/flywing/p/8426690.html

    https://blog.csdn.net/wybztn/article/details/7240117

    转 : https://wenku.baidu.com/view/b64e5e46021ca300a6c30c22590102020740f2fa.html

  • 相关阅读:
    WPF基础篇之控件模板(ControlTemplate)
    WPF基础篇之移动特效
    WPF基础篇之空间布局
    00-API-Mongoose
    00-API-Vue
    博客园皮肤设置
    15-Node
    16-Vue-A
    15-MongoDB
    14-电商项目
  • 原文地址:https://www.cnblogs.com/fps2tao/p/16305637.html
Copyright © 2020-2023  润新知