using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.IO; using System.IO.Ports; using Cosmos.UserFrame; using Cosmos.Common; using Cosmos.CommonManager; namespace Cosmos.OLEDevice { public class DeviceLabelPrinter : ILabelPrinterUs { /// /// StateServer Object (StateServer 객체) /// public StateServer StateObject = (StateServer)StateServer.GetInstance(); /// /// Device 상태 정보 객체 /// public DeviceStatus devStatus = null; /// /// Pos 상태 정보 객체 /// public PosStatus m_cPosStatus = null; /// /// RS232통신을 위한 시리얼포트 객체 /// private SerialPort m_serialPort = null; private BackgroundWorker bw = null; public DeviceLabelPrinter() { try { m_cPosStatus = (PosStatus)StateObject.POS; devStatus = (DeviceStatus)StateObject.DEVICE; bw = new BackgroundWorker(); DeviceInit(); } catch (Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } } private void DeviceInit() { devStatus.LabelPrinter.Data = string.Empty; } #region OPEN/CLOSE 처리 함수 /// /// 디바이스 OPEN시 처리 함수 /// /// public bool OpenDevice(string sSerialPort, long lBaudrate) { bool bRet = false; try { if (m_serialPort != null) { //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start (주석처리) //m_serialPort.DiscardInBuffer(); //m_serialPort.DiscardOutBuffer(); //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end m_serialPort.Close(); System.Threading.Thread.Sleep(50); m_serialPort = null; } if (m_serialPort == null) { m_serialPort = new SerialPort(); //m_serialPort = new SerialPort(sSerialPort, (int)lBaudrate, Parity.None, 8, StopBits.One); if (m_serialPort == null) { devStatus.LabelPrinter.Open = false; devStatus.LabelPrinter.Status = "SERIAL PORT ERROR"; UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) devStatus.LabelPrinter.Status); return bRet; } m_serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); m_serialPort.PinChanged += new SerialPinChangedEventHandler(serialPort_PinChanged); m_serialPort.ErrorReceived += new SerialErrorReceivedEventHandler(serialPort_ErrorReceived); m_serialPort.NewLine = "\r\n"; m_serialPort.Encoding = System.Text.Encoding.GetEncoding("ks_c_5601-1987"); m_serialPort.WriteBufferSize = 1024 * 1024; m_serialPort.PortName = sSerialPort; m_serialPort.BaudRate = (int)lBaudrate; m_serialPort.DataBits = 8; m_serialPort.StopBits = StopBits.One; m_serialPort.Parity = Parity.None; m_serialPort.Handshake = Handshake.None; m_serialPort.ReadTimeout = 500; m_serialPort.WriteTimeout = 2000; m_serialPort.Open(); if (m_serialPort.IsOpen == false) { devStatus.LabelPrinter.Open = false; devStatus.LabelPrinter.Status = "SERIAL PORT OPEN ERROR"; UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) devStatus.LabelPrinter.Status); return bRet; } devStatus.LabelPrinter.Open = true; devStatus.LabelPrinter.Status = "SUCCESS"; bw.DoWork += OlePosHandler; bRet = true; } else { devStatus.LabelPrinter.Open = false; devStatus.LabelPrinter.Status = "SERIAL PORT ERROR"; UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) devStatus.LabelPrinter.Status); return bRet; } } catch (Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } return bRet; } /// /// 디바이스 CLOSE시 처리 함수 /// /// public bool CloseDevice() { bool bRet = false; try { if (m_serialPort == null) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) "Close fail(m_serialPort == null)"); return bRet; } //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start m_serialPort.DiscardInBuffer(); m_serialPort.DiscardOutBuffer(); //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end m_serialPort.Close(); bRet = true; m_serialPort = null; devStatus.LabelPrinter.Status = "CLOSE"; devStatus.LabelPrinter.Open = false; bw.DoWork -= OlePosHandler; } catch (Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } return bRet; } #endregion #region 이벤트 처리 private void OlePosHandler(object sender, DoWorkEventArgs e) { try { if (PosOLEDevice.m_delegateOlePos != null) PosOLEDevice.m_delegateOlePos(PosConst.OPOS_DEVICE.LABELPRINTER, devStatus.Scale.Data, string.Empty, string.Empty); else { UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) "이벤트 전송 대상 없음"); } } catch (Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } } private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { SerialPort serialPort = (SerialPort)sender; byte[] bytRecvBuf = new byte[serialPort.BytesToRead]; try { if (!bw.IsBusy) { serialPort.Read(bytRecvBuf, 0, bytRecvBuf.Length); bw.RunWorkerAsync(); } else { UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) "이벤트 처리 전 이벤트 발생 불가"); } } catch (Exception ex) { } } private void serialPort_PinChanged(object sender, SerialPinChangedEventArgs e) { throw new NotImplementedException(); } private void serialPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) { throw new NotImplementedException(); } #endregion #region 명령어 처리 함수 /// /// 일반 제품정보 라벨 프린터 출력 함수 /// /// 제품명(한글) ex)"파인트" /// 제품명(영문) ex)"Pint" /// 중량 ex)"340g" /// 점포명 ex)"양재사옥점" public void PrintProductWeightInfo(string sProductNm_Ko, string sProductNm_En, string sWeight, string sStoreNm) { string[] sCommands = null; try { if (m_cPosStatus.Base.OlePosLabelPrinterModel == PosConst.POS_DEVICE_LIST.LABEL_PRINTER.OLD_TYPE) { sCommands = new string[] { "CLL\r\n" // 메모리 클리어 , "NEW\r\n" // 새로운 명령어 시작 , "CLIP ON\r\n" , "NASC 949\r\n" // intermec 한글폰트 설정 , "FONT \"HYGothic-Medium\",12\r\n" , "PRPOS 0,150\r\n" //grayber@20180308 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sProductNm_Ko + " : " + sWeight + "\"\r\n" , "PRTXT \"" + sProductNm_Ko + " : " + string.Format( "{0:#,###}",CmUtil.IntParse(sWeight)) + "\"\r\n" //grayber@20180308 시리얼 포트 버퍼 초기화 end , "FONT \"HYGothic-Medium\",9\r\n" , "PRPOS 0,110\r\n" , "PRTXT \"" + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\"\r\n" , "PRPOS 0,70\r\n" //grayber@20180308 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sStoreNm + "\"\r\n" , "PRTXT \"" + sStoreNm +" 점" + "\"\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end , "PF\r\n" }; } else if(m_cPosStatus.Base.OlePosLabelPrinterModel == PosConst.POS_DEVICE_LIST.LABEL_PRINTER.NEW_TYPE) { sCommands = new string[] { "SETUP \"MEDIA,MEDIA SIZE,LENGTH,1200\"\r\n" // 용지출력범위 조정 , "CLL\r\n" // 메모리 클리어 , "NEW\r\n" // 새로운 명령어 시작 , "CLIP ON\r\n" , "NASC 949\r\n" // intermec 한글폰트 설정 , "FONT \"HYGothic-Medium\",9\r\n" , "PRPOS 110,290\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sStoreNm + "\"\r\n" , "PRTXT \"" + sStoreNm + " 점" + "\"\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end , "FONT \"HYGothic-Medium\",5\r\n" , "PRPOS 110,270\r\n" , "PRTXT \"" + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\"\r\n" , "FONT \"HYGothic-Medium\",7\r\n" , "PRPOS 70,50\r\n" , "PRTXT \"" + sProductNm_En + "\"\r\n" , "FONT \"HYGothic-Medium\",9\r\n" , "PRPOS 70,20\r\n" , "PRTXT \"" + sProductNm_Ko + "\"\r\n" , "FONT \"HYGothic-Medium\",14\r\n" , "PRPOS 170,20\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sWeight + "\"\r\n" , "PRTXT \"" + string.Format( "{0:#,###}",CmUtil.IntParse(sWeight)) + "\"\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end , "PF\r\n" }; } else { UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) "Check label printer model configuration."); return; } if(sCommands != null) WriteCommand(sCommands); } catch(Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } } /// /// 02O(해피오더) 제품정보 라벨 프린터 출력 함수 /// /// 제품명 ex)"패밀리" /// 주문번호 ex)"123456" /// 중량 ex)"990g" /// ex) "플레이버1/플레이버2/플레이버3" /// ex) "플레이버4/플레이버5/플레이버6" /// 점포명 ex)"양재사옥" /// 주문시간 ex)"2016-06-23 14:00:00" /// 포장시간 ex)"2016-06-23 14:20:00" public void PrintO2OProductInfo(string sProductNm, string sOrderNo, string sWeight, string sContent_1, string sContent_2, string sStoreNm, string sOrderYmdhms, string sPackingYmdhms) { string[] sCommands = null; try { if (m_cPosStatus.Base.OlePosLabelPrinterModel == PosConst.POS_DEVICE_LIST.LABEL_PRINTER.OLD_TYPE) { sCommands = new string[] { "CLL\r\n" , "NEW\r\n" , "CLIP ON\r\n" , "FONT \"HYGothic-Medium\",12\r\n" , "NASC 949\r\n" , "PRPOS 0,190\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sProductNm + " / " + sWeight + " (" + sOrderNo + ")\"\r\n" , "PRTXT \"" + sProductNm + " / " + string.Format( "{0:#,###}", CmUtil.IntParse( sWeight)) + " (" + sOrderNo + ")\"\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end , "FONT \"HYGothic-Medium\",9\r\n" , "PRPOS 0,150\r\n" , "PRTXT \"" + sContent_1 + "\"\r\n" , "PRPOS 0,110\r\n" , "PRTXT \"" + sContent_2 + "\"\r\n" , "PRPOS 0,50\r\n" , "PRTXT \"주문 " + sOrderYmdhms + "\"\r\n" , "PRPOS 0,10\r\n" , "PRTXT \"포장 " + sPackingYmdhms + "\"\r\n" , "PF\r\n" }; } else if (m_cPosStatus.Base.OlePosLabelPrinterModel == PosConst.POS_DEVICE_LIST.LABEL_PRINTER.NEW_TYPE) { sCommands = new string[] { "SETUP \"MEDIA,MEDIA SIZE,Length,1200\"\r\n" // 라벨프린터 용지범위 수정 , "CLL\r\n" // 메모리 클리어 , "NEW\r\n" // 새로운 명령어 시작 , "CLIP ON\r\n" , "NASC 949\r\n" // intermec 한글폰트 설정 , "FONT \"HYGothic-Medium\",9\r\n" , "PRPOS 110,290\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sStoreNm + "\"\r\n" , "PRTXT \"" + sStoreNm + " 점" + "\"\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end , "FONT \"HYGothic-Medium\",5\r\n" , "PRPOS 90,270\r\n" , "PRTXT \"주문 " + sOrderYmdhms + "\"\r\n" , "PRPOS 90,250\r\n" , "PRTXT \"포장 " + sPackingYmdhms + "\"\r\n" , "FONT \"HYGothic-Medium\",9\r\n" , "PRPOS 60,200\r\n" , "PRTXT \"" + sProductNm + "(" + sOrderNo + ")" + "\"\r\n" , "FONT \"HYGothic-Medium\",8\r\n" , "PRPOS 60,170\r\n" , "PRTXT \"" + sContent_1 + "\"\r\n" , "PRPOS 60,150\r\n" , "PRTXT \"" + sContent_2 + "\"\r\n" , "FONT \"HYGothic-Medium\",14\r\n" , "PRPOS 200,20\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 start //, "PRTXT \"" + sWeight + "\"\r\n" , "PRTXT \"" + string.Format("{0:#,###}", CmUtil.IntParse(sWeight)) + "\"\r\n" //grayber@20180308 라벨프린터 처리, 시리얼 포트 버퍼 초기화 end , "PF\r\n" }; } else { UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) "Check label printer model configuration."); return; } if (sCommands != null) WriteCommand(sCommands); } catch(Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } } /// /// 바코드 출력 라벨 프린터 출력 함수 /// /// 제품명(한글) ex)"파인트" /// 바코드 ex)"zxcgfdsgfhgsf" /// 점포명 ex)"양재사옥점" public void PrintProductBarCodeInfo(string sProductNm_Ko, string sBarcode_Data, string sStoreNm) { string[] sCommands = null; try { if (m_cPosStatus.Base.OlePosLabelPrinterModel == PosConst.POS_DEVICE_LIST.LABEL_PRINTER.OLD_TYPE) { sCommands = new string[] { "CLL\r\n" // 메모리 클리어 , "NEW\r\n" // 새로운 명령어 시작 , "CLIP ON\r\n" , "BF ON\r\n" , "PP 100,90\r\n" , "BM 1\r\n" , "BT \"CODE128\r\n" , "BF \"Swiss 721 BT\",7\r\n" , "PB \"" + sBarcode_Data + "\"\r\n" , "FONT \"HYGothic-Medium\",10\r\n" , "NASC 949\r\n" , "PP 20,40\r\n" , "PT \"" + sProductNm_Ko + "\"\r\n" , "PF\r\n" }; } else if (m_cPosStatus.Base.OlePosLabelPrinterModel == PosConst.POS_DEVICE_LIST.LABEL_PRINTER.NEW_TYPE) { sCommands = new string[] { "SETUP \"MEDIA,MEDIA SIZE,LENGTH,1200\"\r\n" // 용지출력범위 조정 , "CLL\r\n" // 메모리 클리어 , "NEW\r\n" // 새로운 명령어 시작 , "BF ON\r\n" , "PP 90,150\r\n" , "BH 50\r\n" , "BM 1\r\n" , "BT \"CODE128\r\n" , "BF \"Swiss 721 BT\",7\r\n" , "PB \"" + sBarcode_Data + "\"\r\n" , "NASC 949\r\n" , "FONT \"HYGothic-Medium\",9\r\n" , "PP 130,290\r\n" , "PT \"" + sStoreNm + "\" 점\r\n" , "FONT \"HYGothic-Medium\",5\r\n" , "PP 130,270\r\n" , "PT \"시간" + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\"\r\n" , "FONT \"HYGothic-Medium\",9\r\n" , "PP 90,20\r\n" , "PT \"" + sProductNm_Ko + "\"\r\n" , "PF\r\n" }; } else { UserLog.WriteLogFile(UserCom.LOG_IOS, UserCom.WARNING_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) "Check label printer model configuration."); return; } if (sCommands != null) WriteCommand(sCommands); } catch (Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } } /// /// Write commands to port. /// /// private void WriteCommand(string[] sCommands) { try { if(devStatus.LabelPrinter.Open) { foreach(string cmd in sCommands) { m_serialPort.Write(cmd); } } } catch(Exception ex) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, // Project Name (프로젝트명) System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + // Class Name (Class Name (클래스명)) System.Reflection.MethodBase.GetCurrentMethod().Name + "()", // Function Name (Function Name (함수명)) ex.Message); } } #endregion } }