feat: 添加PLC配置并优化后台服务
refactor(PLCTool): 重构PLC工具类,优化连接处理和错误日志 fix: 修正数据库连接字符串和开发环境配置 feat(DoanBackgroundService): 添加新的后台服务实现,改进库存监控逻辑
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
using HslCommunication;
|
||||
using HslCommunication.Profinet.Inovance;
|
||||
using HslCommunication.Profinet.Siemens;
|
||||
using HslCommunication.Profinet.Siemens;
|
||||
using Infrastructure;
|
||||
using JinianNet.JNTemplate;
|
||||
using MiniExcelLibs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -13,145 +11,93 @@ namespace DOAN.Infrastructure.PLC
|
||||
{
|
||||
public class PLCTool
|
||||
{
|
||||
|
||||
|
||||
// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
public SiemensS7Net siemensTcpNet = null;
|
||||
// 私有连接对象
|
||||
private SiemensS7Net siemensTcpNet = null;
|
||||
private readonly string plcAddress;
|
||||
public PLCTool()
|
||||
{
|
||||
|
||||
this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "10.72.82.242")
|
||||
plcAddress = AppSettings.GetConfig("PLCConfig:Address");
|
||||
this.siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, plcAddress)
|
||||
{
|
||||
ConnectTimeOut = 5000
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
public bool ConnectPLC()
|
||||
/// <summary>
|
||||
/// 尝试连接到PLC
|
||||
/// </summary>
|
||||
/// <returns>连接成功返回true,失败返回false</returns>
|
||||
public bool ConnectPLC()
|
||||
{
|
||||
try
|
||||
{
|
||||
OperateResult connect = siemensTcpNet.ConnectServer();
|
||||
var connect = siemensTcpNet.ConnectServer();
|
||||
if (connect.IsSuccess)
|
||||
{
|
||||
// 连接成功
|
||||
Console.WriteLine("PLC连接成功");
|
||||
Console.WriteLine($"PLC连接成功,地址{plcAddress}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 连接失败,输出原因
|
||||
Console.WriteLine("PLC连接失败:" + connect.Message);
|
||||
throw new CustomException("connect failed:" + connect.Message);
|
||||
Console.WriteLine($"PLC连接失败,地址{plcAddress}: {connect.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("PLC连接失败:" + e.Message);
|
||||
Console.WriteLine($"PLC连接失败,地址{plcAddress}: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 写入bit
|
||||
/// </summary>
|
||||
/// <param name="addr"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public bool WriteBit(string addr, bool value)
|
||||
{
|
||||
|
||||
OperateResult write = siemensTcpNet.Write(addr, value);
|
||||
/// <summary>
|
||||
/// 向PLC写入单个bit值
|
||||
/// </summary>
|
||||
/// <param name="addr">PLC地址</param>
|
||||
/// <param name="value">要写入的bool值</param>
|
||||
/// <returns>写入成功返回true,失败返回false</returns>
|
||||
public bool WriteBit(string addr, bool value)
|
||||
{
|
||||
var write = siemensTcpNet.Write(addr, value);
|
||||
return write.IsSuccess;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取bit
|
||||
/// 从PLC读取单个bit值
|
||||
/// </summary>
|
||||
/// <param name="addr"></param>
|
||||
/// <returns></returns>
|
||||
public bool ReadBit(string addr)
|
||||
/// <param name="addr">PLC地址</param>
|
||||
/// <returns>读取到的bool值</returns>
|
||||
public bool ReadBit(string addr)
|
||||
{
|
||||
|
||||
bool M100_7 = siemensTcpNet.ReadBool(addr).Content;
|
||||
return M100_7;
|
||||
return siemensTcpNet.ReadBool(addr).Content;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读多个连续的二进制数据 转为字节数组
|
||||
/// 从PLC读取多个连续的二进制数据并转为字节数组
|
||||
/// </summary>
|
||||
/// <param name="addr"></param>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
//public byte[,] ReadAllValue(string addr = "VB100", ushort length = 11)
|
||||
//{
|
||||
// byte[,] data = new byte[length, 8];
|
||||
// //需要自行解析,length为地址个数
|
||||
// siemensTcpNet.ReadByte(addr);
|
||||
// OperateResult<byte[]> result = siemensTcpNet.Read(addr, length);
|
||||
// if (result.IsSuccess)
|
||||
// {
|
||||
|
||||
// if (result.Content.Length > 0)
|
||||
// {
|
||||
|
||||
// for (int i = 0; i < result.Content.Length; i++)
|
||||
// {
|
||||
|
||||
// int row = i / 8;
|
||||
// int col = i % 8;
|
||||
// data[row, col] = result.Content[i];
|
||||
|
||||
// }
|
||||
// return data;
|
||||
|
||||
|
||||
// }
|
||||
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Console.WriteLine($"PLC IO 取值失败,地址为{addr},地址个数为{length}");
|
||||
// return null;
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 读多个连续的二进制数据 转为字节数组
|
||||
/// </summary>
|
||||
/// <param name="addr"></param>
|
||||
/// <param name="length"></param>
|
||||
/// <returns></returns>
|
||||
/// <param name="addr">起始PLC地址,默认为"VB100"</param>
|
||||
/// <param name="length">要读取的字节数,默认为12</param>
|
||||
/// <returns>读取到的字节数组,如果失败返回null</returns>
|
||||
public byte[] ReadAllValue(string addr = "VB100", ushort length = 12)
|
||||
{
|
||||
|
||||
//需要自行解析,length为地址个数
|
||||
siemensTcpNet.ReadByte(addr);
|
||||
OperateResult<byte[]> result = siemensTcpNet.Read(addr, length);
|
||||
var result = siemensTcpNet.Read(addr, length);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
|
||||
return result.Content;
|
||||
|
||||
return result.Content;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"PLC IO 取值失败,地址为{addr},地址个数为{length}");
|
||||
Console.WriteLine($"PLC IO 取值失败,PLC地址{plcAddress},访问地址为{addr},地址个数为{length}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void ConnectClose()
|
||||
/// <summary>
|
||||
/// 关闭与PLC的连接
|
||||
/// </summary>
|
||||
public void ConnectClose()
|
||||
{
|
||||
siemensTcpNet.ConnectClose();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user