using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Xml; using Aitex.Core.Common.DeviceData; using Aitex.Core.RT.DataCenter; using Aitex.Core.RT.Device; using Aitex.Core.RT.Event; using Aitex.Core.RT.IOCore; using Aitex.Core.RT.Log; using Aitex.Core.RT.OperationCenter; using Aitex.Core.RT.SCCore; using Aitex.Core.RT.Tolerance; using Aitex.Core.Util; using MECF.Framework.Common.Communications; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Device.Bases; using MECF.Framework.Common.Equipment; using Venus_Core; using static System.ValueTuple; namespace Venus_RT.Devices { class KashiyamaPump : PumpBase { public enum KashiyamaPumpState { ON = 0, OFF, Connected, Disconnected, Unknown, ERROR } private readonly Dictionary _JIS7_CODE = new Dictionary { {0, 0x30 }, {1, 0x31 }, {2, 0x32 }, {3, 0x33 }, {4, 0x34 }, {5, 0x35 }, {6, 0x36 }, {7, 0x37 }, {8, 0x38 }, {9, 0x39 }, {10,0x41 }, {11,0x42 }, {12, 0x43 }, {13, 0x44 }, {14, 0x45 }, {15, 0x46 }, }; private readonly byte STX = 0x02; private readonly string _PortNum = "COM91"; private readonly AsyncSerialPort _serial; public KashiyamaPumpState StatusDry { get; set; } public KashiyamaPump(ModuleName mod) : base(mod.ToString(), VirgoDevice.MainPump.ToString(), "Kashiyama Pump", "") { _PortNum = SC.GetStringValue($"{mod}.DryPump.Port"); StatusDry = KashiyamaPumpState.Unknown; _serial = new AsyncSerialPort(_PortNum, 9600, 7, System.IO.Ports.Parity.Even, System.IO.Ports.StopBits.One, "\r\n", false); } public override bool Initialize() { base.Initialize(); if (!_serial.Open()) { StatusDry = KashiyamaPumpState.Disconnected; EV.PostAlarmLog(this.Module, "Kashiyama Pump串口无法打开"); return false; } StatusDry = KashiyamaPumpState.Connected; _serial.OnBinaryDataChanged += OnPortBinaryDataChanged; return true; } private (int hiFCS, int loFCS) CaculateFCS(byte[] data, int length) { int hiFCS = 0, loFCS = 0; for (int i = 0; i < data.Length; i++) { hiFCS ^= data[i] >> 4; loFCS ^= data[i] ^ 0x0F; } return (hiFCS, loFCS); } private (bool bValid, byte command) FCSValidation(byte [] data) { if (data[0] != STX || data.Length < 7) { return (false, 0); } var fcs = CaculateFCS(data, data.Length - 2); if ((_JIS7_CODE[fcs.hiFCS] != data[data.Length - 4]) || (_JIS7_CODE[ fcs.loFCS] != data[data.Length - 3])) { return (false, 0); } return (true, data[1]); } private void OnPortBinaryDataChanged(byte[] obj) { var result = FCSValidation(obj); if(result.bValid) { switch(result.command) { case 0x42: // 'B': status break; case 0x58: // 'X': analog data break; } } else { if (!SC.GetValue("System.IsSimulatorMode")) { LOG.Info($"[{Module}] Kashiyama Pump 数据无效:[{System.Text.Encoding.ASCII.GetString(obj)}]"); } } } static IEnumerable GenerateSequence ( TState seed, Func Generator, Func resultSelector) { var state = seed; while (true) { yield return resultSelector(state); state = Generator(state); } } void test() { var fibonacci = GenerateSequence( (current: 0, next: 1), pair => (pair.next, pair.current + pair.next), pair => pair.current); } } }