Commit 265d3dda 刘韬

优化声波传感器 检测间隔

1 个父辈 ab493891
......@@ -20,21 +20,20 @@ public class DauxiKS107Controller
System.Timers.Timer timer;
public volatile int Distance;
public bool IsRunning { get => timer.Enabled; }
public DauxiKS107Controller(int Elapsms=5000)
public DauxiKS107Controller(int Elapsms = 5000)
{
timer = new System.Timers.Timer(Elapsms);
timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true;
timer.Enabled = false;
}
~DauxiKS107Controller() {
LogUtil.info($"{comPortName}液位传感器退出");
~DauxiKS107Controller()
{
LogUtil.info($"{comPortName}声波传感器退出");
timer.Enabled = false;
if (_serialPort != null) {
_serialPort.Close();
}
_serialPort?.Close();
}
List<int> distantlist=new List<int>() { 1000, 100, 100, 100, 10 };
List<int> distantlist = new List<int>() { 1000, 100, 100, 100, 10 };
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
bool readok = true;
......@@ -42,7 +41,7 @@ public class DauxiKS107Controller
{
if (Quary(out int value, out string errmsg))
{
Debug.WriteLine($"{comPortName}读取到液位:{value}mm,err:{errmsg}");
Debug.WriteLine($"{comPortName}读取到声波距离:{value}mm,err:{errmsg}");
readok = true;
if (value > 0 && value<500)
{
......@@ -54,29 +53,33 @@ public class DauxiKS107Controller
{
distantlist.RemoveAt(0);
}
Distance= TrimMean(distantlist, 0.8);
Distance = TrimMean(distantlist, 0.8);
}
return;
}
else
readok = false;
}
else {
else
{
readok = false;
}
}
if (!readok)
{
LogUtil.info($"{comPortName}液位传感器读取失败,重新打开");
if (!OpenPort(comPortName, out string errmsg)) {
LogUtil.info($"{comPortName}液位传感器打开失败:"+ errmsg);
LogUtil.info($"{comPortName}声波传感器读取失败,重新打开");
if (!OpenPort(comPortName, out string errmsg))
{
LogUtil.info($"{comPortName}声波传感器打开失败:" + errmsg);
}
}
}
/// <summary>
/// 打开串口资源
/// <returns>返回bool类型</returns>
/// </summary>
public bool OpenPort(string _comport,out string errmsg)
public bool OpenPort(string _comport, out string errmsg)
{
comPortName = _comport;
......@@ -84,23 +87,32 @@ public class DauxiKS107Controller
errmsg = "";
if (_serialPort == null)
{
_serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits);
_serialPort.RtsEnable = true; //自动请求
_serialPort.ReadTimeout = 100;//超时
try
{
_serialPort = new SerialPort(comPortName, baudRate, parity, dataBits, stopBits);
_serialPort.RtsEnable = true; //自动请求
_serialPort.ReadTimeout = 100;//超时
}
catch (Exception e)
{
errmsg = "串口打开失败:" + e;
return false;
}
}
errmsg += "初始化.";
if (_serialPort.IsOpen)
_serialPort.Close();
bool ok = false;
try
{
//打开串口
errmsg += "打开串口.";
errmsg += $"打开串口:{comPortName}.";
_serialPort.Open();
for (int i = 1; i < 10; i++) {
for (int i = 1; i < 10; i++)
{
Thread.Sleep(100);
errmsg += "检测数据.";
Quary(out int value, out string err);
......@@ -115,22 +127,23 @@ public class DauxiKS107Controller
}
catch (Exception Ex)
{
errmsg = Ex.ToString();
errmsg += Ex.ToString();
//throw Ex;
}
return ok;
}
static byte[] quarycommand = new byte[] { 0xe8, 0x02, 0xb0 };
public bool Quary(out int value, out string errmsg) {
public bool Quary(out int value, out string errmsg)
{
errmsg = "";
byte[] buf = new byte[8];
value = 0;
try
{
_serialPort.Write(quarycommand, 0, quarycommand.Length);
Thread.Sleep(10);
Thread.Sleep(80);
var readlen = _serialPort.Read(buf, 0, buf.Length);
if (readlen >= 2)
if (readlen == 2)
{
var c = new byte[] { buf[1], buf[0] };
value = (int)BitConverter.ToUInt16(c, 0);
......@@ -141,7 +154,8 @@ public class DauxiKS107Controller
return false;
}
}
catch (Exception ex){
catch (Exception ex)
{
errmsg = ex.ToString();
return false;
}
......@@ -161,11 +175,11 @@ public class DauxiKS107Controller
templist.RemoveAt(0);
templist.RemoveAt(templist.Count - 1);
return (int)templist.Average();
}catch (Exception ex)
}
catch (Exception ex)
{
LogUtil.error(ex.ToString());
return 0;
}
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!