123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Ips.DevAlgorithm
- {
- /// <summary>
- /// 4通道变频器
- /// </summary>
- public class CH4BPQ
- {
- readonly string rem_on = "<REM_ON\r";
- readonly string cfreq = "<CF{0}_{1}\r";
- readonly int tryCount = 3; //控制失败后的尝试次数
- SerialPortDevice _device;
- public CH4BPQ(SerialPortDevice device)
- {
- _device = device;
- }
- /// <summary>
- /// 打开远控
- /// </summary>
- /// <param name="errmsg"></param>
- /// <returns></returns>
- bool RemOn(ref String errmsg)
- {
- bool result;
- if (_device.SendCommand(rem_on, ref errmsg))
- {
- string result2 = _device.GetOutString();
- result = result2.Contains("REM_ON");
- }
- else
- {
- result = false;
- }
- return result;
- }
- /// <summary>
- /// 设置一个频点
- /// </summary>
- /// <param name="ch"></param>
- /// <param name="freq"></param>
- /// <param name="errmsg"></param>
- /// <returns></returns>
- public bool SetFreq(int ch, double freq, ref String errmsg)
- {
- String cmd = String.Format(cfreq, ch, freq.ToString("0000.000"));
- bool result = false;
- for (int idx = 0; idx < tryCount; ++idx)
- {
- if (_device.SendCommand(cmd, ref errmsg))
- {
- string result2 = _device.GetOutString();
- result = result2.Contains("CF");
- }
- else
- {
- result = false;
- errmsg = $"{cmd}:{errmsg}";
- }
- if (result)
- {
- break;
- }
- //控制失败 继续尝试
- Thread.Sleep(500);
- }
- return result;
- }
- public bool SetFreqs(double[] freqs, ref String errmsg)
- {
- if (freqs.Length == 0)
- {
- return false;
- }
- if (!RemOn(ref errmsg))
- {
- return false;
- }
- Thread.Sleep(100);
- bool ret = true;
- for (int idx = 0; idx < freqs.Length; ++idx)
- {
- if (freqs[idx] <= 0.01) continue;
- if (!SetFreq(idx + 1, freqs[idx], ref errmsg))
- {
- ret = false;
- }
- }
- return ret;
- }
- }
- }
|