|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using BPA.Helper;
- using BPASmartClient.Modbus;
- using BPASmartClient.SmallBatchingSystem;
-
- namespace BPASmartClient.SmallBatchingSystem
- {
- public class PlcServer
- {
- private volatile static PlcServer _Instance;
- public static PlcServer GetInstance => _Instance ?? (_Instance = new PlcServer());
- private PlcServer() { }
-
- public ModbusTcp Communication = new ModbusTcp();
-
- public void Connect()
- {
- MessageLog.GetInstance.Show($"开始连接设备,【IP = {Json<CommunicationPar>.Data.Host} 】,【 Port = {Json<CommunicationPar>.Data.Port} 】");
- Communication.IsReconnect = true;
- Communication.ConnectOk = new Action(() =>
- {
- MessageLog.GetInstance.Show("设备连接成功");
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- //带称程序
- var res = ReadData("M20.0", 1);//到达目标位置信号
- if (res != null && res is bool[] bools && bools.Length == 1)
- {
- PlcDataModel.TargetLocComplete = bools[0];
- }
-
- var res1 = ReadData("M4.0", 1);//通道出料完成
- if (res1 != null && res1 is bool[] bools1 && bools1.Length == 1)
- {
- PlcDataModel.BatchingCompleted = bools1[0];
- }
-
- var res2 = ReadData("M13.3", 1);//配方配料完成,托盘在放碗或取碗位置
- if (res2 != null && res2 is bool[] bools2 && bools2.Length == 1)
- {
- PlcDataModel.RecipeBatchingComplete = bools2[0];
- }
-
- //定时程序
- //var res = ReadData("M20.0", 1);//到达目标位置信号
- //if (res != null && res is bool[] bools && bools.Length == 1)
- //{
- // PlcDataModel.TargetLocComplete = bools[0];
- //}
-
- //var res1 = ReadData("M10.0", 1);//通道出料完成
- //if (res1 != null && res1 is bool[] bools1 && bools1.Length == 1)
- //{
- // PlcDataModel.BatchingCompleted = bools1[0];
- //}
-
- //var res2 = ReadData("M13.3", 1);//配方配料完成,托盘在放碗或取碗位置
- //if (res2 != null && res2 is bool[] bools2 && bools2.Length == 1)
- //{
- // PlcDataModel.RecipeBatchingComplete = bools2[0];
- //}
-
- Thread.Sleep(10);
- }), "PLC 数据监听");
- });
- Communication.ModbusTcpConnect(Json<CommunicationPar>.Data.Host, Json<CommunicationPar>.Data.Port);
- }
-
- public void WriteData(string address, object value)
- {
- if (Communication.Connected)
- {
- if (address != null && value != null)
- Communication.Write(address, value);
- }
- }
-
- public object ReadData(string address, int length)
- {
- if (!Communication.Connected) return default;
- object res = null;
- if (address != null)
- res = Communication.Read(address, (ushort)length);
- return res;
- }
- }
- }
|