Browse Source

tgo

Lishi
pry 2 years ago
parent
commit
e4caf364fe
2 changed files with 63 additions and 18 deletions
  1. +11
    -11
      HKControl/Main.cs
  2. +52
    -7
      HKControl/Siemens.cs

+ 11
- 11
HKControl/Main.cs View File

@@ -26,22 +26,22 @@ namespace HKControl
switch (CarNum)
{
case 1:
Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
//Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
return DataModels[1].LeftWindowData.IsSwipe;
case 2:
Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
//Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
return DataModels[1].RightWindowData.IsSwipe;
case 3:
Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
//Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
return DataModels[2].LeftWindowData.IsSwipe;
case 4:
Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
//Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
return DataModels[2].RightWindowData.IsSwipe;
case 5:
Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
//Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].LeftWindowData.IsSwipe}");
return DataModels[3].LeftWindowData.IsSwipe;
case 6:
Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
//Debug.WriteLine($"{CarNum} 号窗口反馈状态:{DataModels[1].RightWindowData.IsSwipe}");
return DataModels[3].LeftWindowData.IsSwipe;
default:
break;
@@ -72,7 +72,7 @@ namespace HKControl
{
SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
{
Debug.WriteLine($"{item.DeviceNum}:连接成功");
HKLog.HKLogImport.WriteInfo($"{item.DeviceNum}:连接成功");
ThreadManage.GetInstance().StartLong(new Action(() =>
{
try
@@ -87,9 +87,9 @@ namespace HKControl
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
HKLog.HKLogImport.WriteInfo(ex.ToString());
}


//DataModels[item.DeviceNum].LeftWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.0");
@@ -99,7 +99,7 @@ namespace HKControl
//DataModels[item.DeviceNum].RightWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M7.3");

Thread.Sleep(100);
}), $"{item.DeviceNum} 号设备监听",true);
}), $"{item.DeviceNum} 号设备监听", true);
});
SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
}), $"{item.DeviceNum} 号设备连接初始化");
@@ -154,7 +154,7 @@ namespace HKControl

private void Control(int num, string add)
{
Debug.WriteLine($"{num} 号出餐口控制");
HKLog.HKLogImport.WriteInfo($"{num} 号出餐口控制");
SiemensDicitonary[num].Write(add, true);
Thread.Sleep(1000);
SiemensDicitonary[num].Write(add, false);


+ 52
- 7
HKControl/Siemens.cs View File

@@ -13,6 +13,10 @@ namespace HKControl
Plc myPlc;
public bool IsConnected => myPlc is null ? false : myPlc.IsConnected;

private CpuType myCpuType;
private string MyIp;
private int Myport;

/// <summary>
/// 打开连接
/// </summary>
@@ -25,8 +29,16 @@ namespace HKControl
{
try
{
myCpuType = cpuType;
MyIp = ip;
Myport = port;
myPlc = new Plc(cpuType, ip, port, rack, solt);
myPlc.Open();
while (!myPlc.IsConnected)
{
myPlc.Open();
if (!myPlc.IsConnected) Thread.Sleep(3000);
}

if (myPlc.IsConnected)
{
ConnectOk?.Invoke();
@@ -36,12 +48,20 @@ namespace HKControl
}
catch (Exception ex)
{
Debug.WriteLine($"设备连接出错:{ip}");
HKLog.HKLogImport.WriteInfo($"设备连接出错:{ip}");
//Debug.WriteLine(ex.ToString());
}

}

private void ExceptionDis()
{
myPlc?.Close();
myPlc = null;
Connect(myCpuType, MyIp, Myport);
}


/// <summary>
/// 断开和PLC的连接
/// </summary>
@@ -52,8 +72,17 @@ namespace HKControl

public TResult Read<TResult>(string address)
{
if (!IsConnected) return default;
return (TResult)myPlc?.Read(address);
try
{
if (!IsConnected) return default;
return (TResult)myPlc?.Read(address);
}
catch (Exception ex)
{
ExceptionDis();
return default;
}

}

public bool[] ReadBools(int address, int count)
@@ -82,13 +111,29 @@ namespace HKControl

private object Read(DataType dataType, int db, int address, VarType varType, int count)
{
if (!IsConnected) return default;
return myPlc?.Read(dataType, db, address, varType, count);
try
{
if (!IsConnected) return default;
return myPlc?.Read(dataType, db, address, varType, count);
}
catch (Exception ex)
{
ExceptionDis();
}

}

public void Write<TValue>(string address, TValue value)
{
myPlc?.Write(address, value);
try
{
myPlc?.Write(address, value);
}
catch (Exception ex)
{
ExceptionDis();
}

}

public ReadT ReadStruct<ReadT>(int db, int startAddress = 0)


Loading…
Cancel
Save