/// <summary> /// private constructor /// </summary> DeviceMonitor() { m_IOLock = new Object(); m_Com = new PLCCom(0); m_RecordLines = new List <RecordLine>(); m_DataProcessor = new DataProcessor(); }
/// <summary> /// set value of single device /// </summary> /// <param name="_Data"></param> /// <param name="_DeviceName"></param> /// <param name="_Com"></param> public static void SetDevice(short _Data, String _DeviceName, PLCCom _Com) { int ret = _Com.SetDevice(_DeviceName, _Data); if (0 != ret) throw new Exception(String.Format("写入数据到设备失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); }
private void ConnButton_Click(object sender, EventArgs e) { try { m_PLCCom = new PLCCom(int.Parse(LogicStationNum.Text)); int ret = m_PLCCom.Open(); if (0 == ret) { LinkStatus.Text = "连通"; LogicStationNum.Enabled = false; ConnButton.Enabled = false; ReadButton.Enabled = true; WriteButton.Enabled = true; m_Connected = true; } else { throw new Exception(String.Format("连接失败. 0x{0:x8} [HEX]", ret)); } } catch (System.Exception ex) { MessageBox.Show(ex.Message, "错误"); } }
/// <summary> /// set value of single device /// </summary> /// <param name="_Data"></param> /// <param name="_DeviceName"></param> /// <param name="_Com"></param> public static void SetDevice(short _Data, String _DeviceName, PLCCom _Com) { int ret = _Com.SetDevice(_DeviceName, _Data); if (0 != ret) { throw new Exception(String.Format("写入数据到设备失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); } }
public void ReadData(PLCCom _Com) { ReadMark = CommUtil.GetDevice(AmountDevice, _Com); if (ReadMark > 0) { GetBoxCode(_Com); GetAmount(_Com); } }
/// <summary> /// get value of single device, retuen as short /// </summary> /// <param name="_DeviceName"></param> /// <param name="_Com"></param> /// <returns></returns> public static short GetDevice(String _DeviceName, PLCCom _Com) { short result = 0; int ret = _Com.GetDevice(_DeviceName, ref result); if (0 != ret) throw new Exception(String.Format("从设备中读取数据失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); return result; }
/// <summary> /// get value of single device, retuen as short /// </summary> /// <param name="_DeviceName"></param> /// <param name="_Com"></param> /// <returns></returns> public static short GetDevice(String _DeviceName, PLCCom _Com) { short result = 0; int ret = _Com.GetDevice(_DeviceName, ref result); if (0 != ret) { throw new Exception(String.Format("从设备中读取数据失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); } return(result); }
public void ResetReadMark(PLCCom _Com) { try { CommUtil.SetDevice(0, ReadMarkDevice, _Com); Log.Info("重设读取标志成功."); } catch (System.Exception ex) { Log.Error("重设读取标志失败.", ex); } }
/// <summary> /// get amount data from device /// </summary> /// <param name="_Com"></param> public void GetAmount(PLCCom _Com) { try { Amount = CommUtil.GetDevice(AmountDevice, _Com); Log.Info(String.Format("数量读取成功. 软元件地址:[{0}]. 数量:[{1}].", AmountDevice, Amount)); } catch (System.Exception ex) { Log.Error("数量读取失败.", ex); throw; } }
/// <summary> /// set data to device /// </summary> /// <param name="_Data"></param> /// <param name="_DeviceName"></param> /// <param name="_DataSize"></param> /// <param name="_Com"></param> public static void SetStringToDevice(String _Data, String _DeviceName, int _DataSize, PLCCom _Com) { sbyte[] buffer = new sbyte[_DataSize]; Array.Clear(buffer, 0, _DataSize); int len = Math.Min(_Data.Length, _DataSize); for (int i = 0; i < _DataSize; ++i) buffer[i] = (sbyte)_Data[i]; int ret = _Com.WriteDeviceBlock(_DeviceName, buffer); if (0 != ret) throw new Exception(String.Format("写入数据到设备失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); }
/// <summary> /// set box code to device /// </summary> /// <param name="_BoxCode"></param> /// <param name="_Com"></param> public void SetBoxCode(String _BoxCode, PLCCom _Com) { try { BoxCode = _BoxCode; CommUtil.SetStringToDevice(BoxCode, BoxCodeDevice, BOX_CODE_SIZE, _Com); Log.Info(String.Format("箱码写入成功. 软元件地址:[{0}]. 箱码:[{1}].", BoxCodeDevice, BoxCode)); } catch (System.Exception ex) { Log.Error("箱码写入失败.", ex); } }
/// <summary> /// set plate code to device /// </summary> /// <param name="_plateCode"></param> /// <param name="_Com"></param> public void SetPlateCode(String _PlateCode, PLCCom _Com) { try { PlateCode = _PlateCode; CommUtil.SetStringToDevice(PlateCode, PlateCodeDevice, PLATE_CODE_SIZE, _Com); Log.Info(String.Format("托盘码写入成功. 软元件地址:[{0}]. 托盘码:Data [{1}]", PlateCodeDevice, PlateCode)); } catch (System.Exception ex) { Log.Error("托盘码写入失败.", ex); } }
/// <summary> /// set amount to device /// </summary> /// <param name="_Amount"></param> /// <param name="_Com"></param> public void SetAmount(short _Amount, PLCCom _Com) { try { Amount = _Amount; CommUtil.SetDevice(Amount, AmountDevice, _Com); Log.Info(String.Format("数量写入成功. 软元件地址:[{0}]. 数量:[{1}].", AmountDevice, Amount)); } catch (System.Exception ex) { Log.Error("数量写入失败.", ex); } }
/// <summary> /// get data from device, return as string /// </summary> /// <param name="_DeviceName"></param> /// <param name="_DataSize"></param> /// <param name="_Com"></param> /// <returns></returns> public static String GetStringFromDevice(String _DeviceName, int _DataSize, PLCCom _Com) { sbyte[] buffer = new sbyte[_DataSize]; Array.Clear(buffer, 0, _DataSize); int ret = _Com.ReadDeviceBlock(_DeviceName, buffer); if (0 != ret) throw new Exception(String.Format("从设备中读取数据失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); StringBuilder result = new StringBuilder(); for (int i = 0; i < _DataSize; ++i) result.Append((Char)buffer[i]); return result.ToString(); }
/// <summary> /// get data from device, return as string /// </summary> /// <param name="_DeviceName"></param> /// <param name="_DataSize"></param> /// <param name="_Com"></param> /// <returns></returns> public static String GetStringFromDevice(String _DeviceName, int _DataSize, PLCCom _Com) { sbyte[] buffer = new sbyte[_DataSize]; Array.Clear(buffer, 0, _DataSize); int ret = _Com.ReadDeviceBlock(_DeviceName, buffer); if (0 != ret) { throw new Exception(String.Format("从设备中读取数据失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); } StringBuilder result = new StringBuilder(); for (int i = 0; i < _DataSize; ++i) { result.Append((Char)buffer[i]); } return(result.ToString()); }
/// <summary> /// get plate code data from device /// </summary> /// <param name="_Com"></param> public void GetPlateCode(PLCCom _Com) { try { String tmpStr = CommUtil.GetStringFromDevice(PlateCodeDevice, PLATE_CODE_SIZE, _Com); StringBuilder regularStr = new StringBuilder(); foreach (Char c in tmpStr) { regularStr.Append(Char.IsLetterOrDigit(c) ? c : '#'); } PlateCode = regularStr.ToString(); Log.Info(String.Format("托盘码读取成功. 软元件地址:[{0}]. 托盘码:[{1}].", PlateCodeDevice, PlateCode)); } catch (System.Exception ex) { Log.Error("托盘码读取失败.", ex); throw; } }
public void ReadData(PLCCom _Com) { try { ReadMark = CommUtil.GetDevice(ReadMarkDevice, _Com); if (0 != ReadMark) { Log.Info(String.Format("检测读取标志已设置为 [{0}]. 开始读取数据.", ReadMark)); GetPlateCode(_Com); foreach (Record record in Records) { record.ReadData(_Com); } Processed = false; } } catch (System.Exception ex) { Log.Error("数据读取失败.", ex); } }
private void ConnButton_Click(object sender, EventArgs e) { try { m_PLCCom = new PLCCom(int.Parse(LogicStationNum.Text)); int ret = m_PLCCom.Open(); if (0 == ret) { LinkStatus.Text = "连通"; LogicStationNum.Enabled = false; ConnButton.Enabled = false; ReadButton.Enabled = true; WriteButton.Enabled = true; m_Connected = true; } else throw new Exception(String.Format("连接失败. 0x{0:x8} [HEX]", ret)); } catch (System.Exception ex) { MessageBox.Show(ex.Message, "错误"); } }
/// <summary> /// get box code data from device /// </summary> /// <param name="_Com"></param> public void GetBoxCode(PLCCom _Com) { try { String tmpStr = CommUtil.GetStringFromDevice(BoxCodeDevice, BOX_CODE_SIZE, _Com); StringBuilder regularStr = new StringBuilder(); foreach (Char c in tmpStr) regularStr.Append(Char.IsLetterOrDigit(c) ? c : '#'); BoxCode = regularStr.ToString(); Log.Info(String.Format("箱码读取成功. 软元件地址:[{0}]. 箱码:[{1}].", BoxCodeDevice, BoxCode)); } catch (System.Exception ex) { Log.Error("箱码读取失败.", ex); throw; } }
/// <summary> /// set data to device /// </summary> /// <param name="_Data"></param> /// <param name="_DeviceName"></param> /// <param name="_DataSize"></param> /// <param name="_Com"></param> public static void SetStringToDevice(String _Data, String _DeviceName, int _DataSize, PLCCom _Com) { sbyte[] buffer = new sbyte[_DataSize]; Array.Clear(buffer, 0, _DataSize); int len = Math.Min(_Data.Length, _DataSize); for (int i = 0; i < _DataSize; ++i) { buffer[i] = (sbyte)_Data[i]; } int ret = _Com.WriteDeviceBlock(_DeviceName, buffer); if (0 != ret) { throw new Exception(String.Format("写入数据到设备失败. 错误码:0x{0:x8} [HEX]. 软元件地址:[{1}].", ret, _DeviceName)); } }
/// <summary> /// private constructor /// </summary> DeviceMonitor() { m_IOLock = new Object(); m_Com = new PLCCom(0); m_RecordLines = new List<RecordLine>(); m_DataProcessor = new DataProcessor(); }
public void ReadData(PLCCom _Com) { try { ReadMark = CommUtil.GetDevice(ReadMarkDevice, _Com); if (0 != ReadMark) { Log.Info(String.Format("检测读取标志已设置为 [{0}]. 开始读取数据.", ReadMark)); GetPlateCode(_Com); foreach (Record record in Records) record.ReadData(_Com); Processed = false; } } catch (System.Exception ex) { Log.Error("数据读取失败.", ex); } }
//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); } }
//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); } }