123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using Aitex.Core.Common.DeviceData;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.RT.Tolerance;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Communications;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.Common.Utilities;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using static Mono.Security.X509.X520;
- using Venus_Core;
- using System.Diagnostics;
- namespace Venus_RT.Devices
- {
-
-
-
-
-
-
-
-
- public enum ChillerMsgType
- {
- Get,
- Set
- }
- public class SMCChillerDrive
- {
- public SMCChillerState StatusSMC { get; set; }
- private readonly string _PortNum = "COM92";
- private readonly AsyncSerialPort _serial;
- BlockingCollection<string> blockingCollection = new BlockingCollection<string>();
- public string PortName => _PortNum;
- private Dictionary<string,string> _address2Module;
- private Dictionary<string, ChillerData> _addressQuery;
- private Dictionary<string, Queue<string>> _addressCmdQueue;
- private ModuleName Module;
-
-
- public SMCChillerDrive(string Port)
- {
- _PortNum = Port;
- StatusSMC = SMCChillerState.Unknown;
- _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", true);
- if (!_serial.Open())
- {
- StatusSMC = SMCChillerState.Disconnected;
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"SMC Chiller串口:{_PortNum}无法打开");
- }
- StatusSMC = SMCChillerState.Connected;
- _serial.OnDataChanged += OnPortDataChanged;
- _serial.OnErrorHappened += OnErrorOccurred;
- _address2Module = new Dictionary<string, string>();
- _addressQuery = new Dictionary<string, ChillerData>();
- _addressCmdQueue = new Dictionary<string, Queue<string>>();
- Task.Run(() =>
- {
- foreach (var data in blockingCollection.GetConsumingEnumerable())
- {
- _serial?.Write(data);
- System.Threading.Thread.Sleep(500);
- }
- });
- }
- private void OnPortDataChanged(string obj)
- {
- try
- {
- if (SC.GetValue<bool>("System.IsSimulatorMode"))
- {
-
-
-
- return;
- }
- if (string.IsNullOrEmpty(obj))
- {
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller message IsNullOrEmpty");
- return;
- }
- string cmd = obj.Split(new char[] { '\r', '\n' })[0];
-
-
- if (ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(cmd.Substring(1, cmd.Length - 3))).ToString("X2") !=
- cmd.Substring(cmd.Length - 2))
- {
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, Module, $"[{_PortNum}] smc chiller message LRC check error");
- return;
- }
-
-
- ModuleName module = ModuleName.System;
-
- string header = cmd.Substring(1, 2);
- if (_address2Module.ContainsKey(header) && _addressQuery.ContainsKey(header))
- {
- module = ModuleHelper.Converter(_address2Module[header].Split('.')[0]);
-
- }
- else
- {
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}]:address => {header} not in Dictionary");
- return;
- }
- switch (cmd.Substring(3, 2))
- {
- case "03":
- if (cmd.Substring(5, 2) == "16")
- {
- ChillerData chillerdata = new ChillerData();
- for (int iRegisterNo = 0; iRegisterNo < 11; iRegisterNo++)
- {
- string sBuf = cmd.Substring(4 * iRegisterNo + 7, 4);
- int iBuf = 0;
- for (int i = 3; i > -1; i--)
- {
- iBuf += Convert.ToInt32(sBuf.Substring(i, 1), 16) * (int)Math.Pow(16, 3 - i);
- }
- switch (iRegisterNo)
- {
- case 0:
- if (iBuf > 32767)
- {
- iBuf -= 65536;
- }
- chillerdata.ControlTcFeedback = (float)iBuf / 10;
- break;
- case 4:
- for (int i = 0; i < 16; i++)
- {
- chillerdata.Statusflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
- }
- break;
- case 5:
- for (int i = 0; i < 16; i++)
- {
- chillerdata.Alarmflag[i] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
- }
- break;
- case 6:
- for (int i = 0; i < 16; i++)
- {
- chillerdata.Alarmflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
- }
- break;
- case 7:
- for (int i = 0; i < 16; i++)
- {
- chillerdata.Alarmflag[i + 32] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
- }
- break;
- case 9:
- for (int i = 0; i < 16; i++)
- {
- chillerdata.Statusflag[i + 16] = Convert.ToBoolean((int)Math.Pow(2, i) & iBuf);
- }
- break;
- }
- }
- _addressCmdQueue[header].Dequeue();
- _addressQuery[header] = chillerdata;
- }
- break;
- case "06":
- break;
- }
- }
- catch (Exception ex)
- {
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{ex.Message}]");
- }
- }
- private void OnErrorOccurred(string obj)
- {
- StatusSMC = SMCChillerState.ERROR;
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] smc chiller error: [{obj}]");
- }
- public void SendCmd(ChillerMsgType type,string str)
- {
-
-
-
- if (type == ChillerMsgType.Set)
- {
- blockingCollection.Add(":" + str + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
- }
-
- if (type == ChillerMsgType.Get)
- {
-
-
- _addressCmdQueue[str.Substring(0, 2)].Enqueue(str);
- if (_addressCmdQueue[str.Substring(0, 2)].Count > 1)
- _addressCmdQueue[str.Substring(0, 2)].Dequeue();
- else
- blockingCollection.Add(":" + _addressCmdQueue[str.Substring(0, 2)].Peek() + ModbusUtility.CalculateLrc(ModbusUtility.HexToBytes(str)).ToString("X2") + "\r\n");
- }
- }
- public void AddAddress(string address, string chillertype)
- {
-
- _address2Module.Add(address,chillertype);
- _addressQuery.Add(address, new ChillerData());
- _addressCmdQueue.Add(address, new Queue<string>());
- }
- public ChillerData QueryData(string address)
- {
- if (_addressQuery.ContainsKey(address))
- {
- return _addressQuery[address];
- }
- else
- {
- LOG.Write(eEvent.ERR_DEVICE_CHILLER, ModuleName.System, $"[{_PortNum}] => address:{address} not exist");
- return null;
- }
- }
- public void Terminate()
- {
- _serial?.Close();
- }
- }
- public class ChillerData
- {
- public bool[] Statusflag {get; set;}
- public bool[] Alarmflag { get; set; }
- public float ControlTcFeedback { get; set; }
- public ChillerData()
- {
- Statusflag = new bool[32];
- Alarmflag = new bool[48];
- ControlTcFeedback = 0.0f;
- }
- }
- }
|