예제 #1
0
 public PrinterController(string pConfigPath)
 {
     errList    = new List <string>();
     ConfigPath = pConfigPath;
     // _log = LogHelper.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "ClientConfig\\Log4Net.config", "PRINTER_LOG");
     CreateInstance();
     if (_curPrinterList == null || _curPrinterList.Count <= 0)
     {
         PrinterDevLogger.Error("打印机对象创建失败。", "");
     }
 }
예제 #2
0
        /// <summary>
        /// 创建实例
        /// </summary>
        /// <returns></returns>
        private void CreateInstance()
        {
            _curPrinterList = new List <IPrint>();
            IPrint io = null;

            try
            {
                string       tBaseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
                ConfigReader cfgReader      = new ConfigReader(this.ConfigPath);
                _curConfigList = ConfigReader.ReadPrinterConfig();
                PrinterDevLogger.Debug("读取打印机配置.ConfigPath=" + ConfigPath, "");

                string msg = string.Empty;
                foreach (PrinterConfig cfg in _curConfigList)
                {
                    string   driverPath = Path.Combine(tBaseDirectory, cfg.Driver);
                    Assembly assembly   = Assembly.LoadFile(driverPath);
                    PrinterDevLogger.Debug("加载组件.driverPath=" + driverPath, "");

                    string name = assembly.FullName.Split(',')[0] + ".PrinterControl";
                    Type   type = assembly.GetType(name);
                    io = Activator.CreateInstance(type, cfg) as IPrint;
                    io.OnShowErrMsg += ShowErrorMsg;
                    _curPrinterList.Add(io);
                    //if(io.CheckPrinterState(out msg))
                    //{
                    //    _log.Info("打印机对象创建成功");
                    //    break;
                    //}
                }
            }
            catch (Exception ex)
            {
                ShowErrorMsg(ErrorType.Error, "创建打印机对象失败,请检查打印插件相关。");
                PrinterDevLogger.Error("打印机对象创建失败,请检查打印插件相关。", "", ex);
            }
        }
예제 #3
0
        /// <summary>
        /// 检测打印机
        /// </summary>
        /// <param name="pErrorMsg"></param>
        public bool CheckPrinterState(out string pMsg)
        {
            bool tCanPrint = true;

            try
            {
                _isComportReceiveData = false;
                pMsg = string.Empty;//错误信息
                if (_spCom.IsOpen)
                {
                    _spCom.Close();
                    Thread.Sleep(100);
                }
                _spCom.Open();
                Thread.Sleep(100);
                if (_spCom.IsOpen)
                {
                    #region COM口打开
                    WriteComData(PrinterCommand.CLEAR_PRINTER_BUFFER);
                    WriteComData(PrinterCommand.INIT_PRINTER);
                    WriteComData(PrinterCommand.JoinCommand(PrinterCommand.DETECTION_PRINTER, (Byte)20));

                    int count = 0;
                    while (!_isComportReceiveData)
                    {
                        count++;
                        if (count >= 100)
                        {
                            break;
                        }
                        Thread.Sleep(20);
                    }
                    //超时
                    if (count < 100)
                    {
                        byte[] tempArray = new byte[] { _curComBufferData[0], _curComBufferData[1], _curComBufferData[2], _curComBufferData[3], _curComBufferData[4], _curComBufferData[5] };

                        PrinterDevLogger.Debug("打印机状态:" + ByteArrayToHexString(tempArray), _curPrinterConfig.ComPort);
                        #region check printer state
                        #region paper status
                        byte tPaperStatus = _curComBufferData[2];
                        //if (tPaperStatus != 0)
                        //{
                        if ((tPaperStatus & 0X01) == 1)
                        {
                            pMsg     += "没有打印纸,缺纸。";
                            tCanPrint = false;
                        }
                        if ((tPaperStatus & 0X04) == 4)
                        {
                            pMsg += "即将缺纸。";
                        }
                        if ((tPaperStatus & 0X20) == 32)
                        {
                            pMsg += "弹纸盒里面有纸,未弹出。";
                            WriteComData(PrinterCommand.PRINTER_EJECTOR_TICKET);    //出票 0X1D, 0X65, 0X05
                            //tCanPrint = false;
                        }
                        //if (!string.IsNullOrEmpty(pMsg))
                        //{
                        //    pMsg = "未知错误";
                        //    tCanPrint = false;
                        //}
                        // }

                        #endregion
                        #region user status
                        byte tUserStatus = _curComBufferData[3];
                        //if (tUserStatus != 0)
                        //{
                        if ((tUserStatus & 0X01) == 1 || (tUserStatus & 0X02) == 2)
                        {
                            pMsg     += "打印机盖没有合上。";
                            tCanPrint = false;
                        }
                        //if (string.IsNullOrEmpty(pMsg))
                        //{
                        //    pMsg = "未知错误";
                        //    tCanPrint = false;
                        //}
                        // }
                        //switch (tUserStatus)
                        //{
                        //    case 1://打印机盖没有合上
                        //    case 2:
                        //        pMsg += GetErrorMsg(5);
                        //        tCanPrint = false;
                        //        break;
                        //    default:
                        //        break;
                        //}
                        #endregion
                        #region  5= Unrecoverable error status
                        byte tOtherStatus = _curComBufferData[5];
                        //if (tOtherStatus != 0)
                        //{
                        if ((tUserStatus & 0X01) == 1)
                        {
                            pMsg     += "切纸错误。";
                            tCanPrint = false;
                        }
                        //    if (string.IsNullOrEmpty(pMsg))
                        //    {
                        //        pMsg = "打印机出现严重错误。";
                        //        tCanPrint = false;
                        //    }
                        //}

                        //switch (tOtherStatus)
                        //{
                        //    case 0:
                        //        break;
                        //    case 1://切刀卡纸错误
                        //        pMsg += GetErrorMsg(6);
                        //        tCanPrint = false;
                        //        break;
                        //    default:
                        //        //tCanPrint = false;
                        //        //pMsg += GetErrorMsg(8);
                        //        break;
                        //}
                        #endregion
                        #endregion
                    }
                    else
                    {
                        tCanPrint = false;
                        pMsg     += "无法获取打印机状态,超时。";
                        PrinterDevLogger.Error("无法获取打印机状态,超时。", _curPrinterConfig.ComPort);
                    }
                    _spCom.Close();
                    Thread.Sleep(100);
                    #endregion
                }
                else
                {
                    tCanPrint = false;
                    pMsg     += "打印机串口无法打开。";
                    PrinterDevLogger.Error("检测打印机状态时,串口无法打开。", _curPrinterConfig.ComPort);
                }
            }
            catch (Exception ex)
            {
                pMsg      = ex.Message;
                tCanPrint = false;
                PrinterDevLogger.Error("检测打印机状态异常。", _curPrinterConfig.ComPort, ex);
            }
            return(tCanPrint);
        }