diff --git a/HighWayIot.Plc/PlcConnect.cs b/HighWayIot.Plc/PlcConnect.cs index 4aa7804..c5d452f 100644 --- a/HighWayIot.Plc/PlcConnect.cs +++ b/HighWayIot.Plc/PlcConnect.cs @@ -20,9 +20,14 @@ namespace HighWayIot.Plc private static LogHelper logHelper = LogHelper.Instance; /// - /// 静态懒加载MelsecMcNet + /// 静态懒加载MelsecMcNet1 /// - private static readonly MelsecMcNet MelsecInstance = new PlcConnect().CreateAb(); + private static readonly MelsecMcNet MelsecInstance1 = new PlcConnect().CreateAb("192.168.0.7"); + + /// + /// 静态懒加载MelsecMcNet2 + /// + private static readonly MelsecMcNet MelsecInstance2 = new PlcConnect().CreateAb("192.168.0.8"); private PlcConnect() { @@ -33,13 +38,13 @@ namespace HighWayIot.Plc /// 初始化三菱的服务器 /// /// - private MelsecMcNet CreateAb() + private MelsecMcNet CreateAb(string ip) { - string Ip = "192.168.0.7"; + //string Ip = ; MelsecMcNet plc = new MelsecMcNet(); try { - plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(Ip, 2001) + plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(ip, 2001) { ConnectTimeOut = 3000, // 连接超时时间,单位毫秒 SleepTime = 0, @@ -62,27 +67,41 @@ namespace HighWayIot.Plc } /// - /// plc 是不是保持链接 + /// plc1 是不是保持链接 /// - public static bool IsConnect + public static bool IsConnect1 { get { - if (MelsecInstance == null) return false; - var result = MelsecInstance.IpAddress; + if (MelsecInstance1 == null) return false; + var result = MelsecInstance1.IpAddress; logHelper.Info($"PLCIP:[{result}]"); return !string.IsNullOrEmpty(result); } } /// - /// 写入数据 + /// plc2 是不是保持链接 + /// + public static bool IsConnect2 + { + get + { + if (MelsecInstance2 == null) return false; + var result = MelsecInstance2.IpAddress; + logHelper.Info($"PLCIP:[{result}]"); + return !string.IsNullOrEmpty(result); + } + } + + /// + /// PLC1写入数据 /// /// 地址 /// 值 /// 数据类型 /// - public static OperateResult PlcWrite(string address, object value, DataTypeEnum type) + public static OperateResult PlcWrite1(string address, object value, DataTypeEnum type) { var result = new OperateResult() { IsSuccess = false }; try @@ -90,34 +109,34 @@ namespace HighWayIot.Plc switch (type) { case DataTypeEnum.Bool: - result = MelsecInstance.Write(address, Convert.ToBoolean(value)); + result = MelsecInstance1.Write(address, Convert.ToBoolean(value)); break; //case DataTypeEnum.Byte: // result = Instance.Write(address, Convert.ToByte(value)); // break; case DataTypeEnum.Int16: - result = MelsecInstance.Write(address, Convert.ToInt16(value)); + result = MelsecInstance1.Write(address, Convert.ToInt16(value)); break; case DataTypeEnum.UInt16: - result = MelsecInstance.Write(address, Convert.ToUInt16(value)); + result = MelsecInstance1.Write(address, Convert.ToUInt16(value)); break; case DataTypeEnum.Int32: - result = MelsecInstance.Write(address, Convert.ToInt32(value)); + result = MelsecInstance1.Write(address, Convert.ToInt32(value)); break; case DataTypeEnum.UInt32: - result = MelsecInstance.Write(address, Convert.ToUInt32(value)); + result = MelsecInstance1.Write(address, Convert.ToUInt32(value)); break; case DataTypeEnum.Int64: - result = MelsecInstance.Write(address, Convert.ToInt64(value)); + result = MelsecInstance1.Write(address, Convert.ToInt64(value)); break; case DataTypeEnum.UInt64: - result = MelsecInstance.Write(address, Convert.ToUInt64(value)); + result = MelsecInstance1.Write(address, Convert.ToUInt64(value)); break; case DataTypeEnum.Float: - result = MelsecInstance.Write(address, Convert.ToSingle(value)); + result = MelsecInstance1.Write(address, Convert.ToSingle(value)); break; case DataTypeEnum.Double: - result = MelsecInstance.Write(address, Convert.ToDouble(value)); + result = MelsecInstance1.Write(address, Convert.ToDouble(value)); break; default: throw new ArgumentException($"Unsupported data type: {type}"); @@ -131,17 +150,290 @@ namespace HighWayIot.Plc return result; } + + /// + /// PLC2写入数据 + /// + /// 地址 + /// 值 + /// 数据类型 + /// + public static OperateResult PlcWrite2(string address, object value, DataTypeEnum type) + { + var result = new OperateResult() { IsSuccess = false }; + try + { + switch (type) + { + case DataTypeEnum.Bool: + result = MelsecInstance2.Write(address, Convert.ToBoolean(value)); + break; + //case DataTypeEnum.Byte: + // result = Instance.Write(address, Convert.ToByte(value)); + // break; + case DataTypeEnum.Int16: + result = MelsecInstance2.Write(address, Convert.ToInt16(value)); + break; + case DataTypeEnum.UInt16: + result = MelsecInstance2.Write(address, Convert.ToUInt16(value)); + break; + case DataTypeEnum.Int32: + result = MelsecInstance2.Write(address, Convert.ToInt32(value)); + break; + case DataTypeEnum.UInt32: + result = MelsecInstance2.Write(address, Convert.ToUInt32(value)); + break; + case DataTypeEnum.Int64: + result = MelsecInstance2.Write(address, Convert.ToInt64(value)); + break; + case DataTypeEnum.UInt64: + result = MelsecInstance2.Write(address, Convert.ToUInt64(value)); + break; + case DataTypeEnum.Float: + result = MelsecInstance2.Write(address, Convert.ToSingle(value)); + break; + case DataTypeEnum.Double: + result = MelsecInstance2.Write(address, Convert.ToDouble(value)); + break; + default: + throw new ArgumentException($"Unsupported data type: {type}"); + } + logHelper.PlcLog($"Read address:[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]"); + } + catch (Exception ex) + { + logHelper.Error("PLC写入数据发生错误!", ex); + } + return result; + } + + /// + /// 读取bool + /// + /// + /// + public static bool ReadBool1(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadBool(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取Int16 + /// + /// + /// + public static short ReadInt161(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadInt16(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取UInt16 + /// + /// + /// + public static ushort ReadUInt161(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadUInt16(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取Int32 + /// + /// + /// + public static int ReadInt321(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadInt32(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取UInt32 + /// + /// + /// + public static uint ReadUInt321(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadUInt32(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取Int64 + /// + /// + /// + public static long ReadInt641(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadInt64(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取UInt64 + /// + /// + /// + public static ulong ReadUInt641(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadUInt64(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取Float + /// + /// + /// + public static float ReadFloat1(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadFloat(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + + /// + /// 读取Double + /// + /// + /// + public static double ReadDouble1(string address) + { + OperateResult result = new OperateResult(); + try + { + result = MelsecInstance1.ReadDouble(address); + } + catch (Exception ex) + { + logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex); + return default; + } + if (!result.IsSuccess) + { + return default; + } + return result.Content; + } + /// /// 读取bool /// /// /// - public static bool ReadBool(string address) + public static bool ReadBool2(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadBool(address); + result = MelsecInstance2.ReadBool(address); } catch (Exception ex) { @@ -160,12 +452,12 @@ namespace HighWayIot.Plc /// /// /// - public static short ReadInt16(string address) + public static short ReadInt162(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadInt16(address); + result = MelsecInstance2.ReadInt16(address); } catch (Exception ex) { @@ -184,12 +476,12 @@ namespace HighWayIot.Plc /// /// /// - public static ushort ReadUInt16(string address) + public static ushort ReadUInt162(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadUInt16(address); + result = MelsecInstance2.ReadUInt16(address); } catch (Exception ex) { @@ -208,12 +500,12 @@ namespace HighWayIot.Plc /// /// /// - public static int ReadInt32(string address) + public static int ReadInt322(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadInt32(address); + result = MelsecInstance2.ReadInt32(address); } catch (Exception ex) { @@ -232,12 +524,12 @@ namespace HighWayIot.Plc /// /// /// - public static uint ReadUInt32(string address) + public static uint ReadUInt322(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadUInt32(address); + result = MelsecInstance2.ReadUInt32(address); } catch (Exception ex) { @@ -256,12 +548,12 @@ namespace HighWayIot.Plc /// /// /// - public static long ReadInt64(string address) + public static long ReadInt642(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadInt64(address); + result = MelsecInstance2.ReadInt64(address); } catch (Exception ex) { @@ -280,12 +572,12 @@ namespace HighWayIot.Plc /// /// /// - public static ulong ReadUInt64(string address) + public static ulong ReadUInt642(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadUInt64(address); + result = MelsecInstance2.ReadUInt64(address); } catch (Exception ex) { @@ -304,12 +596,12 @@ namespace HighWayIot.Plc /// /// /// - public static float ReadFloat(string address) + public static float ReadFloat2(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadFloat(address); + result = MelsecInstance2.ReadFloat(address); } catch (Exception ex) { @@ -328,12 +620,12 @@ namespace HighWayIot.Plc /// /// /// - public static double ReadDouble(string address) + public static double ReadDouble2(string address) { OperateResult result = new OperateResult(); try { - result = MelsecInstance.ReadDouble(address); + result = MelsecInstance2.ReadDouble(address); } catch (Exception ex) { diff --git a/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs b/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs index ba6cfcc..59f794f 100644 --- a/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs +++ b/HighWayIot.Plc/PlcHelper/WorkStationHelper.cs @@ -24,7 +24,7 @@ namespace HighWayIot.Plc.PlcHelper //选择是小车的哪个点位 point += rgvStationNo; - bool result = PlcConnect.PlcWrite($"B{point.ToString("X")}", true, DataTypeEnum.Bool).IsSuccess; + bool result = PlcConnect.PlcWrite1($"B{point.ToString("X")}", true, DataTypeEnum.Bool).IsSuccess; return result; } @@ -41,7 +41,7 @@ namespace HighWayIot.Plc.PlcHelper for (int i = 0x230; i <= 0x240; i++) { int j = 0; - result[j] = PlcConnect.ReadBool($"B{i.ToString("X")}"); + result[j] = PlcConnect.ReadBool1($"B{i.ToString("X")}"); j++; } diff --git a/HighWayIot.Winform/Business/WorkStationBusiness.cs b/HighWayIot.Winform/Business/WorkStationBusiness.cs index 676aa76..6afff10 100644 --- a/HighWayIot.Winform/Business/WorkStationBusiness.cs +++ b/HighWayIot.Winform/Business/WorkStationBusiness.cs @@ -155,8 +155,10 @@ namespace HighWayIot.Winform.Business } byte[] data = entity.Data[0].EPC; + //标签号byte数组转换成字符串 string result = string.Join(" ", data.Select(x => x.ToString("X2"))); + //根据IP和标签EPC获取工位和对应设备号 string deviceNo = _tagrService.GetTagDeviceNoByEPC(result); string workstationNo = _readerService.GetWorkstateNoByIp(ip); @@ -173,6 +175,7 @@ namespace HighWayIot.Winform.Business try { + ///写入对应的PLC信号 _workStationHelper.WriteStationSingal(int.Parse(workstationNo), int.Parse(deviceNo)); } catch (Exception ex) diff --git a/HighWayIot.Winform/UserControlPages/TestPage.cs b/HighWayIot.Winform/UserControlPages/TestPage.cs index 3b1cd8d..7b9007c 100644 --- a/HighWayIot.Winform/UserControlPages/TestPage.cs +++ b/HighWayIot.Winform/UserControlPages/TestPage.cs @@ -50,7 +50,7 @@ namespace HighWayIot.Winform.UserControlPages private void button2_Click(object sender, EventArgs e) { - var res = PlcConnect.IsConnect; + var res = PlcConnect.IsConnect1; PlcShowValue.Text = res.ToString(); } @@ -68,31 +68,31 @@ namespace HighWayIot.Winform.UserControlPages switch (en) { case DataTypeEnum.Bool: - res = PlcConnect.ReadBool(PlcAddress.Text).ToString(); + res = PlcConnect.ReadBool1(PlcAddress.Text).ToString(); break; case DataTypeEnum.Int16: - res = PlcConnect.ReadInt16(PlcAddress.Text).ToString(); + res = PlcConnect.ReadInt161(PlcAddress.Text).ToString(); break; case DataTypeEnum.UInt16: - res = PlcConnect.ReadUInt16(PlcAddress.Text).ToString(); + res = PlcConnect.ReadUInt161(PlcAddress.Text).ToString(); break; case DataTypeEnum.Int32: - res = PlcConnect.ReadInt32(PlcAddress.Text).ToString(); + res = PlcConnect.ReadInt321(PlcAddress.Text).ToString(); break; case DataTypeEnum.UInt32: - res = PlcConnect.ReadUInt32(PlcAddress.Text).ToString(); + res = PlcConnect.ReadUInt321(PlcAddress.Text).ToString(); break; case DataTypeEnum.Int64: - res = PlcConnect.ReadInt64(PlcAddress.Text).ToString(); + res = PlcConnect.ReadInt641(PlcAddress.Text).ToString(); break; case DataTypeEnum.UInt64: - res = PlcConnect.ReadUInt64(PlcAddress.Text).ToString(); + res = PlcConnect.ReadUInt641(PlcAddress.Text).ToString(); break; case DataTypeEnum.Float: - res = PlcConnect.ReadFloat(PlcAddress.Text).ToString(); + res = PlcConnect.ReadFloat1(PlcAddress.Text).ToString(); break; case DataTypeEnum.Double: - res = PlcConnect.ReadDouble(PlcAddress.Text).ToString(); + res = PlcConnect.ReadDouble1(PlcAddress.Text).ToString(); break; default: res = "不对劲儿奥"; @@ -113,7 +113,7 @@ namespace HighWayIot.Winform.UserControlPages { MessageBox.Show("类型转换错误!"); } - var result = PlcConnect.PlcWrite(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); + var result = PlcConnect.PlcWrite1(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); bool r = result.IsSuccess; PlcShowValue.Text = r.ToString(); }