C#多线程
using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace UniSeqDevice { public class Device { public Device(string str)//在构造函数中创建多线程,只要new一个实例就会执行该线程while就会一直执行,一直到主函数进程结束 { Task T_Upadate = Task.Run(() => { while (true) { Console.WriteLine("str");
Thread.Sleep(1000);
} }); } public void Fun1() { Console.WriteLine("1"); } public void Fun2() { Console.WriteLine("2"); }
}}
//在主函数调用并且在主函数创建线程此时主函数的两个线程与构造函数的两个线程是并行的,结束周期是与主函数的执行周期有关系没有while执行一遍就会结束了就算构造函数有死循环当前进程结束,也就不在调用构造函数的线程,线程也会结束,主函数进程while就是为了一直运行不结束 namespace Test { internal class Program { static void Main(string[] args) { Device device = new Device("查询"); Task.Run(() => { while (true) { device.Fun1(); Thread.Sleep(1000); } }); Task.Run(() => { while (true) { device.Fun2(); Thread.Sleep(1000); } }); while (true) { Thread.Sleep(10000); } } } }
namespace UniSeqDevice{ public class DeviceZhuKongBan { static public string ver = "23"; private const ushort ConstInitTem = 3000; private const int ErrorTem = 50; private const int TimeOutTem = 1000 * 60 * 10;
private const int DelayCmd = 200;
private const int DelayTimeUpdate = 1000; private const int DelayTimeUpdateBin = 150; private const int DelayTimeUpdateNeedle = 150; private const int DelayTimeInit = 3000; private const byte DeviceID = 1; private bool IsInitFinished = false; private bool IsNeedleInitFinished = false;//插针是否初始化 private bool IsUpDownBinFinished = false;//升降舱门是否初始化 private ushort CurTemp;//当前温度 private ushort CurPosPump;//当前注射泵位置 private ushort StatePump;//是否忙碌 private static ushort NumReSend = 50;
private volatile FreeModbus freeModebus = new FreeModbus();//实例化Modbus public string CurVersions = "0.0.0.11";
private string NameOfLogBase = $"./Log/{System.DateTime.Now.ToString("yyyyMMdd")}-UnS-"; private string NameOfLog = "";
public static class PumpMoveTpye { public const ushort ChouQu1 = 1; public const ushort ChouQu2 = 2; public const ushort JinXinPian = 3; public const ushort ChuFeiYe = 4; } private static class RegAddrHo { public const ushort InitPump = 0; public const ushort InitPcr = 0; public const ushort CoPos = 1; public const ushort PumpSpeed = 2; public const ushort PumpChouQu = 3; public const ushort PumpXinPian = 5; public const ushort Tem = 9; public const ushort PumpFeiYe = 7; public const ushort UpDownNeedle = 16; public const ushort UpDownBin = 17; public const ushort InOutBin = 18; public const ushort TempMode = 20; public const ushort EnPCR = 21; public const ushort ResetCO1 = 22; public const ushort ResetCO2 = 23; } private static class RegAddrIn { public const ushort InitPump = 0; public const ushort CoPos = 1; public const ushort PumpSpeed = 2; public const ushort PumpPos = 3; public const ushort Tem = 9; public const ushort PumpState = 10; public const ushort InOutBinState = 13; public const ushort UpDownNeedleState = 18; public const ushort UpDownBinState = 19; public const ushort TempModeState = 26; public const ushort SetTemp = 27; public const ushort PCREnState = 28; } private static class Cmd { public const ushort InitPump = 1; public const ushort InitPcr = 2; public const ushort InitWeiEnSiMotor = 4; public const ushort DownNeedle = 2; public const ushort UpNeedle = 1; public const ushort DownBin = 1; public const ushort UpBin = 2; public const ushort InBin = 1; public const ushort OutBin = 2; }
bool isT1TxBusy = false;// bool isT2TxBusy = false;// bool isRxBusy = false; public bool isEndTUpdate = false; public DeviceZhuKongBan(string str) { Task T_Upadate = Task.Run(() => { while (true) { //while (isT1TxBusy || isT2TxBusy) // Thread.Sleep(1); isRxBusy = true; //查询输入寄存器
isRxBusy = false; Thread.Sleep(500); Console.WriteLine(str); if (isEndTUpdate) break; }
}); }
public void Fun1() { Console.WriteLine("1"); } public void Fun2() { Console.WriteLine("2"); } /// <summary> /// 获取当前库的版本号 /// </summary> /// <returns>当前版本号</returns> public string GetVersions() { return CurVersions; }
/// <summary> /// 查询是否已连接上主控板 /// </summary> /// <returns>true:已连接 false: 未连接</returns> public bool IsConnectZhuKongBan() { ushort data = 0; IsInitFinished = false; IsInitFinished = freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.InitPump, ref data); return IsInitFinished; }
/// <summary> /// 初始化串口端口 /// </summary> /// <param name="com">指定初始化的端口</param> /// <returns>true:成功 false: 失败</returns> public bool Init(string com) { try { freeModebus.serialPort.PortName = com; freeModebus.serialPort.BaudRate = 115200; freeModebus.serialPort.Parity = Parity.None; freeModebus.serialPort.StopBits = StopBits.One; NameOfLog = NameOfLogBase + com + ".log"; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化{com}串口 \r\n"); freeModebus.serialPort.Open(); return IsConnectZhuKongBan(); } catch (Exception) { //MessageBox.Show(ex.Message); } return false; } /// <summary> /// 关闭串口 /// </summary> public void Close() { freeModebus.serialPort.Close(); }
#region 韦恩斯电机
/// <summary> /// 获取升降插针状态 /// </summary> /// <param name="UpDownNeedlState">升降状态 1:已升起 2:已降落 0:未连接</param> /// <param name="IsInit">是否已初始化 1:已初始化 0:未初始化</param> /// <returns>true:获取成功 false: 获取失败</returns> public bool GetNeedleState(ref ushort UpDownNeedlState, ref ushort IsInit) { ushort data = 0; if (freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.UpDownNeedleState, ref data)) { if ((data & (1 << 0)) == (1 << 0))//初始化已完成 IsInit = 1; else IsInit = 0;
if ((data & (1 << 1)) == (1 << 1))//插针已降落 UpDownNeedlState = 2; else if ((data & (1 << 2)) == (1 << 2))//插针已升起 UpDownNeedlState = 1; else UpDownNeedlState = 0; return true; } return false; }
/// <summary> /// 初始化升降插针 /// </summary> /// <returns>true:成功 false: 失败</returns> private bool InitUpDownNeedle() { ushort value = 0; int start = Environment.TickCount; while (Environment.TickCount - start < 20 * 1000) { freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownNeedle, Cmd.InitWeiEnSiMotor); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.UpDownNeedleState, ref value)) { if ((value & (1 << 0)) == (1 << 0))//初始化已完成 break; } Thread.Sleep(DelayTimeUpdate); } int run_time = Environment.TickCount - start;
if ((value & (1 << 0)) == (1 << 0))//初始化已完成 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化升降插针成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化升降插针失败,耗时秒:{run_time / 1000} \r\n"); return false; } } /// <summary> /// 初始化出入舱门 /// </summary> /// <returns>true:成功 false: 失败</returns> private bool InitInOutBin() { ushort value = 0; int start = Environment.TickCount; while (Environment.TickCount - start < 20 * 1000) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownBin, Cmd.InitWeiEnSiMotor); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.InOutBinState, ref value)) { if ((value & (1 << 0)) == (1 << 0))//初始化已完成 break; } Thread.Sleep(DelayTimeUpdate); } int run_time = Environment.TickCount - start;
if ((value & (1 << 0)) == (1 << 0))//初始化已完成 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化出入舱门成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化出入舱门失败,耗时秒:{run_time / 1000} \r\n"); return false; } }
/// <summary> /// 初始化升降舱门 /// </summary> /// <returns>true:成功 false: 失败</returns> private bool InitUpDownBin() { ushort value = 0; int start = Environment.TickCount; while (Environment.TickCount - start < 20 * 1000) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownBin, Cmd.InitWeiEnSiMotor); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value)) { if ((value & (1 << 0)) == (1 << 0))//初始化已完成 break; } Thread.Sleep(DelayTimeUpdate); } int run_time = Environment.TickCount - start; if ((value & (1 << 0)) == (1 << 0))//初始化已完成 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化升降舱门成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 初始化升降舱门失败,耗时秒:{run_time / 1000} \r\n"); return false; } }
/// <summary> /// 下降插针 /// </summary> /// <returns>true:成功 false: 失败</returns> public bool NeedleDown() { ushort value = 0; int start = Environment.TickCount;
if (false == IsNeedleInitFinished) { freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownNeedleState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownNeedleState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownNeedleState, ref value); if ((value & (1 << 0)) == (1 << 0))//初始化已完成 IsNeedleInitFinished = true; else { if(true == InitUpDownNeedle()) IsNeedleInitFinished = true; else return false; } }
value = 0; while (((value & (1 << 1)) != (1 << 1)) && (Environment.TickCount - start < 20 * 1000)) {//S2 Thread.Sleep(DelayTimeUpdateNeedle); freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownNeedle, Cmd.DownNeedle); Thread.Sleep(DelayTimeUpdateNeedle); freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.UpDownNeedleState, ref value); if ((value & (1 << 1)) == (1 << 1))//插针已降落 break; } int run_time = Environment.TickCount - start; if ((value & (1 << 1)) == (1 << 1))//插针已降落 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 降插针成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 降插针失败,耗时秒:{run_time / 1000} \r\n"); return false; } } /// <summary> /// 升起插针 /// </summary> /// <returns>true:成功 false: 失败</returns> public bool NeedleUp() { ushort res = 0; ushort value = 0; int start = Environment.TickCount;
if (false == IsNeedleInitFinished) { freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownNeedleState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownNeedleState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownNeedleState, ref value); if ((value & (1 << 0)) == (1 << 0))//初始化已完成 IsNeedleInitFinished = true; else { if (true == InitUpDownNeedle()) IsNeedleInitFinished = true; else return false; } } while (((res & (1 << 2)) != (1 << 2)) && (Environment.TickCount - start < 20 * 1000)) {// Thread.Sleep(DelayTimeUpdateNeedle); freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownNeedle, Cmd.UpNeedle); Thread.Sleep(DelayTimeUpdateNeedle); freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.UpDownNeedleState, ref res); if ((res & (1 << 2)) == (1 << 2))//插针已升起 break; } int run_time = Environment.TickCount - start; if ((res & (1 << 2)) == (1 << 2))//插针已升起 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 升插针成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 升插针失败,耗时秒:{run_time / 1000} \r\n"); return false; } }
/// <summary> /// 获取升降舱门状态 /// </summary> /// <param name="UpDownBinState">升降舱门状态 1:舱门已升起 2:舱门已降落 0:未连接</param> /// <param name="IsInit">是否初始化 1:已初始化 0未初始化</param> /// <returns>true:获取成功 false: 获取失败</returns> public bool GetUpDownBinState(ref ushort UpDownBinState, ref ushort IsInit) { ushort data = 0; if (freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.UpDownBinState, ref data)) { if ((data & (1 << 0)) == (1 << 0))//初始化已完成 IsInit = 1; else IsInit = 0;
if ((data & (1 << 1)) == (1 << 1))//舱门已降落 UpDownBinState = 2; else if ((data & (1 << 2)) == (1 << 2))//舱门已升起 UpDownBinState = 1; else UpDownBinState = 0; return true; } return false; }
/// <summary> /// 获取出入舱门的状态 /// </summary> /// <param name="InOutBinState">出入舱门的状态 1:已入仓 2:已出仓 0:未连接</param> /// <param name="IsInit">是否初始化 1:已初始化 0未初始化</param> /// <returns>true:获取成功 false: 获取失败</returns> public bool GetInOutBinState(ref ushort InOutBinState, ref ushort IsInit) { ushort data = 0; if (freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.InOutBinState, ref data)) { if ((data & (1 << 0)) == (1 << 0))//初始化已完成 IsInit = 1; else IsInit = 0;
if ((data & (1 << 1)) == (1 << 1))//已出仓 InOutBinState = 2; else if ((data & (1 << 2)) == (1 << 2))//已入仓 InOutBinState = 1; else InOutBinState = 0; return true; } return false; }
/// <summary> /// 升起舱门 /// </summary> /// <returns>true:成功 false: 失败</returns> public bool BinUp() { ushort res = 0; ushort value = 0; int start = Environment.TickCount;
if (false == IsUpDownBinFinished) { freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value); if ((value & (1 << 0)) == (1 << 0))//初始化已完成 IsUpDownBinFinished = true; else { if (true == InitUpDownBin()) IsUpDownBinFinished = true; else return false; } }
while (Environment.TickCount - start < 20 * 1000) { Thread.Sleep(DelayTimeUpdateBin); freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownBin, Cmd.UpBin); Thread.Sleep(DelayTimeUpdateBin); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref res)) { if ((res & (1 << 1)) == (1 << 1))//舱门已升起 break; } } int run_time = Environment.TickCount - start; if ((res & (1 << 1)) == (1 << 1))//舱门已升起 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 升舱门成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 升舱门失败,耗时秒:{run_time / 1000} \r\n"); return false; } } /// <summary> /// 降落舱门 /// </summary> /// <returns>true:成功 false: 失败</returns> public bool BinDown() { ushort res = 0; ushort value = 0; int start = Environment.TickCount;
if (false == IsUpDownBinFinished) { freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref value); if ((value & (1 << 0)) == (1 << 0))//初始化已完成 IsUpDownBinFinished = true; else { if (true == InitUpDownBin()) IsUpDownBinFinished = true; else return false; } } while (Environment.TickCount - start < 20 * 1000) { Thread.Sleep(DelayTimeUpdateBin); freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.UpDownBin, Cmd.DownBin); Thread.Sleep(DelayTimeUpdateBin); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref res)) { if ((res & (1 << 2)) == (1 << 2))//舱门已降落 break; } } int run_time = Environment.TickCount - start; if ((res & (1 << 2)) == (1 << 2))//舱门已降落 { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 降舱门成功,耗时秒:{run_time / 1000} \r\n"); return true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 降舱门失败,耗时秒:{run_time / 1000} \r\n"); return false; } } /// <summary> /// 入仓 /// </summary> /// <returns>true:成功 false: 失败</returns> private bool BinIn() { ushort res = 0; Thread.Sleep(DelayTimeUpdate); freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.InOutBin, Cmd.InBin); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.InOutBinState, ref res)) { if ((res & (1 << 2)) == 0)//舱门已升起 return true; } return false; } /// <summary> /// 出仓 /// </summary> /// <returns>true:成功 false: 失败</returns> private bool BinOut() { ushort res = 0; Thread.Sleep(DelayTimeUpdate); freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.InOutBin, Cmd.OutBin); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.InOutBinState, ref res)) { if ((res & (1 << 1)) == 0)//舱门已降落 return true; } return false; } //public bool StateUpDownBin(ref ushort data) //{ // if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.UpDownBinState, ref data)) // { // return true; // } // return false; //} //public bool StateInOutBin(ref ushort data) //{ // if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.InOutBinState, ref data)) // { // return true; // } // return false; //} #endregion
#region 注射泵 /// <summary> /// 初始化注射泵 /// </summary> /// <returns>true:成功 false: 失败</returns> public bool InitPump() { ushort value = 0; bool res = false; int start = Environment.TickCount; if (true == freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.InitPump, Cmd.InitPump)) { Thread.Sleep(DelayTimeUpdate); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.PumpState, ref value); if (value == 1)//注射泵正在移动 { while (true) { Thread.Sleep(DelayTimeUpdate); if (true == freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.InitPump, ref value)) { if ((value & (1 << 0)) == 1) { CurPosPump = 0; res = true; break; } } } } } int run_time = Environment.TickCount - start;
if (res == true) { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 注射泵初始化成功,耗时秒:{run_time / 1000} \r\n"); } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 注射泵初始化失败,耗时秒:{run_time / 1000} \r\n"); res = false; } return res; }
/// <summary> /// 处理注射泵 /// </summary> /// <param name="Speed">设置注射泵速度uL/Min</param> /// <param name="SetPos">设置注射泵行程uL</param> /// <param name="MoveTpye">移动类型 1:抽取1,2:抽取2,3:进芯片,4:出废液</param> /// <returns></returns> public bool HandlePump(ushort Speed, ushort SetPos, ushort MoveTpye) { string str_tpye = null; bool res = false; short distance = (short)(SetPos - CurPosPump); int TimeOut = (int)((float)Math.Abs(distance) / Speed * 60 * 1000);//超时时间ms ushort[] value = new ushort[3]; ushort[] reads = new ushort[3];//reads[0]:绝对值位置 reads[1]:注射泵旋转阀位置 reads[2]:注射泵错误码 try { reads[0] = CurPosPump; value[0] = Speed; value[1] = SetPos; value[2] = MoveTpye; switch (MoveTpye) { case 1: str_tpye = "抽取1"; break; case 2: str_tpye = "抽取2"; break; case 3: str_tpye = "进芯片"; break; case 4: str_tpye = "出废液"; break; } int start = Environment.TickCount; StatePump = 0; while (CurPosPump != SetPos && Environment.TickCount - start < TimeOut + 10000) { if (StatePump == 0) {//StatePump =0 注射泵没有在移动 freeModebus.WriteMultiRegs(DeviceID, RegAddrHo.PumpSpeed, 3, value);//移动注射泵 Thread.Sleep(50); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.PumpState, ref StatePump);//查询注射泵的状态。 Thread.Sleep(50); } //freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.PumpPos, ref CurPosPump);//查询注射泵的位置。 freeModebus.ReadMultInputReg(DeviceID, RegAddrIn.PumpPos, 3, ref reads);//查询注射泵的位置。 Thread.Sleep(1);
CurPosPump = reads[0]; } int run_time = Environment.TickCount - start;//注射泵移动时间(毫秒)
if (CurPosPump == SetPos)//如果查询的位置等于设定的位置,返回成功。 { res = true; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 注射泵{str_tpye}成功,当前绝对位置{CurPosPump}ul,行程:{distance}ul,设置速度:{Speed}ul/min,理论耗时秒:{TimeOut / 1000},实际耗时秒:{run_time / 1000} \r\n"); } else { res = false; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G}注射泵{str_tpye}失败,错误码{reads[3]},当前绝对位置{CurPosPump}ul,行程:{distance}ul,设置速度:{Speed}ul/min,,理论耗时秒:{TimeOut / 1000},实际耗时秒:{run_time / 1000}\r\n"); } } catch (Exception ex) { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G}注射泵异常:{ex.ToString()}\r\n"); }
return res; }
public bool HandlePump2(ushort Speed, ushort SetPos, ushort MoveTpye) { while(isRxBusy || isT1TxBusy) Thread.Sleep(1); //发送 return true; } #endregion
#region 切换阀 /// <summary> /// 移动切换阀到指定位置 /// </summary> /// <param name="SetPos">指定位置</param> /// <returns>true:设置成功 false:设置失败</returns> public bool MoveQieHuanFa(ushort SetPos) { bool res = false; int count = 0; ushort CurPos = 0; int start = Environment.TickCount;
while (CurPos != SetPos && count < NumReSend) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.CoPos, SetPos); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.CoPos, ref CurPos); count++; } if (CurPos == SetPos) res = true; else//复位切换阀 { if (true == freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.ResetCO1, 1)) { System.Threading.Thread.Sleep(5 * 1000); while (CurPos != SetPos && count < NumReSend) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.CoPos, SetPos); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.CoPos, ref CurPos); count++; } if (CurPos == SetPos) res = true; else res = false; } else res = false; } int run_time = Environment.TickCount - start; if(res == true) Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 切换阀{SetPos}切换成功,耗时秒:{run_time / 1000} \r\n"); else Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 切换阀{SetPos}切换失败,耗时秒:{run_time / 1000} \r\n"); return res; }
/// <summary> /// 获取当前切换阀的位置 /// </summary> /// <param name="CurPos">当前切换阀的位置</param> /// <returns>true:获取成功 false: 获取失败</returns> public bool GetQieHuanFaPos(ref ushort CurPos) { return freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.CoPos, ref CurPos); } #endregion
/// <summary> /// 初始化PCR /// </summary> /// <returns>true:成功 false: 失败</returns> public bool InitPcr() { ushort value = 0; bool res = false; int start = Environment.TickCount; for (; ; ) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.InitPcr, Cmd.InitPcr); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.Tem, ref value)) { if (Math.Abs(value - ConstInitTem) < ErrorTem)//如果目标温度与实际温度差的绝对值小于0.5度,则执行下一条命令。 { res = true; CurTemp = ConstInitTem; break; } } Thread.Sleep(DelayTimeUpdate); if (Environment.TickCount - start > TimeOutTem) { res = false; break; } } int run_time = Environment.TickCount - start; if (res) Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} PCR初始化成功,当前温度{CurTemp},耗时秒:{run_time / 1000} \r\n"); else Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} PCR初始化失败,耗时秒:{run_time / 1000} \r\n"); return res; } /// <summary> /// 使能PCR /// </summary> /// <returns>true:成功 false: 失败</returns> public bool EnPCR() { bool res = false; int count = 0; ushort SetEn = 1; ushort CurEn = 0;
while (CurEn != SetEn && count < NumReSend) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.EnPCR, SetEn); Thread.Sleep(DelayCmd); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.PCREnState, ref CurEn); Thread.Sleep(DelayCmd); count++; } if (CurEn == SetEn) { res = true; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 使能温度成功 \r\n"); } else { res = false; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 使能温度失败 \r\n"); } return res; } /// <summary> /// 失能PCR /// </summary> /// <returns>true:成功 false: 失败</returns> public bool DisEnPCR() { bool res = false; int count = 0; ushort SetEn = 0; ushort CurEn = 1;
while (CurEn != SetEn && count < NumReSend) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.EnPCR, SetEn); Thread.Sleep(DelayCmd); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.PCREnState, ref CurEn); Thread.Sleep(DelayCmd); count++; } if (CurEn == SetEn) { res = true; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 失能温度成功 \r\n"); } else { res = false; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 失能温度失败 \r\n"); } return res; }
/// <summary> /// 设置PCR温度并等待PCR温度到达设置的值 /// </summary> /// <param name="Tem">设置温度值(此温度值需乘以100,例如设置30.5度则Tem = 3050)</param> /// <returns>true:成功 false: 失败</returns> private bool SetTem(ushort Tem) { ushort value = 0; bool res = false; int start = Environment.TickCount; for (; ; ) { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.Tem, Tem); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.Tem, ref value)) if (Math.Abs(value - Tem) < ErrorTem)//如果目标温度与实际温度差的绝对值小于0.5度,则执行下一条命令。 { res = true; CurTemp = Tem; break; } Thread.Sleep(DelayTimeUpdate); if (Environment.TickCount - start > TimeOutTem) { res = false; break; } } int run_time = Environment.TickCount - start; if (res) Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 设置温度{Tem}成功,当前温度{CurTemp},耗时秒:{run_time / 1000} \r\n"); else Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 设置温度{Tem}失败,耗时秒:{run_time / 1000} \r\n"); return res; }
///// <summary> ///// 设置一次PCR温度命令,不等待到达设置的值 ///// </summary> ///// <param name="tem">设置温度值(此温度值需乘以100,例如设置30.5度则Tem = 3050)</param> /////
/// <summary> /// 设置一次PCR温度命令,不等待到达设置的值 /// </summary> /// <param name="SetTemp">设置温度值(此温度值需乘以100,例如设置30.5度则Tem = 3050)</param> /// <returns>true:设置成功 false: 设置失败</returns> public bool SetTemNoWait(ushort SetTemp) { bool res = false; int count = 0; ushort UpdateSetTemp = 0;
while (UpdateSetTemp != SetTemp && count < NumReSend) { freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.Tem, SetTemp); Thread.Sleep(DelayCmd); freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.SetTemp, ref UpdateSetTemp); Thread.Sleep(DelayCmd); count++; } if (UpdateSetTemp == SetTemp) { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 设置并行温度:{SetTemp}成功 \r\n"); res = true; } else { Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 设置并行温度:{SetTemp}失败,查询设置温度:{UpdateSetTemp} \r\n"); res = false; } return res; }
/// <summary> /// 设置PCR温度并等待PCR温度到达设置的值 /// </summary> /// <param name="SetTem">设置温度值(此温度值需乘以100,例如设置30.5度则Tem = 3050)</param> /// <param name="Speed">如果此值为0,则以最大变温速度变温,设置温度速度 度每秒(此速度值需乘以100,例如设置0.5度每秒 则Speed = 0.5*100 = 50)</param> /// <returns></returns> public bool SetTem(ushort SetTem,ushort Speed) { for(int i = 0; i < 3; i++) { if (true == GetTem(ref CurTemp)) break; Thread.Sleep(DelayTimeUpdate); }
float TempError = (SetTem - CurTemp) / 100; ushort TempTem = CurTemp; ushort value = 0; bool res = false; int start = Environment.TickCount; Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 当前温度{(float)(CurTemp)/100:0.00} 设置温度{(float)(SetTem) / 100:0.00} 设置速度{(float)(Speed) / 100:0.00} \r\n"); if (Speed != 0) { if (SetTem > CurTemp) { while (Math.Abs(SetTem - CurTemp) >= ErrorTem)//达到了目标温度才会退出循环 { TempTem += Speed; freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.Tem, TempTem); Thread.Sleep(500); GetTem(ref CurTemp); Thread.Sleep(500); if (TempTem >= SetTem) break; } } else if (SetTem < CurTemp) { while (Math.Abs(SetTem - CurTemp) >= ErrorTem)//达到了目标温度才会退出循环 { TempTem -= Speed; freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.Tem, TempTem); Thread.Sleep(500); GetTem(ref CurTemp); Thread.Sleep(500); if (TempTem <= SetTem) break; } } } for (; ; )//等待温度到达最终设置值 { freeModebus.WriteSingleReg(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.Tem, SetTem); Thread.Sleep(DelayTimeUpdate); if (freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.Tem, ref value)) if (Math.Abs(value - SetTem) < ErrorTem)//如果目标温度与实际温度差的绝对值小于0.5度,则执行下一条命令。 { res = true; CurTemp = SetTem; break; } Thread.Sleep(DelayTimeUpdate); if (Environment.TickCount - start > TimeOutTem) { res = false; break; } } int run_time = Environment.TickCount - start; if (res) Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 设置温度{(float)(SetTem) / 100:0.00}成功,设置变温速率度/秒{(float)(Speed) / 100:0.00},实际变温速率度/秒{TempError / run_time * 1000 :0.00},耗时秒:{run_time / 1000} \r\n"); else Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 设置温度{(float)(SetTem) / 100:0.00}失败,设置变温速率度/秒{(float)(Speed) / 100:0.00},耗时秒:{run_time / 1000} \r\n"); return res; }
/// <summary> /// 获取当前PCR温度 /// </summary> /// <param name="temp">获取当前温度值</param> /// <returns>true:获取成功 false: 获取失败</returns> public bool GetTem(ref ushort temp) { bool res = false; res = freeModebus.ReadOneInputReg(DeviceID, RegAddrIn.Tem, ref temp); //if (res) // Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 查询温度成功,当前温度:{temp} \r\n"); //else // Logs.WriteLog(NameOfLog, $"{DateTime.Now:G} 查询温度失败,当前温度:{temp} \r\n"); return res; }
/// <summary> /// 设置变温模式 /// </summary> /// <param name="mode">1:拍照变温 0:正常变温</param> private void SetTempMode(ushort mode) { freeModebus.WriteSingleRegNoAsk(DeviceID, FreeModbus.WriteSingleHoldingReg, RegAddrHo.TempMode, mode); } /// <summary> /// 获取当前温度模式 /// </summary> /// <returns>-1:查询失败。1:拍照变温模式 0:正常变温模式</returns> private short GetTempMode() { ushort res = 0; if (freeModebus.ReadOneInputRegOneTime(DeviceID, RegAddrIn.TempModeState, ref res)) return (short)res; else return -1; } }}