using Estsh.Client.Base; using System.Data; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; namespace Estsh.Client.StepLibrary { public class GYPrintQA : StepBase { //计数器数量 private string _PrdCount = string.Empty; private static string BarTenderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Bartend\bartend.exe"); private static string LabelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\LabelFile"); private static object _lock_print_object = new object(); private const char TAB = '\t'; private const string NEW_LINE = "\r\n"; private List dtLabel = null; //private static CodesoftPrinter m_csPrinter = null; public static bool PrintSerialNumber(List snData, string templateFile, string txtDataFile, List CustPartNoList) { string LabelTemplatePath = Path.Combine(LabelPath, templateFile); string LabelTextPath = Path.Combine(LabelPath, "GYQA" + ".txt"); string LabelFilePath = Path.Combine(LabelPath, "GYQA" + ".BTW"); if (!CommonListData.CheckDataValid(snData)) return false; StringBuilder PrintContent = new StringBuilder(); foreach (DataRow dr in snData) { PrintContent.Append(dr["f10"].ToString()); PrintContent.Append(TAB); //1-serial_number PrintContent.Append(dr["part_no"].ToString()); PrintContent.Append(TAB); //2-part_no PrintContent.Append(dr["f4"].ToString()); PrintContent.Append(TAB); //3-part_spec PrintContent.Append(dr["vendor_part_no"].ToString()); PrintContent.Append(TAB); //4-座椅 PrintContent.Append(dr["mfger_part_no"].ToString()); PrintContent.Append(TAB); //5-头枕 PrintContent.Append(dr["f6"].ToString()); PrintContent.Append(TAB); //6-enum_desc PrintContent.Append(dr["ymd"].ToString()); PrintContent.Append(TAB); //7-ymd lock (_lock_print_object) { WritePrintLog(PrintContent.ToString()); BarTenderPrinter.WriteFile(LabelTextPath, PrintContent.ToString(), Encoding.UTF8, false); BarTenderPrinter.Print(BarTenderPath, LabelFilePath); //PrintCodeSoftSerialNumber(dr, LabelFilePath, m_csPrinter, CustPartNoList); } return true; } return true; } public static bool PrintCodeSoftSerialNumber(DataRow snData, string LabelTextPath, CodesoftPrinter csPrinter, List CustPartNoList) { Dictionary printData = new Dictionary(); printData.Add("car_no", snData["car_no"].ToString()); printData.Add("create_time", snData["create_time"].ToString()); printData.Add("csn", snData["csn"].ToString()); printData.Add("cust_part_no1", CustPartNoList[0]["cust_part_no"].ToString()); printData.Add("cust_part_no2", CustPartNoList[1]["cust_part_no"].ToString()); printData.Add("cust_part_no3", CustPartNoList[2]["cust_part_no"].ToString()); printData.Add("enum_desc", snData["enum_desc"].ToString()); printData.Add("label_type", snData["label_type"].ToString()); printData.Add("model_alias", snData["model_alias"].ToString()); printData.Add("model_code", snData["model_code"].ToString()); printData.Add("model_name", snData["model_name"].ToString()); printData.Add("number_T", snData["number_T"].ToString()); printData.Add("part_class", snData["part_class"].ToString()); printData.Add("part_no", snData["part_no"].ToString()); printData.Add("part_no_3c", snData["part_no_3c"].ToString()); printData.Add("part_spec", snData["part_spec"].ToString()); printData.Add("part_T", snData["part_T"].ToString()); printData.Add("prod_type", snData["prod_type"].ToString()); printData.Add("serial_number", snData["serial_number"].ToString()); printData.Add("type_desc", snData["type_desc"].ToString()); printData.Add("type_name", snData["type_name"].ToString()); csPrinter.DoPrint(LabelTextPath, printData, 1); return true; } private static void WritePrintLog(string msgText) { string logPath = AppDomain.CurrentDomain.BaseDirectory + @"\\PrintLog"; try { if (!System.IO.Directory.Exists(logPath)) { System.IO.Directory.CreateDirectory(logPath); } string txtMsgPath = logPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; string timeStamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss-->"); using (StreamWriter sw = new StreamWriter(txtMsgPath, true)) { sw.WriteLine(timeStamp + msgText.ToString()); sw.Flush(); sw.Close(); } } catch (System.Exception ex) { } } /// /// 输入到打印机 /// /// BarTender.exe 的路径 /// *.btw 文件的路径 public static void Print(string bartendPath, string labelPath) { Process process = new Process(); process.StartInfo = new ProcessStartInfo(ToShortPathName(bartendPath), @" /F=" + ToShortPathName(labelPath) + " /p /x"); process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; process.StartInfo.RedirectStandardOutput = true; try { process.Start(); //process.WaitForExit(100*1000); //等待上述进程执行完毕 process.WaitForExit(100 * 10); //等待上述进程执行完毕 if (process.HasExited == false) //程序停止响应处理 { process.Kill(); process.Dispose(); } } catch { process.Kill(); process.Dispose(); } } /// /// 获取短路径 /// /// 长路径 /// 短路径 /// 短路径的长度 /// 执行结果 [DllImport("kernel32.dll ", CharSet = CharSet.Auto)] public static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength); /// /// 把长路径转化为短路径(兼容 WIN 98 格式) /// /// 长路径 /// 短路径 public static string ToShortPathName(string longName) { StringBuilder shortNameBuffer = new StringBuilder(256); int bufferSize = shortNameBuffer.Capacity; int result = GetShortPathName(longName, shortNameBuffer, bufferSize); return shortNameBuffer.ToString(); } /// /// 执行工步操作 /// /// 执行结果 public override bool Do() { try { //本地计数器 CountRead(); MESPrintSN _app = new MESPrintSN(App); Action(this, new EventArgs()); string inputSN = Context["serial_number"].ToString(); dtLabel = _app.GetPrintSN2(inputSN); List CustPartNoList = new DataTable(); ShowMessage(this, "green|条码正在打印中,条码: " + dtLabel[0]["csn"].ToString()); string printIP = ""; PrintSNByZPL zplPrinter = new PrintSNByZPL(); printIP = _app.GetPrintIP(Context["terminal_id"].ToString()); if (string.IsNullOrEmpty(printIP)) { ShowMessage(this, "red|工位参数中没有正确配置打印机IP地址,请联系系统管理员处理!"); return false; } zplPrinter.PrintSerialNumber(dtLabel, Context["StepLibrary.GYPrintQA.BTWFileName.BTW"].ToString(), printIP, 9100, 1); // 记录日志 using (StreamWriter sw = new StreamWriter("QA_Print_Log_" + DateTime.Now.ToString("yyyyMM") + ".txt", true)) { sw.WriteLine(string.Format("{0} {1}", "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] ", dtLabel[0]["csn"].ToString())); sw.Flush(); sw.Close(); } //插入SAP下线回冲数据 _app.insertBackflush(inputSN, dtLabel[0]["part_no"].ToString()); _app.UpdatePrintCount(inputSN); //本地计数器 CountAdd(); //更新条码状态为下线 _app.updateStatus(inputSN); string stationType = _app.stationType(Context["terminal_id"].ToString()); string tres = _app.ProdTrans(0, Context["serial_number"].ToString(), Convert.ToInt32(Context["terminal_id"].ToString()), 0, stationType); if (tres.ToUpper() != "OK") { ShowMessage(this, "red|" + tres + ""); return false; } Complate(this, new EventArgs()); } catch (Exception ex) { using (StreamWriter sw = new StreamWriter("ProdTrans-" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true)) { sw.WriteLine(string.Format("{0} {1} {2}", "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] ProdTrans", Context["serial_number"].ToString(), ex)); sw.Flush(); sw.Close(); } } return base.Do(); } private void InitializeComponent() { this.SuspendLayout(); this.Name = "PrintQA"; this.Size = new System.Drawing.Size(367, 224); this.ResumeLayout(false); } private void CountAdd() { string value = _PrdCount; int count = 0; try { count = Convert.ToInt32(value); count++; _PrdCount = count.ToString(); } catch { _PrdCount = "1"; } try { StreamWriter sw = new StreamWriter("InPdlineCount.txt"); sw.Write(count.ToString()); sw.Flush(); sw.Close(); } catch { ShowMessage(this, "计数器更新错误,未知!"); } } private void CountRead() { try { if (File.Exists("InPdlineCount.txt")) { FileStream fs = new FileStream(@"InPdlineCount.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); string content = sr.ReadToEnd(); //一次性读取全部数据 _PrdCount = content; fs.Close(); sr.Close(); } else { _PrdCount = "0"; } } catch { _PrdCount = "0"; ShowMessage(this, "计数器更新错误,未知!"); } } } }