| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 | using System;using System.Diagnostics;using System.Collections.Generic;using System.Collections.Concurrent;using System.Text.RegularExpressions;using MECF.Framework.Common.Communications;using MECF.Framework.Common.Equipment;using Aitex.Core.RT.SCCore;using Aitex.Core.RT.Event;using Aitex.Core.RT.Device;using Aitex.Core.RT.Log;using Venus_Core;using MECF.Framework.Common.Device.Bases;namespace Venus_RT.Devices{    class AdixenTurboPump : PumpBase    {        public enum Operation        {            SetAddress,            GetChecksum,            GetFaultList,            SetDatalogInterval,            EnableDatalog,            EnableEcho,            DisableEcho,            IdentifyDevice,            IdentifyProduct,            GetCurrentValues,            SwitchSpeedToNormal,            SwitchSpeedToStandbyValue,            SetStandbySpeed,            GetParametersState,            GetIntervalParametersState,            SetPumpWorkingTime,            SetElectronicWorkingTime,            SetStartDelay,            SetTimeToVent,            SetVentingTime,            SetSpeedThreshold,            SetControlTemperatrue,            StopPumpThermostage,            SetBearingThreshold,            GetCurrentSpeed,            StartPumpRotation,            StopPump,            SetVersions,            SetAnalogOutput,            SetTemperatureUnit,            SetBuzzerOpt,            SetCommandMode,            SetBraking,            Invalid,        }        private readonly Dictionary<Operation, string> _noneParaCommandOp = new Dictionary<Operation, string>        {            {Operation.EnableDatalog,               "#{0:D3}DLR\r" },            {Operation.EnableEcho,                  "#{0:D3}ECHON\r" },            {Operation.DisableEcho,                 "#{0:D3}ECHOFF\r" },            {Operation.SwitchSpeedToNormal,         "#{0:D3}NSP\r" },            {Operation.SwitchSpeedToStandbyValue,   "#{0:D3}SBY\r" },            {Operation.StopPumpThermostage,         "#{0:D3}SET31,30\r" },            {Operation.StartPumpRotation,           "#{0:D3}TMPON\r" },            {Operation.StopPump,                    "#{0:D3}TMPOFF\r" },        };        private readonly Dictionary<Operation, string> _readDataCommandOp = new Dictionary<Operation, string>        {            {Operation.GetChecksum,                 "#{0:D3}CHKVS\r" },            {Operation.GetFaultList,                "#{0:D3}DEF\r" },            {Operation.IdentifyDevice,              "#{0:D3}IDN\r" },            {Operation.IdentifyProduct,             "#{0:D3}IDP\r" },            {Operation.GetCurrentValues,            "#{0:D3}LEV10\r" },            {Operation.GetParametersState,          "#{0:D3}SEL10\r" },            {Operation.GetCurrentSpeed,             "#{0:D3}SPD\r" },            {Operation.GetIntervalParametersState,  "#{0:D3}STA\r" },        };        private readonly Dictionary<Operation, string> _setDataCommandOp = new Dictionary<Operation, string>        {            {Operation.SetAddress,                  "#{0:D3}ADR {1:D3}\r" },            {Operation.SetDatalogInterval,          "#{0:D3}DLI {1:D3}\r" },            {Operation.SetStandbySpeed,             "#{0:D3}RPM,{1:D4}\r" },            {Operation.SetPumpWorkingTime,          "#{0:D3}SET10,{1:D5}\r"  },            {Operation.SetElectronicWorkingTime,    "#{0:D3}SET11,{1:D5}\r"  },            {Operation.SetStartDelay,               "#{0:D3}SET13,{1:D5}\r" },            {Operation.SetTimeToVent,               "#{0:D3}SET14,{1:D5}\r" },            {Operation.SetVentingTime,              "#{0:D3}SET15,{1:D4}\r" },            {Operation.SetSpeedThreshold,           "#{0:D3}SET30,{1:D2}\r" },            {Operation.SetControlTemperatrue,       "#{0:D3}SET31,{1:D2}\r" },            {Operation.SetBearingThreshold,         "#{0:D3}SET32,{1:D3}\r" },            {Operation.SetAnalogOutput,             "#{0:D3}OPT01,{1:D1}\r" },            {Operation.SetTemperatureUnit,          "#{0:D3}OPT02,{1:D1}\r" },            {Operation.SetBuzzerOpt,                "#{0:D3}OPT11,{1:D1}\r" },            {Operation.SetCommandMode,              "#{0:D3}OPT14,{1:D1}\r" },            {Operation.SetBraking,                  "#{0:D3}OPT25,{1:D1}\r" },        };        private readonly Dictionary<string, string> _comError = new Dictionary<string, string>        {            {"Err0",  "adjustment error(out of bounds)" },            {"Err1",  "command error (syntax) " },            {"Err2",  "parameter error (e.g. non hexadecimal character)" },            {"Err3",  "context error" },            {"Err4",  "checksum error" },        };        private readonly ushort PUMP_ON_FLAG            = 0x02;        private readonly ushort PUMP_ERROR_FLAG         = 0x40;        private readonly ushort ISO_VALVE_OPEN_FLAG     = 0x01;        private readonly ushort WATER_VALVE_OEPN_FLAG   = 0x08;        private readonly ushort VALVE_AT_SPEED_FLAG     = 0x40;        public override bool IsRunning        {            get            {                return (_pumpStatus & PUMP_ON_FLAG) == PUMP_ON_FLAG;            }        }        public override bool IsError        {            get            {                return (_pumpStatus & PUMP_ERROR_FLAG) == PUMP_ERROR_FLAG;            }        }        public bool IsISOValveOpen        {            get            {                return (_valveStatus & ISO_VALVE_OPEN_FLAG) == ISO_VALVE_OPEN_FLAG;            }        }        public bool IsWaterValveOpen        {            get            {                return (_valveStatus & WATER_VALVE_OEPN_FLAG) == WATER_VALVE_OEPN_FLAG;            }        }        public bool AtSpeed        {            get            {                return (_valveStatus & VALVE_AT_SPEED_FLAG) == VALVE_AT_SPEED_FLAG;            }        }        public string sTemperature        {            get { return Temperature.ToString(); }        }        public string RotationalSpeed        {            get { return Speed.ToString(); }        }        private readonly int _timeout = 2000;        private readonly int _address = 0;        private readonly int _internal_parameters_length = 78;        private readonly AsyncSerialPort _serial;        private Stopwatch _queryWatch = new Stopwatch();        private Stopwatch _responseWatch = new Stopwatch();        private readonly int _readInterval = 1000;        private ConcurrentQueue<Operation> _queCommands = new ConcurrentQueue<Operation>();        private Operation _lastCommand = Operation.Invalid;        private string _lastAlarmString = string.Empty;        private Regex _rex_ok = new Regex(@"#\d{1,3},OK");        private Regex _rex_err = new Regex(@"#\d{1,3},Err");        private byte _pumpStatus = 0;        private byte _valveStatus = 0;        public AdixenTurboPump(ModuleName mod)        {            Name = VenusDevice.TurboPump.ToString();            Module = mod.ToString();            var _PortNum = SC.GetStringValue($"{mod}.TurboPump.Port");            _serial = new AsyncSerialPort(_PortNum, 9600, 8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One, "\r", false);            //SerachCommandList = new List<string>()            //{            //SkyPumpMessage.READ_DATA            //};            //sendDataChangedEvent += AdixenTurboPump_sendDataChangedEvent;            //baseStopwatch.Start();            //baseTimer.Enabled = true;        }        private void AdixenTurboPump_sendDataChangedEvent(string obj)        {                    }        public override bool Initialize()        {            if (!_serial.Open())            {                LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Adixen Turbo Pump 串口无法打开");                return false;            }            _serial.OnDataChanged += OnPortDataChanged;            _serial.OnBinaryDataChanged += OnPortBinaryDataChanged;            _serial.OnErrorHappened += OnErrorOccurred;            _queryWatch.Start();            return true;        }        public override void Monitor()        {            if(_queryWatch.ElapsedMilliseconds > _readInterval)            {                _queryWatch.Restart();                if (_lastCommand == Operation.Invalid)                {                    if (_queCommands.IsEmpty)                    {                        _sendCommand(Operation.GetIntervalParametersState);                    }                    else                    {                        Operation command;                        if (_queCommands.TryDequeue(out command))                        {                            _sendCommand(command);                            _lastCommand = command;                        }                    }                }                else if (_responseWatch.ElapsedMilliseconds > _timeout)                {                    _serial.ClearPortBuffer();                    _lastCommand = Operation.Invalid;                }            }        }        public override void Reset()        {            Operation result;            while (_queCommands.TryDequeue(out result)) ;            //_serial.ClearPortBuffer();            _lastCommand = Operation.Invalid;        }        public override void Terminate()        {            _serial?.Close();        }        private void OnErrorOccurred(string obj)        {            _noRepeatAlarm($"[{Module}] Adixen Turbo Pump serial port error: [{obj}]");        }        private void OnPortDataChanged(string obj)        {            try            {                _lastCommand = Operation.Invalid;                if (obj.Substring(0, 2) == "OK")                {                    return;                }                if(obj.Substring(0, 3) == "Err")                {                    if (_comError.ContainsKey(obj.Substring(0, 4)))                    {                        _noRepeatAlarm($"Adixen Turbo Pump communication error: {_comError[obj.Substring(0, 4)]}");                    }                    else                    {                        _noRepeatAlarm($"Adixen Turbo Pump unknown communication error: {obj}");                    }                    return;                }                var result_data = obj.Trim().Split(',');                if (result_data.Length >= 9)                {                    int speed = 0;                    int temp = 0;                    if (int.TryParse(result_data[0], out speed))                    {                        Speed = speed;                    }                    if (int.TryParse(result_data[8], out temp))                    {                        Temperature = temp;                    }                }            }            catch (Exception ex)            {                _noRepeatAlarm($"[{Module}] Adixen Turbo Pump error: [{ex.Message}], Data: {obj}");            }        }        private void OnPortBinaryDataChanged(byte[] obj)        {            bool bValidate = false;            for(int i=0; i< obj.Length; i++)            {                if(obj[i] == (byte)'#' && obj.Length > i + 5 && obj[i+4] == (byte)',')                {                    int nOffset = 5;                    if(obj.Length - i >= _internal_parameters_length)                    {                        _pumpStatus = obj[i + 6];                        _valveStatus = obj[i + 7];                        nOffset = 9;                    }                                        byte[] array = new byte[obj.Length - i - nOffset];                    Array.Copy(obj, i + nOffset, array, 0, obj.Length - i - nOffset);                    var strData = System.Text.Encoding.Default.GetString(array);                    OnPortDataChanged(strData);                    bValidate = true;                    break;                }            }            if(!bValidate)            {                var strData = System.Text.Encoding.Default.GetString(obj);                _noRepeatAlarm($"Receive invalidate data:{strData} ");            }        }        public override void SetPumpOnOff(bool on)        {            var _chamber = DEVICE.GetDevice<JetPMBase>(Module);            if (on && !_chamber.TurboPumpInterlock)            {                LOG.Write(eEvent.ERR_TURBO_PUMP, Module, "Cannot Run Turbo Pump for Turbo Pump Interlock(DI-9) is off.");                return ;            }            _queCommands.Enqueue(on ? Operation.StartPumpRotation : Operation.StopPump);        }        private bool _sendCommand(Operation op)        {            if (_noneParaCommandOp.ContainsKey(op))            {                var cmd = string.Format(_noneParaCommandOp[op], _address);                return _sendCmd(cmd);            }            else if (_readDataCommandOp.ContainsKey(op))            {                var cmd = string.Format(_readDataCommandOp[op], _address);                return _sendCmd(cmd);            }            _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");            return false;        }        private bool _sendCommand(Operation op, int data)        {            if (_setDataCommandOp.ContainsKey(op))            {                var cmd = string.Format(_setDataCommandOp[op], _address, data);                return _sendCmd(cmd);            }            _noRepeatAlarm($"Adixen Turbo Pump: The command {op} does not need one parameter");            return false;        }        private bool _sendCmd(string cmd)        {            _responseWatch.Restart();            return _serial.Write(cmd);        }        private void _noRepeatAlarm(string alarm)        {            if(_lastAlarmString != alarm)            {                _lastAlarmString = alarm;                LOG.Write(eEvent.ERR_TURBO_PUMP, Module, alarm);            }        }    }}
 |