/// <summary> /// threadpool callback func /// </summary> /// <param name="data"></param> public void ProcessData(RecordLine recordLine) { if (null == recordLine || 0 == recordLine.ReadMark) { return; } try { ValidateData(recordLine); String dataStr = GenDataMessage(recordLine); Log.Info(String.Format("生成数据报文:[{0}].", dataStr)); SaveToDB(dataStr); SaveToFile(dataStr); recordLine.Processed = true; Log.Info("数据处理."); } catch (System.Exception ex) { Log.Error("数据处理异常.", ex); } }
public static List <RecordLine> ReadFromXmlFile(String _XmlFilename) { List <RecordLine> result = new List <RecordLine>(); if (!String.IsNullOrEmpty(_XmlFilename)) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(_XmlFilename); System.Xml.XmlNodeList lines = doc.GetElementsByTagName("RecordLine"); foreach (System.Xml.XmlNode line in lines) { System.Xml.XmlElement lineEle = line as System.Xml.XmlElement; String palletizer = lineEle.GetAttribute("PalletizerName"); String readMarkDevice = lineEle.GetAttribute("ReadMarkDevice"); String plateCodeDevice = lineEle.GetAttribute("PlateCodeDevice"); System.Xml.XmlNodeList records = lineEle.GetElementsByTagName("Record"); if (records.Count > 0) { RecordLine recordLine = new RecordLine(palletizer, readMarkDevice, plateCodeDevice); foreach (System.Xml.XmlNode record in records) { System.Xml.XmlElement recordEle = record as System.Xml.XmlElement; String boxCodeDevice = recordEle.GetAttribute("BoxCodeDevice"); String amountDevice = recordEle.GetAttribute("AmountDevice"); recordLine.AddRecord(new Record(boxCodeDevice, amountDevice)); } result.Add(recordLine); } } } return(result); }
/// <summary> /// initialize record lines from records configuration file /// </summary> /// <param name="_RecordConfigFilename"></param> void InitializeRecordLines(String _RecordConfigFilename) { try { m_RecordLines = RecordLine.ReadFromXmlFile(_RecordConfigFilename); Log.Info(String.Format("初始化码垛信息软元件配置成功. 已配置 {0} 条码垛线路.", m_RecordLines.Count)); } catch (System.Exception ex) { Log.Error("初始化码垛信息软元件配置失败.", ex); throw; } }
/// <summary> /// get formatted string from given device value /// </summary> public String GenDataMessage(RecordLine _RecordLine) { StringBuilder records = new StringBuilder(); foreach (Record record in _RecordLine.Records) { if (record.ReadMark > 0) { records.AppendFormat("{0}:{1};", record.BoxCode, record.Amount); } } String result = String.Format(m_MsgFormatter, _RecordLine.PalletizerName, _RecordLine.PlateCode, records.ToString()); return(result); }
/// <summary> /// monitor thread callback func /// </summary> void Monitor(Object p) { RecordLine recordLine = p as RecordLine; if (null == recordLine) { return; } DateTime stamp; while (!m_CancelToken.IsCancellationRequested) { stamp = DateTime.Now; lock (m_IOLock) { recordLine.ReadData(m_Com); } m_DataProcessor.ProcessData(recordLine); if (0 != recordLine.ReadMark && recordLine.Processed) { lock (m_IOLock) { recordLine.ResetReadMark(m_Com); } } int sleepInterval = (int)(m_MonitorInterval - (DateTime.Now - stamp).TotalMilliseconds); if (sleepInterval > 0) { Thread.Sleep(sleepInterval); } } }
//void Test1(PLCCom); static void Main(string[] args) { try { int LogicalStationNum = Convert.ToInt32(ConfigurationManager.AppSettings["LogicalStationNum"]); Log.AddConsoleLogger(ConfigurationManager.AppSettings["LogFormatter"]); Log.OpenLog("PLC-Console"); PLCCom com = new PLCCom(LogicalStationNum); int ret = com.Open(); if (0 != ret) throw new Exception("Open com fail."); RecordLine line1 = new RecordLine("MD1", "D01001", "D01002"); Record record1_1 = new Record("D01012", "D01026"); Record record1_2 = new Record("D01032", "D01046"); RecordLine line2 = new RecordLine("MD2", "D01101", "D01102"); Record record2_1 = new Record("D01112", "D01126"); Record record2_2 = new Record("D01132", "D01146"); RecordLine line3 = new RecordLine("MD3", "D01201", "D01202"); Record record3_1 = new Record("D01212", "D01226"); Record record3_2 = new Record("D01232", "D01246"); RecordLine line4 = new RecordLine("MD4", "D01301", "D01302"); Record record4_1 = new Record("D01312", "D01326"); Record record4_2 = new Record("D01332", "D01346"); if (false) { Console.WriteLine("Test read mark."); while (true) { short lineMark1 = CommUtil.GetDevice(line1.ReadMarkDevice, com); if (lineMark1 != 0) Console.WriteLine(String.Format("{2},{3} -- {0}'s read mark is set to {1}.", line1.PalletizerName, lineMark1, DateTime.Now, DateTime.Now.Millisecond)); short lineMark2 = CommUtil.GetDevice(line2.ReadMarkDevice, com); if (lineMark2 != 0) Console.WriteLine(String.Format("{2},{3} -- {0}'s read mark is set to {1}.", line2.PalletizerName, lineMark2, DateTime.Now, DateTime.Now.Millisecond)); short lineMark3 = CommUtil.GetDevice(line3.ReadMarkDevice, com); if (lineMark3 != 0) Console.WriteLine(String.Format("{2},{3} -- {0}'s read mark is set to {1}.", line3.PalletizerName, lineMark3, DateTime.Now, DateTime.Now.Millisecond)); short lineMark4 = CommUtil.GetDevice(line4.ReadMarkDevice, com); if (lineMark4 != 0) Console.WriteLine(String.Format("{2},{3} -- {0}'s read mark is set to {1}.", line4.PalletizerName, lineMark4, DateTime.Now, DateTime.Now.Millisecond)); } } else { while (true) { Console.Write("\nPush enter to write data."); Console.ReadLine(); line1.SetPlateCode("11111111", com); record1_1.SetBoxCode("1A0000000000000000000000000", com); record1_1.SetAmount(11, com); record1_2.SetBoxCode("1B0000000000000000000000000", com); record1_2.SetAmount(12, com); CommUtil.SetDevice(1, line1.ReadMarkDevice, com); line2.SetPlateCode("22222222", com); record2_1.SetBoxCode("2A0000000000000000000000000", com); record2_1.SetAmount(21, com); record2_2.SetBoxCode("2B0000000000000000000000000", com); record2_2.SetAmount(22, com); CommUtil.SetDevice(1, line2.ReadMarkDevice, com); line3.SetPlateCode("33333333", com); record3_1.SetBoxCode("3A0000000000000000000000000", com); record3_1.SetAmount(31, com); record3_2.SetBoxCode("3B0000000000000000000000000", com); record3_2.SetAmount(32, com); CommUtil.SetDevice(1, line3.ReadMarkDevice, com); line4.SetPlateCode("44444444", com); record4_1.SetBoxCode("4A0000000000000000000000000", com); record4_1.SetAmount(41, com); record4_2.SetBoxCode("4B0000000000000000000000000", com); record4_2.SetAmount(42, com); CommUtil.SetDevice(1, line4.ReadMarkDevice, com); } } } catch (System.Exception ex) { Console.WriteLine(ex.Message); } }
public static List<RecordLine> ReadFromXmlFile(String _XmlFilename) { List<RecordLine> result = new List<RecordLine>(); if (!String.IsNullOrEmpty(_XmlFilename)) { System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(_XmlFilename); System.Xml.XmlNodeList lines = doc.GetElementsByTagName("RecordLine"); foreach (System.Xml.XmlNode line in lines) { System.Xml.XmlElement lineEle = line as System.Xml.XmlElement; String palletizer = lineEle.GetAttribute("PalletizerName"); String readMarkDevice = lineEle.GetAttribute("ReadMarkDevice"); String plateCodeDevice = lineEle.GetAttribute("PlateCodeDevice"); System.Xml.XmlNodeList records = lineEle.GetElementsByTagName("Record"); if (records.Count > 0) { RecordLine recordLine = new RecordLine(palletizer, readMarkDevice, plateCodeDevice); foreach (System.Xml.XmlNode record in records) { System.Xml.XmlElement recordEle = record as System.Xml.XmlElement; String boxCodeDevice = recordEle.GetAttribute("BoxCodeDevice"); String amountDevice = recordEle.GetAttribute("AmountDevice"); recordLine.AddRecord(new Record(boxCodeDevice, amountDevice)); } result.Add(recordLine); } } } return result; }
private void ValidateData(RecordLine recordLine) { { bool hasInvalidChar = false; foreach (Char c in recordLine.PlateCode) hasInvalidChar |= !Char.IsDigit(c); if (hasInvalidChar) { String msg = "托盘码发现非法字符."; if (m_ValidatePlateCode) throw new Exception(msg); else Log.Warn(msg); } } { bool allEmpty = true; foreach (Record record in recordLine.Records) allEmpty &= record.ReadMark == 0; if (allEmpty) { String msg = "货物数量为 0."; if (m_ValidateRecordNum) throw new Exception(msg); else Log.Warn(msg); } } { bool hasInvalidChar = false; foreach (Record record in recordLine.Records) if (record.ReadMark != 0) foreach (Char c in record.BoxCode) hasInvalidChar |= !Char.IsLetterOrDigit(c); if (hasInvalidChar) { String msg = "货物箱码发现非法字符."; if (m_ValidatePlateCode) throw new Exception(msg); else Log.Warn(msg); } } }
/// <summary> /// threadpool callback func /// </summary> /// <param name="data"></param> public void ProcessData(RecordLine recordLine) { if (null == recordLine || 0 == recordLine.ReadMark) return; try { ValidateData(recordLine); String dataStr = GenDataMessage(recordLine); Log.Info(String.Format("生成数据报文:[{0}].", dataStr)); SaveToDB(dataStr); SaveToFile(dataStr); recordLine.Processed = true; Log.Info("数据处理."); } catch (System.Exception ex) { Log.Error("数据处理异常.", ex); } }
/// <summary> /// get formatted string from given device value /// </summary> public String GenDataMessage(RecordLine _RecordLine) { StringBuilder records = new StringBuilder(); foreach (Record record in _RecordLine.Records) if (record.ReadMark > 0) records.AppendFormat("{0}:{1};", record.BoxCode, record.Amount); String result = String.Format(m_MsgFormatter, _RecordLine.PalletizerName, _RecordLine.PlateCode, records.ToString()); return result; }
private void ValidateData(RecordLine recordLine) { { bool hasInvalidChar = false; foreach (Char c in recordLine.PlateCode) { hasInvalidChar |= !Char.IsDigit(c); } if (hasInvalidChar) { String msg = "托盘码发现非法字符."; if (m_ValidatePlateCode) { throw new Exception(msg); } else { Log.Warn(msg); } } } { bool allEmpty = true; foreach (Record record in recordLine.Records) { allEmpty &= record.ReadMark == 0; } if (allEmpty) { String msg = "货物数量为 0."; if (m_ValidateRecordNum) { throw new Exception(msg); } else { Log.Warn(msg); } } } { bool hasInvalidChar = false; foreach (Record record in recordLine.Records) { if (record.ReadMark != 0) { foreach (Char c in record.BoxCode) { hasInvalidChar |= !Char.IsLetterOrDigit(c); } } } if (hasInvalidChar) { String msg = "货物箱码发现非法字符."; if (m_ValidatePlateCode) { throw new Exception(msg); } else { Log.Warn(msg); } } } }