123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System;
- using System.Diagnostics;
- using System.Timers;
- using MECF.Framework.Simulator.Core.Driver;
- namespace MECF.Framework.Simulator.Core.Light
- {
- class SimLightPdc : SerialPortDeviceSimulator
- {
- public bool Failed { get; set; }
- public bool IsPumpOn { get; set; }
- public bool IsOverTemp { get; set; }
- public bool IsAtSpeed { get; set; }
- Stopwatch _timer = new Stopwatch();
- //private System.Timers.Timer _tick;
- Random _rd = new Random();
- private object _locker = new object();
- public string ResultValue { get; set; }
- public SimLightPdc(string port)
- : base(port, -1, "\r", ' ',false)
- {
- ResultValue = "";
-
- }
- private void _tick_Elapsed(object sender, ElapsedEventArgs e)
- {
-
- lock (_locker)
- {
- if (IsAtSpeed)
- {
- //if (_timer.Elapsed > TimeSpan.FromSeconds(10))
- //{
- string text = "0011039806" + _rd.Next(0, 999999).ToString("D6");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- }
- //}
- //if (_timer.IsRunning && _timer.Elapsed > TimeSpan.FromSeconds(10))
- //{
- // _timer.Stop();
- // IsAtSpeed = true;
- //}
- }
-
-
- }
- protected override void ProcessUnsplitMessage(byte[] msg)
- {
- byte[] message = new byte[] { 0x40, 0x03, 0x01, 0x00, 0x00, 0x44 };
- OnWriteMessage(message);
- }
- protected override void ProcessUnsplitMessage(string msg)
- {
- //if (!Failed)
- {
- if (msg == "0010030302=?101\r")
- {
- //OnWriteMessage("0011030306000000014" + "\r" );
- string text = "0011030306" + (Failed ? _rd.Next(0, 999).ToString("D6") : "000000");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- if (msg == "0010039802=?115\r")
- {
- string text = "0011039806" + _rd.Next(0, 999999).ToString("D6");
-
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- if (msg == "0010034602=?108\r")
- {
- string text = "0011034606" + _rd.Next(0, 999999).ToString("D6");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- if (msg == "0011001006111111015\r")
- {
- lock (_locker)
- {
- if (!IsPumpOn)
- {
- IsPumpOn = true;
- IsAtSpeed = false;
- _timer.Restart();
- }
- }
- OnWriteMessage("0011001006111111015" + "\r");
- return;
- }
-
- if (msg == "0011001006000000009\r")
- {
- lock (_locker)
- {
- if (IsPumpOn)
- {
- IsPumpOn = false;
- IsAtSpeed = false;
- _timer.Restart();
- }
- }
- OnWriteMessage("0011001006000000009" + "\r");
- return;
- }
- if (msg == "0010001002=?096\r")
- {
- string text = "0011001006" + (IsPumpOn ? "111111" : "000000");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- if (msg == "0010030502=?103\r")
- {
- string text = "0011030506" + (IsOverTemp ? "111111" : "000000");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- if (msg == "0010030602=?104\r")
- {
- string text = "0011030606" + (IsAtSpeed ? "111111" : "000000");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- if (msg == "0010030702=?105\r")
- {
- string text = "0011030706" + (IsAtSpeed ? "000000" : "111111");
- int sum = 0;
- foreach (var item in text)
- {
- sum += (int)item;
- }
- sum = sum % 256;
- OnWriteMessage(text + sum.ToString("D3") + "\r");
- return;
- }
- }
-
- }
-
-
- }
- }
|