终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

93 righe
3.5 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using BPA.Helper;
  8. using BPASmartClient.Modbus;
  9. using BPASmartClient.SmallBatchingSystem;
  10. namespace BPASmartClient.SmallBatchingSystem
  11. {
  12. public class PlcServer
  13. {
  14. private volatile static PlcServer _Instance;
  15. public static PlcServer GetInstance => _Instance ?? (_Instance = new PlcServer());
  16. private PlcServer() { }
  17. public ModbusTcp Communication = new ModbusTcp();
  18. public void Connect()
  19. {
  20. MessageLog.GetInstance.Show($"开始连接设备,【IP = {Json<CommunicationPar>.Data.Host} 】,【 Port = {Json<CommunicationPar>.Data.Port} 】");
  21. Communication.IsReconnect = true;
  22. Communication.ConnectOk = new Action(() =>
  23. {
  24. MessageLog.GetInstance.Show("设备连接成功");
  25. ThreadManage.GetInstance().StartLong(new Action(() =>
  26. {
  27. //带称程序
  28. //var res = ReadData("M20.0", 1);//到达目标位置信号
  29. //if (res != null && res is bool[] bools && bools.Length == 1)
  30. //{
  31. // PlcDataModel.TargetLocComplete = bools[0];
  32. //}
  33. //var res1 = ReadData("M4.0", 1);//通道出料完成
  34. //if (res1 != null && res1 is bool[] bools1 && bools1.Length == 1)
  35. //{
  36. // PlcDataModel.BatchingCompleted = bools1[0];
  37. //}
  38. //var res2 = ReadData("M13.3", 1);//配方配料完成,托盘在放碗或取碗位置
  39. //if (res2 != null && res2 is bool[] bools2 && bools2.Length == 1)
  40. //{
  41. // PlcDataModel.RecipeBatchingComplete = bools2[0];
  42. //}
  43. //定时程序
  44. var res = ReadData("M20.0", 1);//到达目标位置信号
  45. if (res != null && res is bool[] bools && bools.Length == 1)
  46. {
  47. PlcDataModel.TargetLocComplete = bools[0];
  48. }
  49. var res1 = ReadData("M10.0", 1);//通道出料完成
  50. if (res1 != null && res1 is bool[] bools1 && bools1.Length == 1)
  51. {
  52. PlcDataModel.BatchingCompleted = bools1[0];
  53. }
  54. var res2 = ReadData("M13.3", 1);//配方配料完成,托盘在放碗或取碗位置
  55. if (res2 != null && res2 is bool[] bools2 && bools2.Length == 1)
  56. {
  57. PlcDataModel.RecipeBatchingComplete = bools2[0];
  58. }
  59. Thread.Sleep(10);
  60. }), "PLC 数据监听");
  61. });
  62. Communication.ModbusTcpConnect(Json<CommunicationPar>.Data.Host, Json<CommunicationPar>.Data.Port);
  63. }
  64. public void WriteData(string address, object value)
  65. {
  66. if (Communication.Connected)
  67. {
  68. if (address != null && value != null)
  69. Communication.Write(address, value);
  70. }
  71. }
  72. public object ReadData(string address, int length)
  73. {
  74. if (!Communication.Connected) return default;
  75. object res = null;
  76. if (address != null)
  77. res = Communication.Read(address, (ushort)length);
  78. return res;
  79. }
  80. }
  81. }