Browse Source

设备控制

Lishi
pry 2 years ago
parent
commit
3ac2aacba3
2 changed files with 81 additions and 10 deletions
  1. +77
    -6
      HKControl/Main.cs
  2. +4
    -4
      HKControl/Siemens.cs

+ 77
- 6
HKControl/Main.cs View File

@@ -13,15 +13,47 @@ namespace HKControl

ConcurrentDictionary<int, Siemens> SiemensDicitonary = new ConcurrentDictionary<int, Siemens>();
ConcurrentDictionary<int, DataModel> DataModels = new ConcurrentDictionary<int, DataModel>();
ConcurrentQueue<int> CarQueue = new ConcurrentQueue<int>();


/// <summary>
/// 判断指定出餐窗口是否允许刷卡(1-6)
/// </summary>
/// <param name="CarNum"></param>
/// <returns></returns>
public bool GetIsSwipe(int CarNum)
{
var left = DataModels.Values.FirstOrDefault(p => p.LeftWindowData.CarNum == CarNum.ToString());
var right = DataModels.Values.FirstOrDefault(p => p.RightWindowData.CarNum == CarNum.ToString());
switch (CarNum)
{
case 1:
return DataModels[1].LeftWindowData.IsSwipe;
case 2:
return DataModels[1].RightWindowData.IsSwipe;
case 3:
return DataModels[2].LeftWindowData.IsSwipe;
case 4:
return DataModels[2].RightWindowData.IsSwipe;
case 5:
return DataModels[3].LeftWindowData.IsSwipe;
case 6:
return DataModels[3].LeftWindowData.IsSwipe;
default:
break;
}
return false;
}

/// <summary>
/// 设置指定窗口开始出餐(1--6)
/// </summary>
/// <param name="CarNum"></param>
public void Start(int CarNum)
{
CarQueue.Enqueue(CarNum);
}

/// <summary>
/// 初始化
/// </summary>
public void Init()
{
DataInit();
@@ -31,19 +63,58 @@ namespace HKControl
if (!SiemensDicitonary.ContainsKey(item.DeviceNum)) { SiemensDicitonary.TryAdd(item.DeviceNum, new Siemens()); }
ThreadManage.GetInstance().Start(new Action(() =>
{
SiemensDicitonary[item.DeviceNum].Connect(CpuType.S71200, item.IpAddress);
SiemensDicitonary[item.DeviceNum].Connect(CpuType.S7200Smart, item.IpAddress);
SiemensDicitonary[item.DeviceNum].ConnectOk = new Action(() =>
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
var vattable = DataModels[item.DeviceNum];
DataModels[item.DeviceNum] = SiemensDicitonary[item.DeviceNum].ReadClass<DataModel>(0, 1);
DataModels[item.DeviceNum].LeftWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");
DataModels[item.DeviceNum].LeftWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");

DataModels[item.DeviceNum].RightWindowData.IsSwipe = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");
DataModels[item.DeviceNum].RightWindowData.Complete = SiemensDicitonary[item.DeviceNum].Read<bool>("M0.0");

Thread.Sleep(100);
}), $"{item.DeviceNum} 号设备监听");
});
}), $"{item.DeviceNum} 号设备连接初始化");

});
StartRun();
}

private void StartRun()
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
while (CarQueue.Count > 0)
{
if (CarQueue.TryDequeue(out int carNum))
{
switch (carNum)
{
case 1:
case 2:
SiemensDicitonary[1].Write("M0.0", true);
break;

case 3:
case 4:
SiemensDicitonary[2].Write("M0.0", true);
break;

case 5:
case 6:
SiemensDicitonary[3].Write("M0.0", true);
break;

default:
break;
}
}
}
Thread.Sleep(100);
}), "开始运行设备");
}

private void DataInit()


+ 4
- 4
HKControl/Siemens.cs View File

@@ -20,7 +20,7 @@ namespace HKControl
/// <param name="port">plc 端口号</param>
/// <param name="rack">PLC 机架号</param>
/// <param name="solt"> PLC 插槽号</param>
public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 0)
public void Connect(CpuType cpuType, string ip, int port = 102, short rack = 0, short solt = 2)
{
myPlc = new Plc(cpuType, ip, port, rack, solt);
myPlc.Open();
@@ -34,10 +34,10 @@ namespace HKControl
myPlc?.Close();
}

public object Read(string address)
public TResult Read<TResult>(string address)
{
if (!IsConnected) return default;
return myPlc?.Read(address);
return (TResult)myPlc?.Read(address);
}

public bool[] ReadBools(int address, int count)
@@ -70,7 +70,7 @@ namespace HKControl
return myPlc?.Read(dataType, db, address, varType, count);
}

public void Write(string address, object value)
public void Write<TValue>(string address, TValue value)
{
myPlc?.Write(address, value);
}


Loading…
Cancel
Save