Commit 4fb1a0fc authored by 潘栩锋's avatar 潘栩锋 🚴

添加 风环PLC 添加V5 看门狗功能。 用于电脑与PLC连接断开,PLC会断电。

parent 35b3a653
......@@ -21,13 +21,25 @@ namespace FLY.FeedbackRenZiJia.Server
const int MaxRegsOfOneRead = 50;
const int MaxCoilsOfOneRead = 100;
//D200 4x201 通道数量
//D201 4x202 设置值 更新
//D202 4x203 当前值 更新 //不用
//D400 4x401 设置值 160个
//D600 4x601 当前值 160个 //不用
//M3 0x4 风环开关
//M4 0x5 检测电流
//D200 通道数量
//D201 设置值 更新
//D202 当前值 更新 //不用
//D400 设置值 160个
//D600 当前值 160个 //不用
//M3 风环开关
//M4 检测电流
//M0 看门狗 PC写1
const int ADDR_ChannelCnt = 200;
const int ADDR_ChannelValuesUpdate = 201;
const int ADDR_ChannelValues = 400;
const int ADDR_ChannelValues_Count = 160;
const int ADDR_Heartbeat = 0;
const int ADDR_StatusZone = 3;
const int ADDR_StatusZone_Index_PcOnline = 0;
const int ADDR_StatusZone_Index_HasCurrent = 1;
const int ADDR_StatusZone_Cnt = 2;
public FLY.Modbus.WithThread.ClientTCP mclient;
/// <summary>
......@@ -66,8 +78,12 @@ namespace FLY.FeedbackRenZiJia.Server
/// 异常次数
/// </summary>
public int ErrorCnt { get; private set; }
/// <summary>
/// 心跳写入周期,单位ms
/// </summary>
const int heartbeatInterval = 2000;
CancellationTokenSource cts;
Stopwatch stopwatch_heartbeat;
public PLCLink(IPEndPoint ep)
{
......@@ -94,8 +110,8 @@ namespace FLY.FeedbackRenZiJia.Server
if (!IsConnected)
return;
//D200 4x201 通道数量
RegWrite regWrite = new RegWrite(PLCAddressArea.Register, 200, new UInt16[] { channelCnt });
//D200 通道数量
RegWrite regWrite = new RegWrite(PLCAddressArea.Register, ADDR_ChannelCnt, new UInt16[] { channelCnt });
lock (rws)
{
rws.Add(regWrite);
......@@ -106,8 +122,8 @@ namespace FLY.FeedbackRenZiJia.Server
if (!IsConnected)
return;
//D201 4x202 设置值 更新
RegWrite regWrite = new RegWrite(PLCAddressArea.Register, 201, new UInt16[] { heatUpdate });
//D201 设置值 更新
RegWrite regWrite = new RegWrite(PLCAddressArea.Register, ADDR_ChannelValuesUpdate, new UInt16[] { heatUpdate });
lock (rws)
{
rws.Add(regWrite);
......@@ -141,7 +157,7 @@ namespace FLY.FeedbackRenZiJia.Server
void OnPoll_update()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch_heartbeat = new Stopwatch();
_connect:
while (true)
......@@ -166,6 +182,7 @@ namespace FLY.FeedbackRenZiJia.Server
rws.Clear();
}
stopwatch.Restart();
stopwatch_heartbeat.Restart();
while (true)
{
if (cts.IsCancellationRequested)
......@@ -187,6 +204,20 @@ namespace FLY.FeedbackRenZiJia.Server
IsConnected = true;
}
//心跳包
if (stopwatch_heartbeat.ElapsedMilliseconds >= heartbeatInterval)
{
stopwatch_heartbeat.Restart();
if (!SetHeartbeat())
{
if (cts.IsCancellationRequested)//可能是外部强制关闭
goto _end;
goto _error;//连接断开,终止更新线程
}
}
//输出写入数据
if (!UpdateWriteData())
{
......@@ -212,15 +243,15 @@ namespace FLY.FeedbackRenZiJia.Server
bool UpdateReadData()
{
//M3 0x4 风环开关
//M4 0x5 检测电流
if (!mclient.Do_01(3, 2, out IEnumerable<bool> values))
//M3 风环开关
//M4 检测电流
if (!mclient.Do_01(ADDR_StatusZone, ADDR_StatusZone_Cnt, out IEnumerable<bool> values))
return false;
HasFan = values.ElementAt(0);
HasElectricity = values.ElementAt(1);
HasFan = values.ElementAt(ADDR_StatusZone_Index_PcOnline);
HasElectricity = values.ElementAt(ADDR_StatusZone_Index_HasCurrent);
//D201 4x202 设置值 更新
if (!mclient.Do_03(201, 1, out IEnumerable<UInt16> values2))
//D201 设置值 更新
if (!mclient.Do_03(ADDR_ChannelValuesUpdate, 1, out IEnumerable<UInt16> values2))
return false;
HeatUpdate_R = values2.ElementAt(0);
......@@ -263,8 +294,8 @@ namespace FLY.FeedbackRenZiJia.Server
UInt16[] buf = values.ToArray();
List<RegWrite> writes = new List<RegWrite>();
//D400 4x401 设置值 160个
int addr = 400;
//D400 设置值 160个
int addr = ADDR_ChannelValues;
int cnt = buf.Count();
int offset = 0;
......@@ -292,5 +323,16 @@ namespace FLY.FeedbackRenZiJia.Server
}
}
/// <summary>
/// 心跳包,每5秒设置一次
/// </summary>
bool SetHeartbeat()
{
//M0 Heartbeat 信号, 由电脑写1, PLC写0, 当电脑 1分钟 都没写一次1,就会停止当前全部加热
if (!mclient.Do_05(ADDR_Heartbeat, true))
return false;
return true;
}
}
}
......@@ -19,3 +19,9 @@ PLC 地址为 192.168.50.60
//--------------------------------------------
v4
不检测 X17, 对应的 M3(自动风环开关检测) 恒为1
//--------------------------------------------
v5
添加 M0, PC喂狗,需要每30秒内设置M0=1, PLC会把M0=0,同时设置M3=1,
允许加热, 当30秒内M0都一直为0, M3会被复位为0, 不能加热
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment