123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using MECF.Framework.Simulator.Core.Driver;
- namespace Venus_Simulator.Devices
- {
- class PendulumValveMockPMD : SerialPortDeviceSimulator
- {
- public enum PendulumValveStatus
- {
- Unknown,
- OFF,
- ON,
- ERROR
- }
- private const string EOF = "\r";
- private const char MSG_DELIMITER = '_';
- private const string MOCKUP_PORT = "COM79";
- private int _pressure = 120;
- private int _position = 500;
- private bool _bHold = false;
- private Random _rd = new Random();
- PendulumValveStatus _simPendulumValveStatus;
- public PendulumValveMockPMD() : base(MOCKUP_PORT, -1, EOF, MSG_DELIMITER)
- {
- _simPendulumValveStatus = PendulumValveStatus.Unknown;
- }
- protected override void ProcessUnsplitMessage(string message)
- {
- if (string.IsNullOrEmpty(message))
- throw new ArgumentException("Hardware command message is invalid");
- var items = message.Split('\r');
- foreach (var item in message.Split('\r'))
- {
- if (item == "")
- {
- continue;
- }
- string item2 = item + "\r";
- if (_bHold == false)
- {
- //_pressure += _rd.Next(-10, 10);
- //_position += _rd.Next(-50, 50);
- }
- if (_simPendulumValveStatus == PendulumValveStatus.OFF)
- {
- _position = 0;
- }
- string sRes = string.Empty;
- string str = item2.Trim();
- switch (str)
- {
- case "P:":
- sRes = string.Format("P:{0:D8}\r\n", _pressure);
- break;
- case "A:":
- sRes = string.Format("A:{0:D6}\r\n", _position);
- break;
- case "C:":
- _bHold = false;
- _simPendulumValveStatus = PendulumValveStatus.OFF;
- sRes = "C:\r\n";
- break;
- case "O:":
- _bHold = false;
- _simPendulumValveStatus = PendulumValveStatus.ON;
- sRes = "O:\r\n";
- break;
- case "H:":
- _bHold = true;
- sRes = "H:\r\n";
- break;
- case "i:30":
- string data = _simPendulumValveStatus == PendulumValveStatus.OFF ? "3" : "4";
- sRes = $"i:301{data}100000\r\n";
- break;
- case "i:02A04":
- sRes = $"i:02B041000.1\r\n";
- break;
- case "i:02A05":
- sRes = $"i:02B051001.2\r\n";
- break;
- default:
- {
- if (str.Contains("S:"))
- {
- int pressure;
- if (int.TryParse(str.Substring(2, 8), out pressure))
- {
- _pressure = pressure;
- }
- _bHold = false;
- sRes = "S:\r\n";
- }
- else if (str.StartsWith("R:"))
- {
- int position;
- if (int.TryParse(str.Substring(2, 6), out position))
- {
- _position = position;
- }
- _bHold = false;
- sRes = "R:\r\n";
- }
- else
- return;
- }
- break;
- }
- OnWriteMessage(sRes);
- }
- }
- }
- }
|