private void DispatcherTimerAction(Object sender, EventArgs e) { if (!File.Exists(@"D:\Maintain.csv")) { string[] heads = { "时间", "机台号", "触发次数", "扫码次数", "上传次数" }; Csvfile.AddNewLine(@"D:\Maintain.csv", heads); } if (LastCleanRecordFlag != GetBanciDate() + GetBanci()) { LastCleanRecordFlag = GetBanciDate() + GetBanci(); Inifile.INIWriteValue(ParameterIniPath, "Record", "LastCleanRecordFlag", LastCleanRecordFlag); if (!Directory.Exists("D:\\" + LastCleanRecordFlag)) { Directory.CreateDirectory("D:\\" + LastCleanRecordFlag); } string[] count = { DateTime.Now.ToString(), JiTaiHao, TrigerTimes.ToString(), ScanTimes.ToString(), UpdateTimes.ToString() }; Csvfile.AddNewLine(@"D:\Maintain.csv", count); TrigerTimes = 0; ScanTimes = 0; UpdateTimes = 0; Inifile.INIWriteValue(ParameterIniPath, "Times", "TrigerTimes", TrigerTimes.ToString()); Inifile.INIWriteValue(ParameterIniPath, "Times", "ScanTimes", ScanTimes.ToString()); Inifile.INIWriteValue(ParameterIniPath, "Times", "UpdateTimes", UpdateTimes.ToString()); MsgText = AddMessage("记录清空"); } if (recordItemList.Count > 0) { lock (LockObject) { foreach (RecordItem item in recordItemList) { RecordCollection.Add(item); } recordItemList.Clear(); } } }
public void PlcRun() { bool first = true; bool scanFlag = false; Random rd = new Random(); while (true) { System.Threading.Thread.Sleep(10); if (Xinjie == null) { System.Threading.Thread.Sleep(1000); } else { try { PLCConnect = Xinjie.ReadM(24576); if (PLCConnect) { PlcIn = Xinjie.ReadMultiMCoil(1500); Xinjie.WritMultiMCoil(1800, PlcOut); Xinjie.WriteW(150, rd.Next(0, 999).ToString()); dd8170 = Xinjie.ReadD(16554); dd4208 = Xinjie.ReadD(4208); RotalAngle = (dd8170 - dd4208) / 91776 * 360; //扫码 if (scanFlag != PlcIn[0]) { scanFlag = PlcIn[0]; if (scanFlag) { Abarcode_f = false; Bbarcode_f = false; PlcOut[0] = false; PlcOut[1] = false; PlcOut[2] = false; Xinjie.WritMultiMCoil(1800, PlcOut); ScanA.GetBarCode(ScanActionCallback1); ScanB.GetBarCode(ScanActionCallback2); TrigerTimes++; Inifile.INIWriteValue(ParameterIniPath, "Times", "TrigerTimes", TrigerTimes.ToString()); } else { PlcOut[0] = false; } } if (Abarcode_f && Bbarcode_f) { Abarcode_f = false; Bbarcode_f = false; PlcOut[0] = true; PlcOut[1] = Abarcode_s; PlcOut[2] = Bbarcode_s; if (Abarcode_s && Bbarcode_s) { ScanTimes++; Inifile.INIWriteValue(ParameterIniPath, "Times", "ScanTimes", ScanTimes.ToString()); } RecordItem recordItem = new RecordItem(); recordItem.日期 = GetBanciDate(); recordItem.班次 = GetBanci(); recordItem.机台号 = JiTaiHao; recordItem.产品barcode = Abarcode; recordItem.机台穴号 = Bbarcode; lock (LockObject) { recordItemList.Add(recordItem); } if (Directory.Exists("D:\\" + GetBanciDate() + GetBanci())) { string filename = "D:\\" + GetBanciDate() + GetBanci() + "\\" + GetBanciDate() + GetBanci() + ".csv"; if (File.Exists(filename)) { string[] heads = { "日期", "班次", "机台号", "机台穴号", "产品barcode" }; Csvfile.AddNewLine(filename, heads); } string[] count = { recordItem.日期, recordItem.班次, recordItem.机台号, recordItem.机台穴号, recordItem.产品barcode }; Csvfile.AddNewLine(filename, count); } } } else { if (first) { first = false; } else { MsgText = AddMessage("PLC断线,重新连接..."); } Xinjie.ModbusDisConnect(); System.Threading.Thread.Sleep(1000); Xinjie.ModbusInit(PLCPortCom, 19200, System.IO.Ports.Parity.Even, 8, System.IO.Ports.StopBits.One); Xinjie.ModbusConnect(); } } catch (Exception ex) { MsgText = AddMessage(ex.Message); PLCConnect = false; } } } }