添加middleware
This commit is contained in:
555
middleware/zdhsys/Device/Dev_BG.cs
Normal file
555
middleware/zdhsys/Device/Dev_BG.cs
Normal file
@@ -0,0 +1,555 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.MemoryMappedFiles;
|
||||
using System.IO;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using System.Windows.Forms;
|
||||
using EasyModbus;
|
||||
using System.IO.Pipes;
|
||||
using System.Net.Sockets;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
|
||||
namespace zdhsys
|
||||
{
|
||||
internal partial class Dev_BG
|
||||
{
|
||||
|
||||
public Dev_BG()
|
||||
{
|
||||
sub_dev_Vave = new bg_in_out_water();
|
||||
}
|
||||
|
||||
public bg_in_out_water sub_dev_Vave;
|
||||
|
||||
public string chaoshengspd = "set_mixer_speed 15 ";//设置搅拌转速;
|
||||
public string chaoshengpower = "set_mixer_pow 15 ";//设置超声功率;
|
||||
|
||||
public string mixstart = "mixer_start 15\r\n";
|
||||
public string mixend = "mixer_stop 15\r\n";
|
||||
|
||||
public string tempset = "set_pid_temp 1 ";//设置温度
|
||||
public string tempstart = "pid_enable 1 1";//开始温控
|
||||
public string tempstop = "pid_enable 1 0";//开始温控
|
||||
|
||||
public string ComPortName = "COM3";
|
||||
SerialPort serialPort_Dev_BG;
|
||||
|
||||
public bool openPort()
|
||||
{
|
||||
serialPort_Dev_BG = new SerialPort(ComPortName, 115200, Parity.None, 8, StopBits.One);
|
||||
try
|
||||
{
|
||||
serialPort_Dev_BG.Open();
|
||||
// throw new Exception();
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBox.Show(e.Message.ToString());
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Thread lisn_th;
|
||||
public void start_lsn()
|
||||
{
|
||||
lisn_th = new Thread(lispro);
|
||||
lisn_th.Start();
|
||||
}
|
||||
|
||||
public string rcvdata;
|
||||
bool checking = false;
|
||||
private void lispro()
|
||||
{
|
||||
//byte[] receiveData = new byte[serialPort1.BytesToRead];//接收到的数据 serialPort1.Read(receiveData, 0, receiveData.Length);//读取数据
|
||||
while (serialPort_Dev_BG.IsOpen)
|
||||
{
|
||||
Thread.Sleep(100);
|
||||
if (checking)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (serialPort_Dev_BG.BytesToRead > 0)
|
||||
{
|
||||
rcvdata = serialPort_Dev_BG.ReadLine();
|
||||
Console.Write(rcvdata); // 输出到控制台
|
||||
//textBox1.Text = data;
|
||||
// textBox1.Lines = data.Split('\n');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class bg_dev_status_cfg
|
||||
{
|
||||
public int TotalSTA;//0 before initial; 1 initialed and idle; 2 working; 3 finish; 4 error
|
||||
public int [] pumpsta;//1号泵 对应1-4路
|
||||
public int [] pumpsta_curspd;
|
||||
// public int pump2sta;//2号泵 对应5-8路,
|
||||
// public int pump3sta;//3号泵 对应9-12路
|
||||
|
||||
public int [] pumpVavsta;//1号泵阀门,进1 出0
|
||||
// public int pump2Vavsta;//2号泵阀门,进1 出0
|
||||
// public int pump3Vavsta;//3号泵阀门,进1 出0
|
||||
|
||||
public int motorUDsta;//电机上下状态,上0, 下1
|
||||
public int motorFBsta;//电机前后状态,后0,前1
|
||||
|
||||
public int[] sonarSta;//12路超声状态
|
||||
public int[] sonarPowSet;//12路超声功率设置
|
||||
public int[] sonarSpeedSet;//12路超声速度设置
|
||||
|
||||
public int[] heatSta;//12路加热状态
|
||||
public int[] heatTempSet;//12路加热温度设置
|
||||
|
||||
public int[] waterLevLow;//清洗低位
|
||||
public int[] waterLevHigh;//清洗高位
|
||||
|
||||
public int pumpSpd; //齿轮泵速度
|
||||
public int pumpSpdHigh;//齿轮泵高速
|
||||
public int pumpSpdLow;//齿轮泵低速度
|
||||
|
||||
public int greepPumpSpd;//蠕动泵速度
|
||||
|
||||
public bg_dev_status_cfg()
|
||||
{
|
||||
pumpsta = new int[3];
|
||||
pumpsta_curspd = new int[3];
|
||||
pumpVavsta = new int[3];
|
||||
|
||||
sonarSta = new int[12];
|
||||
sonarPowSet = new int[12];
|
||||
heatSta = new int[12];
|
||||
heatTempSet = new int[12];
|
||||
waterLevLow = new int[12];
|
||||
waterLevHigh = new int[12];
|
||||
sonarSpeedSet = new int[12];
|
||||
|
||||
pumpSpd = 500;//默认500
|
||||
|
||||
for(int i=0;i<3;i++)
|
||||
{
|
||||
pumpsta[i] = 0;
|
||||
pumpsta_curspd[i] = 0;
|
||||
pumpVavsta[i]= 0;
|
||||
}
|
||||
|
||||
for(int i=0;i<12;i++)
|
||||
{
|
||||
sonarSta[i] = 0;
|
||||
sonarPowSet[i]= 20;
|
||||
sonarSpeedSet[i] = 100;
|
||||
heatSta[i] = 0;
|
||||
heatTempSet[i] = 60;
|
||||
waterLevHigh[i] = 55;
|
||||
}
|
||||
}
|
||||
|
||||
public void loadfromcfg()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public bg_dev_status_cfg m_Status_cfg = new bg_dev_status_cfg();//当前设备状态与参数
|
||||
|
||||
public void motorliftup()
|
||||
{
|
||||
string cmd1 = "motor_dir_set 1 0\r\n";
|
||||
byte[] array1 = System.Text.Encoding.ASCII.GetBytes(cmd1);
|
||||
serialPort_Dev_BG.Write(array1, 0, array1.Length);
|
||||
Thread.Sleep(500);
|
||||
|
||||
string cmd2 = "motor1_2_run\r\n";
|
||||
byte[] array2 = System.Text.Encoding.ASCII.GetBytes(cmd2);
|
||||
serialPort_Dev_BG.Write(array2, 0, array2.Length);
|
||||
|
||||
m_Status_cfg.motorUDsta = 0;
|
||||
}
|
||||
|
||||
public void motorliftDowm()
|
||||
{
|
||||
string cmd1 = "motor_dir_set 1 1\r\n";
|
||||
byte[] array1 = System.Text.Encoding.ASCII.GetBytes(cmd1);
|
||||
serialPort_Dev_BG.Write(array1, 0, array1.Length);
|
||||
Thread.Sleep(500);
|
||||
|
||||
string cmd2 = "motor1_2_run\r\n";
|
||||
byte[] array2 = System.Text.Encoding.ASCII.GetBytes(cmd2);
|
||||
serialPort_Dev_BG.Write(array2, 0, array2.Length);
|
||||
|
||||
m_Status_cfg.motorUDsta = 1;
|
||||
}
|
||||
|
||||
public void motorfront()
|
||||
{
|
||||
string cmd1 = "motor_dir_set 2 1\r\n";
|
||||
byte[] array1 = System.Text.Encoding.ASCII.GetBytes(cmd1);
|
||||
serialPort_Dev_BG.Write(array1, 0, array1.Length);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
string cmd2 = "motor3_4_run\r\n";
|
||||
byte[] array2 = System.Text.Encoding.ASCII.GetBytes(cmd2);
|
||||
serialPort_Dev_BG.Write(array2, 0, array2.Length);
|
||||
|
||||
m_Status_cfg.motorFBsta = 1;
|
||||
}
|
||||
public void motorback()
|
||||
{
|
||||
string cmd1 = "motor_dir_set 2 0\r\n";
|
||||
byte[] array1 = System.Text.Encoding.ASCII.GetBytes(cmd1);
|
||||
serialPort_Dev_BG.Write(array1, 0, array1.Length);
|
||||
Thread.Sleep(1000);
|
||||
|
||||
string cmd2 = "motor3_4_run\r\n";
|
||||
byte[] array2 = System.Text.Encoding.ASCII.GetBytes(cmd2);
|
||||
serialPort_Dev_BG.Write(array2, 0, array2.Length);
|
||||
|
||||
m_Status_cfg.motorFBsta = 0;
|
||||
}
|
||||
|
||||
//node : 1, 2
|
||||
//chn: 1-6,7
|
||||
//temp = 60
|
||||
public void set_temp(int node,int chn, int val)
|
||||
{
|
||||
string cmd = "set_pid_temp "+node.ToString()+" "+chn.ToString()+" "+val.ToString()+ "\r\n";
|
||||
// string cmd = tempset + " " + val.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
|
||||
//node : 1, 2
|
||||
//chn: 1-6,7
|
||||
//action = 1
|
||||
public void start_temp(int node, int chn)
|
||||
{
|
||||
string cmd ="pid_enable " + node.ToString()+ " " +chn.ToString()+ " 1\r\n";
|
||||
// string cmd = tempstart + " " + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
//node : 1, 2
|
||||
//chn: 1-6
|
||||
//action = 0
|
||||
public void stop_temp(int node, int chn)
|
||||
{
|
||||
string cmd = "pid_enable " + node.ToString() + " " + chn.ToString() + " 0\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
|
||||
//超声控制
|
||||
//测试代码,全开,15为全部发送
|
||||
public void set_sonar_rotationspd(int spd)
|
||||
{
|
||||
string cmd = chaoshengspd + " " + spd.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
}
|
||||
|
||||
public void set_sonar_mixpower(int pow)
|
||||
{
|
||||
string cmd = chaoshengpower + " " + pow.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
}
|
||||
|
||||
public void start_sonar()
|
||||
{
|
||||
string cmd0 = "set_mixer_pow 15 20\r\n";
|
||||
byte[] array0 = System.Text.Encoding.ASCII.GetBytes(cmd0);
|
||||
serialPort_Dev_BG.Write(array0, 0, array0.Length);
|
||||
Thread.Sleep(500);
|
||||
string cmd = mixstart + " 15 " + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
}
|
||||
|
||||
public void stop_sonar()
|
||||
{
|
||||
string cmd = mixend + " 15 " + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
}
|
||||
|
||||
//测试代码 独立开
|
||||
//说明:独立ID和实际ID关系待查
|
||||
//
|
||||
public void set_sonar_rotationspd(int ID, int spd)
|
||||
{
|
||||
string cmd = "set_mixer_speed "+ ID.ToString() + " " + spd.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
|
||||
public void set_sonar_mixpower(int ID, int pow)
|
||||
{
|
||||
string cmd = "set_mixer_pow " + ID.ToString() + " " + pow.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
|
||||
public void start_sonar(int ID)
|
||||
{
|
||||
string cmd = mixstart + " "+ID.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
}
|
||||
public void stop_sonar(int ID)
|
||||
{
|
||||
string cmd = mixend + " "+ID.ToString() + "\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//超声控制
|
||||
|
||||
|
||||
|
||||
public void runpump(int chn, int action, int speed)//启动停止泵,注意!泵2和3是反的,这里加了修正by jgl20240124
|
||||
{
|
||||
int realchn;
|
||||
if (chn == 2) { realchn = 3; } //2号和3号是反的
|
||||
else if (chn == 3) { realchn = 2; }//2号和3号是反的
|
||||
else if (chn == 1) { realchn = 1; }//1号是对的
|
||||
else { return; }//通道错误,不发数据
|
||||
string cmd = "run_gear_pump "+ realchn.ToString()+" " + action.ToString() + " " + speed.ToString() + " \r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
|
||||
//状态记录采用原来的
|
||||
m_Status_cfg.pumpsta_curspd[chn - 1] = speed;
|
||||
m_Status_cfg.pumpsta[chn - 1] = action;
|
||||
}
|
||||
|
||||
public void start_pump_out(int chn, int spd)//设置阀门为出水
|
||||
{
|
||||
string cmd = "run_valve_test "+chn.ToString()+" 0\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
// runpump(1,2000);
|
||||
m_Status_cfg.pumpVavsta[chn - 1] = 0;
|
||||
Thread.Sleep(1000);
|
||||
|
||||
}
|
||||
public void start_pump_in(int chn,int spd)//设置阀门为进水
|
||||
{
|
||||
string cmd = "run_valve_test " + chn.ToString() + " 1\r\n";
|
||||
byte[] array = System.Text.Encoding.ASCII.GetBytes(cmd);
|
||||
serialPort_Dev_BG.Write(array, 0, array.Length);
|
||||
m_Status_cfg.pumpVavsta[chn - 1] = 1;
|
||||
Thread.Sleep(1000);
|
||||
|
||||
}
|
||||
|
||||
public void start_pump(int chn,int spd)//启动泵,注意!泵2和3是反的,这里加了修正by jgl20240124
|
||||
{
|
||||
/* int realchn;
|
||||
if(chn ==2){ realchn = 3; } //2号和3号是反的
|
||||
else if(chn==3){ realchn = 2; }//2号和3号是反的
|
||||
else if(chn==1){ realchn = 1; }//1号是对的
|
||||
else { return; }//通道错误,不发数据
|
||||
Thread.Sleep(1000);
|
||||
string cmd1 = "run_gear_pump " + realchn.ToString() + " 1 " + spd.ToString() + "\r\n";
|
||||
byte[] array1 = System.Text.Encoding.ASCII.GetBytes(cmd1);
|
||||
serialPort_Dev_BG.Write(array1, 0, array1.Length);*/
|
||||
runpump(chn,1, spd);
|
||||
}
|
||||
|
||||
public ushort get_sta()
|
||||
{
|
||||
checking = true;
|
||||
Thread.Sleep(500);
|
||||
string cmd1 = "switchScan \r\n";
|
||||
byte[] array1 = System.Text.Encoding.ASCII.GetBytes(cmd1);
|
||||
serialPort_Dev_BG.Write(array1, 0, array1.Length);
|
||||
string data;
|
||||
ushort podsta = 0;
|
||||
|
||||
int cnt = 0;
|
||||
int rcv = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (serialPort_Dev_BG.BytesToRead > 0)
|
||||
{
|
||||
|
||||
data = serialPort_Dev_BG.ReadLine();
|
||||
|
||||
bool result = ushort.TryParse(data, out podsta); //i now = 108
|
||||
rcv++;
|
||||
/// if(rcv == 2)
|
||||
//{
|
||||
if(result)
|
||||
{
|
||||
podsta = ushort.Parse(data);
|
||||
break;
|
||||
}
|
||||
|
||||
// Console.Write(rcvdata); // 输出到控制台
|
||||
}
|
||||
cnt++;
|
||||
if(cnt >30) {
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
|
||||
Thread.Sleep(500);
|
||||
checking = false;
|
||||
return podsta;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void stopPump(int chn)
|
||||
{
|
||||
runpump(chn,0, 0);
|
||||
}
|
||||
|
||||
public int cycles = 1;
|
||||
public int status = 0;
|
||||
Thread run_th;
|
||||
public bool isrunning;
|
||||
|
||||
public void start_run()
|
||||
{
|
||||
isrunning = true;
|
||||
run_th = new Thread(runProcess);
|
||||
run_th.Start();
|
||||
// run_th.Join();
|
||||
}
|
||||
|
||||
public void stop_run()
|
||||
{
|
||||
if (run_th.IsAlive)
|
||||
{
|
||||
run_th.Abort();
|
||||
}
|
||||
Thread.Sleep(1000);
|
||||
StopALL_VAV();
|
||||
Thread.Sleep(1000);
|
||||
stop_sonar();
|
||||
status = 4;
|
||||
Thread.Sleep(1000);
|
||||
motorliftup();//X到位
|
||||
status = 6;
|
||||
Thread.Sleep(1000);
|
||||
motorback();
|
||||
Thread.Sleep(5 * 1000);
|
||||
status = 0;
|
||||
m_Status_cfg.TotalSTA = 1;
|
||||
|
||||
}
|
||||
|
||||
public void initialdev()
|
||||
{
|
||||
m_Status_cfg.TotalSTA = 2;
|
||||
StopALL_VAV();
|
||||
Thread.Sleep(1000);
|
||||
stop_sonar();
|
||||
// status = 4;
|
||||
Thread.Sleep(1000);
|
||||
motorliftup();//X到位
|
||||
// status = 6;
|
||||
Thread.Sleep(1000);
|
||||
motorback();
|
||||
Thread.Sleep(10 * 1000);
|
||||
// status = 0;
|
||||
m_Status_cfg.TotalSTA = 1;
|
||||
}
|
||||
|
||||
|
||||
public void runProcess()
|
||||
{
|
||||
status = 1;
|
||||
motorliftDowm();//到位
|
||||
Thread.Sleep(25 * 1000);
|
||||
status = 2;
|
||||
for (int i = 0; i < cycles; i++)
|
||||
{
|
||||
//搅拌
|
||||
start_sonar();
|
||||
status = 3;
|
||||
Thread.Sleep(10 * 1000);
|
||||
//吸附
|
||||
stop_sonar();
|
||||
status = 4;
|
||||
motorfront();
|
||||
Thread.Sleep(45 * 1000);
|
||||
runOutWaterAuto_duli(1,500,30);
|
||||
Thread.Sleep(5 * 1000);
|
||||
runInWaterAuto_duli(1,500,50);
|
||||
motorback();
|
||||
Thread.Sleep(15 * 1000);
|
||||
start_sonar();
|
||||
Thread.Sleep(10 * 1000);
|
||||
stop_sonar();
|
||||
// moveY_dir(true);//Y下降
|
||||
// Thread.Sleep(10 * 1000);
|
||||
// Thread.Sleep(10 * 1000);
|
||||
//进出水
|
||||
// start_pump_in();
|
||||
// Thread.Sleep(10 * 1000);
|
||||
// start_pump_out();//进水
|
||||
// Thread.Sleep(10 * 1000);
|
||||
// stopPump();
|
||||
// moveY_dir(false);//Y下降
|
||||
}
|
||||
Thread.Sleep(2 * 1000);
|
||||
motorliftup();//X到位
|
||||
status = 6;
|
||||
Thread.Sleep(30 * 1000);
|
||||
status = 0;
|
||||
isrunning = false;
|
||||
|
||||
m_Status_cfg.TotalSTA = 3;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void online_ctl_initial()
|
||||
{
|
||||
// stop_run();
|
||||
Thread th;
|
||||
th = new Thread(initialdev);
|
||||
th.Start();
|
||||
}
|
||||
|
||||
public void online_ctl_run()
|
||||
{
|
||||
if (m_Status_cfg.TotalSTA != 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_Status_cfg.TotalSTA = 2;
|
||||
// stop_run();
|
||||
start_run();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
869
middleware/zdhsys/Device/Dev_BG_sub.cs
Normal file
869
middleware/zdhsys/Device/Dev_BG_sub.cs
Normal file
@@ -0,0 +1,869 @@
|
||||
using EasyModbus;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace zdhsys
|
||||
{
|
||||
internal partial class Dev_BG
|
||||
{
|
||||
private Process pythonprocess;
|
||||
|
||||
public void stopPythonDet()
|
||||
{
|
||||
// 当您想要终止 Python 脚本时
|
||||
if (!pythonprocess.HasExited)
|
||||
{
|
||||
pythonprocess.Kill(); // 终止 Python 进程
|
||||
}
|
||||
|
||||
Console.WriteLine("Python 进程已终止");
|
||||
}
|
||||
|
||||
public void startPythonDet()
|
||||
{
|
||||
// Python 解释器的路径
|
||||
string pythonInterpreter = @"D:\python311\python.exe"; // 替换为您的 Python 解释器路径
|
||||
|
||||
// Python 脚本的路径
|
||||
string scriptPath = @"D:\botdet\databot\bot.py";
|
||||
|
||||
// 创建并启动 Python 进程
|
||||
ProcessStartInfo start = new ProcessStartInfo
|
||||
{
|
||||
FileName = pythonInterpreter,
|
||||
Arguments = scriptPath,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
CreateNoWindow = true
|
||||
};
|
||||
|
||||
pythonprocess = new Process { StartInfo = start };
|
||||
pythonprocess.Start();
|
||||
|
||||
// 在这里添加代码,根据您的需求执行其他任务
|
||||
// 例如,等待一段时间
|
||||
// Thread.Sleep(10000); // 等待10秒
|
||||
|
||||
// 当您想要终止 Python 脚本时
|
||||
// if (!pythonprocess.HasExited)
|
||||
// {
|
||||
// pythonprocess.Kill(); // 终止 Python 进程
|
||||
// }
|
||||
|
||||
// Console.WriteLine("Python 进程已终止");
|
||||
}
|
||||
|
||||
|
||||
public void getCmdfromUDP()
|
||||
{
|
||||
// UDP 端口
|
||||
int UDP_PORT = 5006;
|
||||
// 创建一个 UdpClient 用于读取传入的数据
|
||||
UdpClient udpClient = new UdpClient(UDP_PORT);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// 获取发送方的 IP 和端口
|
||||
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
// 接收来自发送方的数据
|
||||
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
|
||||
string returnData = Encoding.ASCII.GetString(receiveBytes);
|
||||
// 检查接收到的数据是否为空
|
||||
if (!string.IsNullOrWhiteSpace(returnData))
|
||||
{
|
||||
var receivedData = JsonConvert.DeserializeObject<Dictionary<int, List<object>>>(returnData);
|
||||
if (receivedData != null && receivedData.Count > 0)
|
||||
{
|
||||
// 处理接收到的数据
|
||||
// Console.WriteLine("Received: " + returnData);
|
||||
// DealWithCmd(receivedData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 处理空数据
|
||||
// Console.WriteLine("Received empty data");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Received an empty message");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void DealWithCmd(string receivedData) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class bg_in_out_water
|
||||
{
|
||||
IPEndPoint Vaves_Ctl_IP;//电磁阀的IP
|
||||
int Vaves_Ctl_IP_port = 8234;//电磁阀通信端口
|
||||
ModbusClient m_modbusClient;
|
||||
|
||||
public struct pod_ctl_st
|
||||
{
|
||||
public int podInPosSta;//瓶子在位状态
|
||||
public int waterInVaveSTA;//进阀门状态
|
||||
public int waterOutVaveSTA;//出阀门状态
|
||||
public float waterLeve;//液面高度百分比
|
||||
}
|
||||
|
||||
|
||||
public pod_ctl_st[] m_PodSTA = new pod_ctl_st[12];
|
||||
Thread th_readPod;
|
||||
|
||||
private void UpdatePodStatus(Dictionary<int, List<object>> bottleData)
|
||||
{
|
||||
foreach (var item in bottleData)
|
||||
{
|
||||
int bottleNumber = item.Key - 1; // 减1是因为数组索引从0开始
|
||||
var bottleInfo = item.Value;
|
||||
|
||||
// 更新瓶子状态
|
||||
m_PodSTA[bottleNumber].podInPosSta = Convert.ToInt32(bottleInfo[0]);
|
||||
m_PodSTA[bottleNumber].waterLeve = Convert.ToSingle(bottleInfo[1]);
|
||||
|
||||
// Console.WriteLine($"Bottle {bottleNumber + 1}: InPosSta = {m_PodSTA[bottleNumber].podInPosSta}, WaterLevel = {m_PodSTA[bottleNumber].waterLeve}");
|
||||
}
|
||||
}
|
||||
|
||||
public void getpodstafromUDP()
|
||||
{
|
||||
// UDP 端口
|
||||
int UDP_PORT = 5005;
|
||||
// 创建一个 UdpClient 用于读取传入的数据
|
||||
UdpClient udpClient = new UdpClient(UDP_PORT);
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
// 获取发送方的 IP 和端口
|
||||
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
|
||||
// 接收来自发送方的数据
|
||||
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
|
||||
string returnData = Encoding.ASCII.GetString(receiveBytes);
|
||||
// 检查接收到的数据是否为空
|
||||
if (!string.IsNullOrWhiteSpace(returnData))
|
||||
{
|
||||
var receivedData = JsonConvert.DeserializeObject<Dictionary<int, List<object>>>(returnData);
|
||||
if (receivedData != null && receivedData.Count > 0)
|
||||
{
|
||||
// 处理接收到的数据
|
||||
// Console.WriteLine("Received: " + returnData);
|
||||
UpdatePodStatus(receivedData);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 处理空数据
|
||||
// Console.WriteLine("Received empty data");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Received an empty message");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bg_in_out_water() //初始化函数
|
||||
{
|
||||
// 创建Modbus客户端实例
|
||||
m_modbusClient = new ModbusClient("192.168.1.7", 8234); // 替换为你的设备IP地址和端口号
|
||||
m_modbusClient.Connect();
|
||||
Console.WriteLine("Connected to device");
|
||||
GetVaves_ALL_status();
|
||||
// 写入单个线圈(例如,地址1,状态true)
|
||||
Thread.Sleep(1000);
|
||||
for (int i = 1; i <= 24; i++)
|
||||
{
|
||||
SetVaves_status(i, 0);
|
||||
}
|
||||
|
||||
Console.WriteLine("Coil at address 1 written.");
|
||||
|
||||
th_readPod = new Thread(getpodstafromUDP);//创建读取线程
|
||||
th_readPod.Start();
|
||||
|
||||
}
|
||||
public bool GetVaves_ALL_status()
|
||||
{
|
||||
// 读取保持寄存器(例如,从地址0开始读取10个寄存器)
|
||||
int[] registers = m_modbusClient.ReadHoldingRegisters(0, 24); // 替换为你的寄存器地址和数量
|
||||
if (registers != null)
|
||||
{
|
||||
// Console.WriteLine("Read " + registers.Length + " registers:");
|
||||
int ID = 1;
|
||||
foreach (int register in registers)
|
||||
{
|
||||
// Console.WriteLine("ID=" + ID + " data=" + register);
|
||||
|
||||
// m_PodSTA[ID-1].waterOutVaveSTA = false;//更新阀门位置的函数
|
||||
// m_PodSTA[ID-1].waterInVaveSTA = false;
|
||||
ID++;
|
||||
}
|
||||
for (int i = 0; i < 12; i++)
|
||||
{
|
||||
ID = i + 1;
|
||||
m_PodSTA[i].waterInVaveSTA = registers[i * 2];
|
||||
m_PodSTA[i].waterOutVaveSTA = registers[i * 2 + 1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Failed to read registers.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
public bool SetVaves_status(int ID, int val)
|
||||
{
|
||||
if (ID <= 0 || ID > 24)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (val != 0 && val != 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
m_modbusClient.WriteSingleRegister(ID - 1, val);
|
||||
Console.WriteLine($"Coil at address {ID}={val} written.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
GetVaves_ALL_status();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool outWater(int ID, float dstvalue)//放水直到dst停止,ID从1开始-12
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool inWater(int ID, float dstvalue)//加水直到dstvalue停止,ID从1开始-12
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void pod1InWater(int pmpspd)
|
||||
{
|
||||
//阀门控制
|
||||
sub_dev_Vave.SetVaves_status(1, 1);
|
||||
//通道1开启
|
||||
start_pump_in(1, 0);
|
||||
Thread.Sleep(1000);
|
||||
start_pump(1, pmpspd);//泵控制
|
||||
}
|
||||
public void pod1InWaterStop()
|
||||
{
|
||||
stopPump(1);//泵控制
|
||||
Thread.Sleep(1000);
|
||||
//阀门控制
|
||||
sub_dev_Vave.SetVaves_status(1, 0);
|
||||
}
|
||||
public void pod1OutWater(int pmpspd)
|
||||
{
|
||||
//阀门控制
|
||||
sub_dev_Vave.SetVaves_status(2, 1);
|
||||
//通道1开启
|
||||
start_pump_out(1, 0);
|
||||
Thread.Sleep(1000);
|
||||
start_pump(1, pmpspd);//泵控制
|
||||
}
|
||||
public void pod1OutWaterStop()
|
||||
{
|
||||
stopPump(1);//泵控制
|
||||
Thread.Sleep(1000);
|
||||
//阀门控制
|
||||
sub_dev_Vave.SetVaves_status(2, 0);
|
||||
}
|
||||
|
||||
public float targetlvin = 50;
|
||||
public float targetlvout = 30;
|
||||
private void processinwater()
|
||||
{
|
||||
float currentlv = sub_dev_Vave.m_PodSTA[0].waterLeve;
|
||||
if (currentlv - targetlvin > 0)
|
||||
{
|
||||
Console.WriteLine("target reached! return!");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine("start in water!");
|
||||
pod1InWater(500);
|
||||
// this.pBVpod1pump.BackColor = Color.LightGreen;
|
||||
while (currentlv - targetlvin < -2)
|
||||
{
|
||||
currentlv = sub_dev_Vave.m_PodSTA[0].waterLeve;
|
||||
Thread.Sleep(100);//每隔100 检查一次
|
||||
}
|
||||
Console.WriteLine("in target reached, stop!");
|
||||
pod1InWaterStop();
|
||||
pod1OutWaterStop();
|
||||
//this.pBVpod1pump.BackColor = Color.DarkGray;
|
||||
|
||||
}
|
||||
Thread p1inth;
|
||||
public void runpod1InWater()
|
||||
{
|
||||
if (p1inth != null)
|
||||
{
|
||||
if (p1inth.IsAlive)
|
||||
{
|
||||
p1inth.Abort();
|
||||
p1inth = null;
|
||||
}
|
||||
}
|
||||
p1inth = new Thread(processinwater);
|
||||
p1inth.Start();
|
||||
}
|
||||
|
||||
public void runpod1OutWater()
|
||||
{
|
||||
if (p1inth != null)
|
||||
{
|
||||
if (p1inth.IsAlive)
|
||||
{
|
||||
p1inth.Abort();
|
||||
p1inth = null;
|
||||
}
|
||||
}
|
||||
p1inth = new Thread(processoutwater);
|
||||
p1inth.Start();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void processoutwater()
|
||||
{
|
||||
float currentlv = sub_dev_Vave.m_PodSTA[0].waterLeve;
|
||||
if (currentlv - targetlvout < 0)
|
||||
{
|
||||
Console.WriteLine("target reached! return!");
|
||||
return;
|
||||
}
|
||||
Console.WriteLine("start out water!");
|
||||
pod1OutWater(500);
|
||||
// this.pBVpod1pump.BackColor = Color.LightGreen;
|
||||
while (currentlv - targetlvout > 2)
|
||||
{
|
||||
currentlv = sub_dev_Vave.m_PodSTA[0].waterLeve;
|
||||
Thread.Sleep(100);//每隔100 检查一次
|
||||
}
|
||||
Console.WriteLine("out target reached, stop!");
|
||||
pod1InWaterStop();
|
||||
pod1OutWaterStop();
|
||||
//this.pBVpod1pump.BackColor = Color.DarkGray;
|
||||
}
|
||||
public int duliQuitFlag = 0;
|
||||
public void runInWaterAuto_duli(int ID, int pumpspeed,float targetLv)
|
||||
{
|
||||
if(ID<=0 || ID > 12) { return; }
|
||||
duliQuitFlag = 0;
|
||||
int i = ID - 1;
|
||||
float currentlv = sub_dev_Vave.m_PodSTA[i].waterLeve;
|
||||
float start_lv = currentlv;
|
||||
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
if (currentlv - targetLv > 0)
|
||||
{
|
||||
Console.WriteLine("ID="+ID.ToString()+" target reached"+ " "+targetLv.ToString() +" return!");
|
||||
return;
|
||||
}
|
||||
|
||||
//打开进水阀门
|
||||
int openvavID = 2 * i + 1;
|
||||
int zuPumpID = (int)(i / 4) + 1;
|
||||
Console.WriteLine("进水 ID=" + ID.ToString() + " 阀门=" + openvavID.ToString() + " 泵+阀=" + zuPumpID.ToString());
|
||||
|
||||
sub_dev_Vave.SetVaves_status(openvavID, 1);
|
||||
//切换进水组电磁阀
|
||||
start_pump_in(zuPumpID, 0);
|
||||
Thread.Sleep(500);
|
||||
//启动进水泵
|
||||
stopwatch.Start();
|
||||
start_pump(zuPumpID, pumpspeed);//泵控制
|
||||
|
||||
while (currentlv - targetLv < -2)
|
||||
{
|
||||
currentlv = sub_dev_Vave.m_PodSTA[i].waterLeve;
|
||||
Thread.Sleep(100);//每隔100 检查一次
|
||||
if(duliQuitFlag==1)
|
||||
{
|
||||
Console.WriteLine("inwater forced quit!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
stopwatch.Stop();
|
||||
TimeSpan elapsedTime = stopwatch.Elapsed;
|
||||
|
||||
Console.WriteLine($"ID={ID}, from lv={start_lv} to lv={targetLv} with pumpspeed{pumpspeed}, " +
|
||||
$"the time is{elapsedTime},out target reached, stop! ");
|
||||
//关闭进水泵
|
||||
stopPump(zuPumpID);//泵控制
|
||||
|
||||
Thread.Sleep(1000);
|
||||
//阀门控制
|
||||
sub_dev_Vave.SetVaves_status(openvavID, 0);
|
||||
//关闭阀门
|
||||
Console.WriteLine();
|
||||
|
||||
}
|
||||
|
||||
public int in159Flag = 0;
|
||||
//1 5 9 inwater
|
||||
public void runInWater159(int pumpspeed, float targetlv)
|
||||
{
|
||||
in159Flag = 0;
|
||||
float curlv1 = sub_dev_Vave.m_PodSTA[0].waterLeve;
|
||||
float curlv5 = sub_dev_Vave.m_PodSTA[4].waterLeve;
|
||||
float curlv9 = sub_dev_Vave.m_PodSTA[8].waterLeve;
|
||||
int[] addwaterSTA = new int[3];
|
||||
//int[] pumpSTA = new int[3];
|
||||
|
||||
addwaterSTA[0] = 0;
|
||||
addwaterSTA[1] = 0;
|
||||
addwaterSTA[2] = 0;
|
||||
|
||||
if(curlv1 - targetlv >0)
|
||||
{
|
||||
Console.WriteLine("ID= 1 target reached" + " " + curlv1.ToString() + " return!");
|
||||
addwaterSTA[0] = 1;
|
||||
}
|
||||
if (curlv5 - targetlv > 0)
|
||||
{
|
||||
Console.WriteLine("ID= 5 target reached" + " " + curlv5.ToString() + " return!");
|
||||
addwaterSTA[1] = 1;
|
||||
}
|
||||
if (curlv9 - targetlv > 0)
|
||||
{
|
||||
Console.WriteLine("ID= 9 target reached" + " " + curlv9.ToString() + " return!");
|
||||
addwaterSTA[2] = 1;
|
||||
}
|
||||
for(int i = 0;i<3;i++)
|
||||
{
|
||||
if (addwaterSTA[i] == 0)
|
||||
{
|
||||
int openvavID_in = 8 * i + 1;
|
||||
sub_dev_Vave.SetVaves_status(openvavID_in, 1);//设置进出水电磁阀
|
||||
Thread.Sleep(100);
|
||||
int zuPumpID = i +1;
|
||||
start_pump_in(zuPumpID, 0);//设置泵为进水
|
||||
Thread.Sleep(500);
|
||||
//
|
||||
}
|
||||
}
|
||||
//开起泵
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
if (addwaterSTA[i] == 0)
|
||||
{
|
||||
int zuPumpID = i + 1;
|
||||
//启动泵
|
||||
start_pump(zuPumpID, pumpspeed);//泵控制启动
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
//状态检测
|
||||
while (true)
|
||||
{
|
||||
curlv1 = sub_dev_Vave.m_PodSTA[0].waterLeve;
|
||||
curlv5 = sub_dev_Vave.m_PodSTA[4].waterLeve;
|
||||
curlv9 = sub_dev_Vave.m_PodSTA[8].waterLeve;
|
||||
|
||||
if ((curlv1 - targetlv > 0)&&(addwaterSTA[0] == 0))
|
||||
{
|
||||
Console.WriteLine("ID= 1 target reached" + " " + curlv1.ToString() + " return!");
|
||||
addwaterSTA[0] = 1;
|
||||
stopPump(1);//泵控制
|
||||
Thread.Sleep(500);
|
||||
sub_dev_Vave.SetVaves_status(1, 0);
|
||||
//关闭阀门
|
||||
}
|
||||
if ((curlv5 - targetlv > 0) && (addwaterSTA[1] == 0))
|
||||
{
|
||||
Console.WriteLine("ID= 5 target reached" + " " + curlv5.ToString() + " return!");
|
||||
addwaterSTA[1] = 1;
|
||||
stopPump(2);//泵控制
|
||||
Thread.Sleep(500);
|
||||
sub_dev_Vave.SetVaves_status(9, 0);
|
||||
//关闭阀门
|
||||
}
|
||||
if ((curlv9 - targetlv > 0)&& (addwaterSTA[2] == 0))
|
||||
{
|
||||
Console.WriteLine("ID= 9 target reached" + " " + curlv9.ToString() + " return!");
|
||||
addwaterSTA[2] = 1;
|
||||
stopPump(3);//泵控制
|
||||
Thread.Sleep(500);
|
||||
sub_dev_Vave.SetVaves_status(17, 0);
|
||||
//关闭阀门
|
||||
}
|
||||
if (addwaterSTA[1] + addwaterSTA[2] + addwaterSTA[0] >= 3)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void runOutWaterAuto_duli(int ID, int pumpspeed, float targetLv)
|
||||
{
|
||||
if (ID <= 0 || ID > 12) { return; }
|
||||
duliQuitFlag = 0;
|
||||
int i = ID - 1;
|
||||
float currentlv = sub_dev_Vave.m_PodSTA[i].waterLeve;
|
||||
|
||||
float start_lv = currentlv;
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
|
||||
if (currentlv - targetLv < 0)
|
||||
{
|
||||
Console.WriteLine("ID=" + ID.ToString() + " target reached" + " " + targetLv.ToString() + " return!");
|
||||
return;
|
||||
}
|
||||
//打开进水阀门
|
||||
int openvavID_out = 2 * ID;
|
||||
int zuPumpID = (int)(i / 4) + 1;
|
||||
Console.WriteLine("出水 ID=" + ID.ToString() + " 阀门=" + openvavID_out.ToString() + " 泵+阀=" + zuPumpID.ToString());
|
||||
|
||||
sub_dev_Vave.SetVaves_status(openvavID_out, 1);
|
||||
//切换出水组电磁阀
|
||||
start_pump_out(zuPumpID, 0);
|
||||
Thread.Sleep(500);
|
||||
//启动出泵
|
||||
stopwatch.Start();
|
||||
start_pump(zuPumpID, pumpspeed);//泵控制
|
||||
|
||||
while (currentlv - targetLv > 2)
|
||||
{
|
||||
currentlv = sub_dev_Vave.m_PodSTA[i].waterLeve;
|
||||
Thread.Sleep(100);//每隔100 检查一次
|
||||
if (duliQuitFlag == 1)
|
||||
{
|
||||
Console.WriteLine("outwater forced quit!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
stopwatch.Stop();
|
||||
TimeSpan elapsedTime = stopwatch.Elapsed;
|
||||
Console.WriteLine($"ID={ID}, from lv={start_lv} to lv={targetLv} with pumpspeed{pumpspeed}, " +
|
||||
$"the time is{elapsedTime},out target reached, stop! ");
|
||||
//关闭进水泵
|
||||
stopPump(zuPumpID);//泵控制
|
||||
Thread.Sleep(1000);
|
||||
//阀门控制
|
||||
sub_dev_Vave.SetVaves_status(openvavID_out, 0);
|
||||
//关闭阀门
|
||||
}
|
||||
|
||||
public void StopALL_VAV()
|
||||
{
|
||||
Console.WriteLine("stop ALL pumps");
|
||||
stopPump(1);//泵控制
|
||||
Thread.Sleep(500);
|
||||
stopPump(2);//泵控制
|
||||
Thread.Sleep(500);
|
||||
stopPump(3);//泵控制
|
||||
Thread.Sleep(500);
|
||||
Console.WriteLine("close ALL VAVS");
|
||||
for (int i = 1; i <= 24; i++)
|
||||
{
|
||||
sub_dev_Vave.SetVaves_status(i, 0);
|
||||
}
|
||||
}
|
||||
//组 1,2,3
|
||||
public int AddwaterQuitFLG = 0;
|
||||
public void run_inWater_Zu(int Zu_ID, int pumpspeed, float targetLv)
|
||||
{
|
||||
if (Zu_ID <= 0 || Zu_ID > 3) { return; }
|
||||
AddwaterQuitFLG = 0;
|
||||
|
||||
int lowpumpspeed = Math.Min(pumpspeed, 500);
|
||||
int realpumpspdcmd = pumpspeed;
|
||||
|
||||
int [] ID = new int[4];//通路号
|
||||
int[] idx = new int[4];//索引号
|
||||
int[] ID_status = new int[4];//4个子通路状态
|
||||
float[] currentlev = new float[4];
|
||||
float[] lev_err = new float[4];
|
||||
|
||||
int reachnum = 0;//已满通路数字
|
||||
for(int j=0;j<4;j++)
|
||||
{
|
||||
ID[j] = (Zu_ID-1)*4+j+1;
|
||||
idx[j] = ID[j] - 1;
|
||||
currentlev[j] = sub_dev_Vave.m_PodSTA[idx[j]].waterLeve;
|
||||
lev_err[j] = currentlev[j] - targetLv;
|
||||
if(lev_err[j]>=0)
|
||||
{
|
||||
ID_status[j] = 1;//当前通路已满
|
||||
reachnum++;
|
||||
Console.WriteLine("通路已满:" + ID[j].ToString() + " level=" + currentlev[j].ToString() + " target=" + targetLv.ToString());
|
||||
// realpumpspdcmd = lowpumpspeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
ID_status[j] = 0;//当前通路未满
|
||||
}
|
||||
}
|
||||
if(reachnum==4)//全部4个已经到达目标
|
||||
{
|
||||
Console.WriteLine("Zu_ID=" + Zu_ID.ToString() + "ALL target reached" + " " + targetLv.ToString() + " return!");
|
||||
for(int j=0;j<4;)
|
||||
{
|
||||
Console.WriteLine("ID=" + ID[j].ToString() + " lv=" + currentlev[j].ToString());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(reachnum>=1)
|
||||
{
|
||||
realpumpspdcmd = lowpumpspeed;//有一个通路已满,速度换为龟速
|
||||
}
|
||||
|
||||
//切换进水组电磁阀
|
||||
start_pump_in(Zu_ID, 0);
|
||||
//打开需要加水的瓶子阀门
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
if (ID_status[j] == 0)//打开未满通路进水阀门
|
||||
{
|
||||
int openvavID1 = 2 * idx[j] + 1;
|
||||
sub_dev_Vave.SetVaves_status(openvavID1, 1);
|
||||
Console.WriteLine("进水 ID=" + ID[j].ToString() + " 阀门=" + openvavID1.ToString() + "打开");
|
||||
}
|
||||
}
|
||||
Thread.Sleep(500);
|
||||
//启动泵
|
||||
start_pump(Zu_ID, realpumpspdcmd);//泵控制启动
|
||||
|
||||
//循环检查,并关闭对应阀门
|
||||
while(true)
|
||||
{
|
||||
// int reachnum_dy = 0;
|
||||
for (int j = 0; j < 4; j++)//对每一个通路检查
|
||||
{
|
||||
currentlev[j] = sub_dev_Vave.m_PodSTA[idx[j]].waterLeve;
|
||||
lev_err[j] = currentlev[j] - targetLv;
|
||||
//只检查未满的通路
|
||||
if (ID_status[j] == 0)
|
||||
{
|
||||
if (lev_err[j] >= -1)//当前通路已满液
|
||||
{
|
||||
reachnum++;
|
||||
ID_status[j] = 1;
|
||||
Console.WriteLine("通路已满:" + ID[j].ToString() + " level=" + currentlev[j].ToString() + " target=" + targetLv.ToString());
|
||||
if (reachnum >= 4)//全部满了
|
||||
{
|
||||
stopPump(Zu_ID);//泵控制,停止
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
else if (reachnum == 1)//第一个满了时,切换泵速度
|
||||
{
|
||||
if (realpumpspdcmd != lowpumpspeed)//有一个通路已满时,采用低速
|
||||
{
|
||||
Console.WriteLine("using low pump speed 500");
|
||||
realpumpspdcmd = lowpumpspeed;
|
||||
stopPump(Zu_ID);//泵控制
|
||||
Thread.Sleep(500);
|
||||
start_pump(Zu_ID, realpumpspdcmd);//泵控制
|
||||
}
|
||||
|
||||
}
|
||||
int openvavID1 = 2 * idx[j] + 1;
|
||||
sub_dev_Vave.SetVaves_status(openvavID1, 0);//关闭阀门
|
||||
}
|
||||
}
|
||||
}
|
||||
if(reachnum >= 4)
|
||||
{
|
||||
Console.WriteLine("加液体完成,正常退出!");
|
||||
break;
|
||||
}
|
||||
if(AddwaterQuitFLG ==1)
|
||||
{
|
||||
Console.WriteLine("外部指令退出!关闭所有阀门和泵");
|
||||
stopPump(Zu_ID);//泵控制
|
||||
Thread.Sleep(500);
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
int openvavID1 = 2 * idx[j] + 1;
|
||||
sub_dev_Vave.SetVaves_status(openvavID1, 0);
|
||||
Console.WriteLine("进水 ID=" + ID[j].ToString() + " 阀门=" + openvavID1.ToString() + "关闭");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void run_outWater_Zu(int Zu_ID, int pumpspeed, float targetLv)
|
||||
{
|
||||
if (Zu_ID <= 0 || Zu_ID > 3) { return; }
|
||||
AddwaterQuitFLG = 0;
|
||||
|
||||
int lowpumpspeed = Math.Min(pumpspeed, 500);
|
||||
int realpumpspdcmd = pumpspeed;
|
||||
|
||||
int[] ID = new int[4];//通路号
|
||||
int[] idx = new int[4];//索引号
|
||||
int[] ID_status = new int[4];//4个子通路状态
|
||||
float[] currentlev = new float[4];
|
||||
float[] lev_err = new float[4];
|
||||
|
||||
int reachnum = 0;//已达到目标通路数字
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
ID[j] = (Zu_ID - 1) * 4 + j + 1;
|
||||
idx[j] = ID[j] - 1;
|
||||
currentlev[j] = sub_dev_Vave.m_PodSTA[idx[j]].waterLeve;
|
||||
lev_err[j] = targetLv -currentlev[j];
|
||||
if (lev_err[j] >= 0)
|
||||
{
|
||||
ID_status[j] = 1;//当前通路已达到目标
|
||||
reachnum++;
|
||||
Console.WriteLine("通路已达到目标:" + ID[j].ToString() + " level=" + currentlev[j].ToString() + " target=" + targetLv.ToString());
|
||||
// realpumpspdcmd = lowpumpspeed;
|
||||
}
|
||||
else
|
||||
{
|
||||
ID_status[j] = 0;//当前通路未达到目标
|
||||
}
|
||||
}
|
||||
if (reachnum == 4)//全部4个已经到达目标
|
||||
{
|
||||
Console.WriteLine("Zu_ID=" + Zu_ID.ToString() + "ALL target reached" + " " + targetLv.ToString() + " return!");
|
||||
for (int j = 0; j < 4;)
|
||||
{
|
||||
Console.WriteLine("ID=" + ID[j].ToString() + " lv=" + currentlev[j].ToString());
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (reachnum >= 1)
|
||||
{
|
||||
realpumpspdcmd = lowpumpspeed;//有一个通路已满,速度换为龟速
|
||||
}
|
||||
|
||||
//切换出水组电磁阀
|
||||
start_pump_out(Zu_ID, 0);
|
||||
//打开需要加水的瓶子阀门
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
if (ID_status[j] == 0)//打开未满通路进水阀门
|
||||
{
|
||||
int openvavID1 = 2 * idx[j] + 2;
|
||||
sub_dev_Vave.SetVaves_status(openvavID1, 1);
|
||||
Console.WriteLine("出水 ID=" + ID[j].ToString() + " 阀门=" + openvavID1.ToString() + "打开");
|
||||
}
|
||||
}
|
||||
Thread.Sleep(500);
|
||||
//启动泵
|
||||
start_pump(Zu_ID, realpumpspdcmd);//泵控制启动
|
||||
|
||||
//循环检查,并关闭对应阀门
|
||||
while (true)
|
||||
{
|
||||
// int reachnum_dy = 0;
|
||||
for (int j = 0; j < 4; j++)//对每一个通路检查
|
||||
{
|
||||
currentlev[j] = sub_dev_Vave.m_PodSTA[idx[j]].waterLeve;
|
||||
lev_err[j] = targetLv - currentlev[j];
|
||||
//只检查未满的通路
|
||||
if (ID_status[j] == 0)
|
||||
{
|
||||
if (lev_err[j] >= 0)//当前通路已满液
|
||||
{
|
||||
reachnum++;
|
||||
ID_status[j] = 1;
|
||||
Console.WriteLine("通路已达到目标:" + ID[j].ToString() + " level=" + currentlev[j].ToString() + " target=" + targetLv.ToString());
|
||||
if (reachnum >= 4)//全部满了
|
||||
{
|
||||
stopPump(Zu_ID);//泵控制,停止
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
else if (reachnum == 1)//第一个满了时,切换泵速度
|
||||
{
|
||||
if (realpumpspdcmd != lowpumpspeed)//有一个通路已满时,采用低速
|
||||
{
|
||||
Console.WriteLine("using low pump speed 500");
|
||||
realpumpspdcmd = lowpumpspeed;
|
||||
stopPump(Zu_ID);//泵控制
|
||||
Thread.Sleep(500);
|
||||
start_pump(Zu_ID, realpumpspdcmd);//泵控制
|
||||
}
|
||||
|
||||
}
|
||||
int openvavID1 = 2 * idx[j] + 2;
|
||||
sub_dev_Vave.SetVaves_status(openvavID1, 0);//关闭阀门
|
||||
}
|
||||
}
|
||||
}
|
||||
if (reachnum >= 4)
|
||||
{
|
||||
Console.WriteLine("加液体完成,正常退出!");
|
||||
break;
|
||||
}
|
||||
if (AddwaterQuitFLG == 1)
|
||||
{
|
||||
Console.WriteLine("外部指令退出!关闭所有阀门和泵");
|
||||
stopPump(Zu_ID);//泵控制
|
||||
Thread.Sleep(500);
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
int openvavID1 = 2 * idx[j] + 2;
|
||||
sub_dev_Vave.SetVaves_status(openvavID1, 0);
|
||||
Console.WriteLine("出水 ID=" + ID[j].ToString() + " 阀门=" + openvavID1.ToString() + "关闭");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
361
middleware/zdhsys/Device/Dev_CZ.cs
Normal file
361
middleware/zdhsys/Device/Dev_CZ.cs
Normal file
@@ -0,0 +1,361 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Drawing;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace zdhsys
|
||||
{
|
||||
public class Dev_CZ
|
||||
{
|
||||
public Dev_CZ() {
|
||||
start_com();
|
||||
currentdata_cz = new chengzhongdata();
|
||||
rstPod = -1;
|
||||
rstPodval = -1;
|
||||
}
|
||||
|
||||
DateTimeOffset nowOffset;
|
||||
DateTimeOffset nowOffset_lastmsg;// 用于计算相邻间隔时间
|
||||
|
||||
Thread recvudp_chengzhong = null;//开启和接收udp线程
|
||||
//string data
|
||||
string udprcvdata_chengzhong;//
|
||||
UdpClient client_cz;//udp通信本机端
|
||||
IPAddress remoteIp_CZ;//设备地址
|
||||
IPEndPoint remoteEndPoint_CZ;//设备IP udp接收
|
||||
IPEndPoint remoteEndPoint_CZ_forsend;//设备IP udp发送
|
||||
int port = 5555;
|
||||
public int latest_dev_state;//最新设备状态
|
||||
public int latest_pod;//最新设备状态 pod
|
||||
public double latest_val;//最新设备反馈值
|
||||
public int timegap;
|
||||
|
||||
|
||||
public int rstPod;//接受到的称量的瓶子
|
||||
public double rstPodval;//称量结果
|
||||
bool newrstarrvied = false;
|
||||
|
||||
public struct memrst//pod = 0; 表示大瓶子;pod = 1-12 小瓶子
|
||||
{
|
||||
public int pod;
|
||||
public double val;
|
||||
}
|
||||
public memrst latest_mem_rst;
|
||||
|
||||
public memrst GetMemrst()
|
||||
{
|
||||
if(newrstarrvied)
|
||||
{
|
||||
newrstarrvied= false;
|
||||
latest_mem_rst.pod = rstPod;
|
||||
latest_mem_rst.val = rstPodval;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("warning, no new mem result got!");
|
||||
latest_mem_rst.pod = -1;
|
||||
latest_mem_rst.val = -1;
|
||||
}
|
||||
return latest_mem_rst;
|
||||
}
|
||||
|
||||
|
||||
struct chengzhongdata
|
||||
{
|
||||
public int status;
|
||||
public int podID;
|
||||
public int val;
|
||||
}
|
||||
chengzhongdata currentdata_cz;
|
||||
|
||||
struct chengzhongCMD
|
||||
{
|
||||
public int cmd;
|
||||
public int podID;
|
||||
public int addval;
|
||||
}
|
||||
chengzhongCMD currentCMD_cz;
|
||||
|
||||
|
||||
private void start_com()//初始化和启动udp
|
||||
{
|
||||
recvudp_chengzhong = new Thread(runrecv_chengzhong);
|
||||
recvudp_chengzhong.Start();
|
||||
}
|
||||
|
||||
private bool checkconnection()//检查链接,10秒超时
|
||||
{
|
||||
nowOffset = DateTimeOffset.Now;
|
||||
int timestampOffset = (int)(nowOffset - nowOffset_lastmsg).TotalSeconds;
|
||||
timegap = timestampOffset;
|
||||
if (timestampOffset > 10)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private void waitforok()//等待设备就绪,cmd == 8
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
if(checkconnection())
|
||||
{
|
||||
if(latest_dev_state ==8)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Console.WriteLine("not ready");
|
||||
Console.WriteLine(latest_dev_state.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
Console.WriteLine("device ready 8");
|
||||
}
|
||||
//发送指令
|
||||
//cmd = 1 初始化
|
||||
//cmd = 3 通知设备已收到数据
|
||||
//cmd = 4 发送称大瓶子指令
|
||||
//cmd = 5 定量加液体指令 podID val
|
||||
//cmd = 6 发送称小瓶子指令 podID
|
||||
private void sendcmd(int cmd, int ID, double val)//发送指令
|
||||
{
|
||||
// waitforok();
|
||||
string cmdsend = "{\"cmd\": ,\"ID\":-1,\"val\":-1}";
|
||||
JObject objs = JObject.Parse(cmdsend);
|
||||
objs["cmd"] = cmd;
|
||||
objs["ID"] = ID;
|
||||
objs["val"] = val;
|
||||
|
||||
string jsonstring = objs.ToString();
|
||||
|
||||
Console.WriteLine(jsonstring);
|
||||
byte[] senddata = System.Text.Encoding.UTF8.GetBytes(jsonstring);
|
||||
Console.WriteLine(remoteEndPoint_CZ_forsend.ToString());
|
||||
client_cz.Send(senddata, senddata.Length, remoteEndPoint_CZ_forsend);
|
||||
}
|
||||
|
||||
private void cmd_result()
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
sendcmd(3, -1, -1);
|
||||
Thread.Sleep(500);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
//返回-1 表示链接断开或设备错误,其他,返回当前工作状态 8 空闲,9 错误,其他时候对应指令
|
||||
public int get_current_state()
|
||||
{
|
||||
if(checkconnection())
|
||||
{
|
||||
return latest_dev_state;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
//等待设备ok,发送初始化,确认初始化指令已收到
|
||||
public bool sendcmd_initial_until_start()
|
||||
{
|
||||
waitforok();
|
||||
Console.WriteLine("check dev state ok, send cmd");
|
||||
int cnt = 0;
|
||||
while(true)
|
||||
{
|
||||
cnt++;
|
||||
sendcmd(1, -1, -1);//发送初始化命令
|
||||
Thread.Sleep(1000);
|
||||
int state = get_current_state();
|
||||
Console.WriteLine(state.ToString());
|
||||
if (state == 1)
|
||||
{
|
||||
Console.WriteLine("send ok, device start initial");
|
||||
return true;
|
||||
}
|
||||
if(cnt > 10)
|
||||
{
|
||||
Console.WriteLine("send failed! ,time out break!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//等待设备ok,发送指令,确认指令已收到
|
||||
public bool sendcmd_mem1_until_start()//称大瓶子
|
||||
{
|
||||
waitforok();
|
||||
Console.WriteLine("check dev state ok, send cmd");
|
||||
int cnt = 0;
|
||||
while (true)
|
||||
{
|
||||
cnt++;
|
||||
sendcmd(4, -1, -1);//发送命令
|
||||
Thread.Sleep(1000);
|
||||
int state = get_current_state();
|
||||
Console.WriteLine(state.ToString());
|
||||
if (state == 4)
|
||||
{
|
||||
Console.WriteLine("send ok, device start initial");
|
||||
return true;
|
||||
}
|
||||
if (cnt > 10)
|
||||
{
|
||||
Console.WriteLine("send failed! ,time out break!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//等待设备ok,发送指令,确认指令已收到
|
||||
public bool sendcmd_Add_until_start(int podID, double val)//加磁核
|
||||
{
|
||||
waitforok();
|
||||
Console.WriteLine("check dev state ok, send cmd");
|
||||
int cnt = 0;
|
||||
while (true)
|
||||
{
|
||||
cnt++;
|
||||
sendcmd(5, podID, val);//发送命令
|
||||
Thread.Sleep(1000);
|
||||
int state = get_current_state();
|
||||
Console.WriteLine(state.ToString());
|
||||
if (state == 5)
|
||||
{
|
||||
Console.WriteLine("send ok, device start");
|
||||
return true;
|
||||
}
|
||||
if (cnt > 10)
|
||||
{
|
||||
Console.WriteLine("send failed! ,time out break!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//等待设备ok,发送指令,确认指令已收到
|
||||
public bool sendcmd_Mem2_until_start(int podID)//称小瓶子
|
||||
{
|
||||
waitforok();
|
||||
Console.WriteLine("check dev state ok, send cmd");
|
||||
int cnt = 0;
|
||||
while (true)
|
||||
{
|
||||
cnt++;
|
||||
sendcmd(6, podID, -1);//发送命令
|
||||
Thread.Sleep(1000);
|
||||
int state = get_current_state();
|
||||
Console.WriteLine(state.ToString());
|
||||
if (state == 6)
|
||||
{
|
||||
Console.WriteLine("send ok, device start");
|
||||
return true;
|
||||
}
|
||||
if (cnt > 10)
|
||||
{
|
||||
Console.WriteLine("send failed! ,time out break!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//向设备反馈
|
||||
public bool sendcmd_havereceived()
|
||||
{
|
||||
sendcmd(3, -1, -1);//发送已收到指令
|
||||
rstPod = latest_pod;
|
||||
rstPodval = latest_val;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//udp 初始化, 接受udp信息
|
||||
private void runrecv_chengzhong()
|
||||
{
|
||||
client_cz = new UdpClient(5555);
|
||||
remoteIp_CZ = IPAddress.Parse("192.168.1.222");
|
||||
int remotePort = 5555;
|
||||
remoteEndPoint_CZ = new IPEndPoint(remoteIp_CZ, remotePort);
|
||||
remoteEndPoint_CZ_forsend = new IPEndPoint(remoteIp_CZ, remotePort);
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] data = client_cz.Receive(ref remoteEndPoint_CZ);
|
||||
if (data != null)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder("");
|
||||
sb.Append(Encoding.UTF8.GetString(data));
|
||||
// Console.WriteLine(sb);
|
||||
udprcvdata_chengzhong = sb.ToString();
|
||||
// Console.WriteLine(udprcvdata_chengzhong);
|
||||
try
|
||||
{
|
||||
string json_str_form = "{\"status\": 1,\"id\":0,\"val\":0}";
|
||||
JToken jTokenform = JToken.Parse(json_str_form);
|
||||
JObject objs = JObject.Parse(udprcvdata_chengzhong);
|
||||
|
||||
if (jTokenform.Type != objs.Type)
|
||||
{
|
||||
Console.WriteLine("JSON格式不正确!");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Console.WriteLine("JSON格式正确!");
|
||||
currentdata_cz.status = int.Parse(objs["status"].ToString());
|
||||
currentdata_cz.podID = int.Parse(objs["id"].ToString());
|
||||
currentdata_cz.val = int.Parse(objs["val"].ToString());
|
||||
//objs[]
|
||||
latest_dev_state = currentdata_cz.status;
|
||||
if (currentdata_cz.status == 3)
|
||||
{
|
||||
Console.WriteLine("got mesure val!");
|
||||
Console.WriteLine(currentdata_cz.podID.ToString());
|
||||
Console.WriteLine(currentdata_cz.val.ToString());
|
||||
latest_pod = currentdata_cz.podID;
|
||||
latest_val = currentdata_cz.val;
|
||||
sendcmd_havereceived();
|
||||
|
||||
}
|
||||
nowOffset_lastmsg = DateTimeOffset.Now;
|
||||
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {
|
||||
Console.WriteLine(ex.Message.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// tbudpdata.Text = ex.ToString();
|
||||
// Console.WriteLine(ex.ToString());
|
||||
MessageBox.Show("UDP异常", ex.Message);
|
||||
// break;
|
||||
}
|
||||
// Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user