123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- 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 MECF.Framework.Common.Device.Bases;
- namespace Venus_RT.Devices
- {
- class AdixenTurboPump : TurboPump
- {
- 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 int _readInterval = 500;
- private readonly int _readTimeout = 2000;
- private readonly int _address = 0;
- private readonly AsyncSerialPort _serial;
- private Stopwatch _queryWatch = new Stopwatch();
- private Operation _lastReadCommand = 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");
- public AdixenTurboPump(ModuleName mod)
- {
- 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);
- }
- public override bool Initialize()
- {
- if (!_serial.Open())
- {
- EV.PostAlarmLog(this.Module, "Adixen Turbo Pump 串口无法打开");
- return false;
- }
- _serial.OnDataChanged += OnPortDataChanged;
- _serial.OnErrorHappened += OnErrorOccurred;
- return true;
- }
- public override void Monitor()
- {
- if ((_queryWatch.ElapsedMilliseconds > _readInterval && _lastReadCommand == Operation.Invalid) || _queryWatch.ElapsedMilliseconds > _readTimeout)
- {
- SendCommand(Operation.GetCurrentValues);
- _queryWatch.Restart();
- }
- }
- public override void Reset()
- {
- }
- 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)
- {
- if (string.IsNullOrEmpty(obj))
- {
- _noRepeatAlarm("Adixen Turbo Pump receive empty message");
- return;
- }
- try
- {
- if (_rex_ok.Match(obj).Success)
- return;
- if(_rex_err.Match(obj).Success)
- {
- var errs = obj.Trim().Split(',');
- if(_comError.ContainsKey(errs[1]))
- {
- _noRepeatAlarm($"Adixen Turbo Pump communication error: {_comError[errs[1]]}");
- }
- else
- {
- _noRepeatAlarm($"Adixen Turbo Pump unknown communication error: {obj}");
- }
- _lastReadCommand = Operation.Invalid;
- return;
- }
- if(_lastReadCommand == Operation.Invalid)
- {
- EV.PostWarningLog(Module, $"Adixen Turbo Pump unexpected communication data: {obj}");
- return;
- }
- var result_data = obj.Trim().Split(',');
- switch(_lastReadCommand)
- {
- case Operation.GetCurrentSpeed:
- var str_speed = result_data[1].Split();
- Speed = int.Parse(str_speed[0]);
- break;
- }
- _lastReadCommand = Operation.Invalid;
- }
- catch (Exception ex)
- {
- _noRepeatAlarm($"[{Module}] Adixen Turbo Pump error: [{ex.Message}], Data: {obj}");
- }
- }
- public bool SendCommand(Operation op)
- {
- if (_noneParaCommandOp.ContainsKey(op))
- {
- var cmd = string.Format(_noneParaCommandOp[op], _address);
- return _serial.Write(cmd);
- }
- else if (_readDataCommandOp.ContainsKey(op))
- {
- var cmd = string.Format(_readDataCommandOp[op], _address);
- if(_serial.Write(cmd))
- {
- _lastReadCommand = op;
- return true;
- }
- }
- _noRepeatAlarm($"Adixen Turbo Pump: The {op} command need parameters");
- return false;
- }
- public 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)
- {
- return _serial.Write(cmd);
- }
- private void _noRepeatAlarm(string alarm)
- {
- if(_lastAlarmString != alarm)
- {
- _lastAlarmString = alarm;
- EV.PostAlarmLog(Module, alarm);
- }
- }
- }
- }
|