feat: 添加PLC配置并优化后台服务

refactor(PLCTool): 重构PLC工具类,优化连接处理和错误日志
fix: 修正数据库连接字符串和开发环境配置
feat(DoanBackgroundService): 添加新的后台服务实现,改进库存监控逻辑
This commit is contained in:
2025-08-19 09:30:44 +08:00
parent 410abd988a
commit 07880805a3
5 changed files with 578 additions and 140 deletions

View File

@@ -10,7 +10,7 @@
"dbConfigs": [
{
"Conn": "Data Source=192.168.0.58;User ID=root;Password=123456;Initial Catalog=pbl_huaxiang;Port=3306",
"Conn": "Data Source=139.224.232.211;User ID=root;Password=doantech123;Initial Catalog=pbl_huaxiang;Port=3308",
//"Conn": "Data Source=10.72.80.161;User ID=root;Password=doantech123;Initial Catalog=pbl_huaxiang_v2;Port=3306",
"DbType": 0, //数据库类型 MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4
"ConfigId": "0", //多租户唯一标识

View File

@@ -1 +1,5 @@
{}
{
"PLCConfig": {
"Address": "10.72.82.242"
}
}

View File

@@ -21,10 +21,11 @@ namespace DOAN.ServiceCore
/// 永驻线程
/// 功能:检测传感器信号,判断箱子数
/// </summary>
public class DoanBackgroundService : IHostedService, IDisposable
public class DoanBackgroundService : BaseService<Storagelocation>, IHostedService, IDisposable
{
private readonly IHubContext<PBLhub> notificationHubContext;
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private readonly CancellationTokenSource _cancellationTokenSource =
new CancellationTokenSource();
private Task _executingTask;
private PLCTool pLCTool;
private List<Storagelocation> storagelocationList = new List<Storagelocation>();
@@ -39,13 +40,13 @@ namespace DOAN.ServiceCore
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine($"PCL定时任务开启");
pLCTool = new PLCTool();
pLCTool.ConnectPLC();
// 初始化料架层和点位表数据
RefreshData(null);
// sql标准
Context.Queryable<Storagelocation>();
// 初始化料架层和点位表数据
RefreshData(null);
// 启动后台任务
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
@@ -72,7 +73,10 @@ namespace DOAN.ServiceCore
{
if (position < 0 || position > 7)
{
throw new ArgumentOutOfRangeException(nameof(position), "Position must be between 0 and 7" + position);
throw new ArgumentOutOfRangeException(
nameof(position),
"Position must be between 0 and 7" + position
);
}
byte mask = (byte)(1 << position);
@@ -111,24 +115,34 @@ namespace DOAN.ServiceCore
// 读取按钮PLC I/O状态
foreach (PlcButton button in buttonPositionList)
{
int row = button.Address - 100;
int col = button.Index;
if (plcSensorValues != null && row >= 0 && row < plcSensorValues.Length && !GetInvertedBit(plcSensorValues[row], col))
if (
plcSensorValues != null
&& row >= 0
&& row < plcSensorValues.Length
&& !GetInvertedBit(plcSensorValues[row], col)
)
{
// 按钮按下灭灯
if (button.Code == "灭灯按钮")
{
Storagelocation offLightstoragelocation = storagelocationList.Where(it => it.Id == button.StoragelocationId).First();
bool isSuccess = pLCTool.WriteBit(offLightstoragelocation.PlcAddress, false);
Storagelocation offLightstoragelocation = storagelocationList
.Where(it => it.Id == button.StoragelocationId)
.First();
bool isSuccess = pLCTool.WriteBit(
offLightstoragelocation.PlcAddress,
false
);
if (isSuccess)
{
offLightstoragelocation.IsLight = 0;
using (var scope = DbScoped.SugarScope.CopyNew())
{
await scope.Updateable(offLightstoragelocation).ExecuteCommandAsync();
await scope
.Updateable(offLightstoragelocation)
.ExecuteCommandAsync();
//灭灯日志
Light_Log light_Log = new Light_Log
{
@@ -143,7 +157,10 @@ namespace DOAN.ServiceCore
await scope.Insertable(light_Log).ExecuteCommandAsync();
}
}
await notificationHubContext.Clients.All.SendAsync("PBL_storagelocation_change", "手动灭灯");
await notificationHubContext.Clients.All.SendAsync(
"PBL_storagelocation_change",
"手动灭灯"
);
}
// 镜体补料按钮
if (button.Code == "镜体补料按钮")
@@ -172,10 +189,16 @@ namespace DOAN.ServiceCore
LayerNum = 1,
CurrentPackageCount = 0,
AlarmThreshold = 1,
ActionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString(),
ActionTime = DateTime
.Now.ToString("yyyy-MM-dd HH:mm:ss")
.ToString(),
};
string alarmMessage = System.Text.Json.JsonSerializer.Serialize(alarmData);
await notificationHubContext.Clients.All.SendAsync("PBL_lack_alarm", alarmMessage);
string alarmMessage =
System.Text.Json.JsonSerializer.Serialize(alarmData);
await notificationHubContext.Clients.All.SendAsync(
"PBL_lack_alarm",
alarmMessage
);
}
}
else
@@ -187,14 +210,15 @@ namespace DOAN.ServiceCore
}
}
var updateStoragelocationList = new List<Storagelocation>();
var inventoryLogs = new List<Inventorylog>();
foreach (var layerItem in storagelocationList)
{
// 获取这个料架层的点位表
var layerPoints = pointPositionList.Where(it => it.FkStorageId == layerItem.Id).ToList();
var layerPoints = pointPositionList
.Where(it => it.FkStorageId == layerItem.Id)
.ToList();
if (layerPoints.Any())
{
@@ -205,9 +229,15 @@ namespace DOAN.ServiceCore
int row = point.ByteNum - 100;
int col = point.BitNum - 1;
if (plcSensorValues != null && row >= 0 && row < plcSensorValues.Length)
if (
plcSensorValues != null
&& row >= 0
&& row < plcSensorValues.Length
)
{
currentPackageCount += GetInvertedBit(plcSensorValues[row], col) ? 1 : 0;
currentPackageCount += GetInvertedBit(plcSensorValues[row], col)
? 1
: 0;
}
}
@@ -215,22 +245,38 @@ namespace DOAN.ServiceCore
int previousPackageCount = layerItem.PackageNum.GetValueOrDefault();
if (currentPackageCount > previousPackageCount)
{
UpdateAndLog(layerItem, currentPackageCount, 2, "出库", inventoryLogs);
UpdateAndLog(
layerItem,
currentPackageCount,
2,
"出库",
inventoryLogs
);
updateStoragelocationList.Add(layerItem);
}
else if (currentPackageCount < previousPackageCount)
{
UpdateAndLog(layerItem, currentPackageCount, 1, "入库", inventoryLogs);
UpdateAndLog(
layerItem,
currentPackageCount,
1,
"入库",
inventoryLogs
);
updateStoragelocationList.Add(layerItem);
}
// 补料报警触发
if (layerItem.IsLackAlarm == 1 && currentPackageCount < layerItem.AlarmNum)
if (
layerItem.IsLackAlarm == 1
&& currentPackageCount < layerItem.AlarmNum
)
{
using (var scope = DbScoped.SugarScope.CopyNew())
{
// 是否已经报警过,并且
bool hasLastAlarm = await scope.Queryable<AlarmLog>()
bool hasLastAlarm = await scope
.Queryable<AlarmLog>()
.Where(it => it.StoragelocationId == layerItem.Id)
.Where(it => it.Type == 1)
.Where(it => it.Status == 1)
@@ -243,7 +289,8 @@ namespace DOAN.ServiceCore
AlarmLog alarmLog = new AlarmLog
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
Name = $"镜壳需补料:{layerItem.RackCode} {layerName} 阈值{layerItem.AlarmNum}",
Name =
$"镜壳需补料:{layerItem.RackCode} {layerName} 阈值{layerItem.AlarmNum}",
Code = "镜壳补料",
StoragelocationId = layerItem.Id,
Type = 1,
@@ -259,10 +306,16 @@ namespace DOAN.ServiceCore
LayerNum = layerItem.LayerNum,
CurrentPackageCount = currentPackageCount,
AlarmThreshold = layerItem.AlarmNum,
ActionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").ToString(),
ActionTime = DateTime
.Now.ToString("yyyy-MM-dd HH:mm:ss")
.ToString(),
};
string alarmMessage = System.Text.Json.JsonSerializer.Serialize(alarmData);
await notificationHubContext.Clients.All.SendAsync("PBL_lack_alarm", alarmMessage);
string alarmMessage =
System.Text.Json.JsonSerializer.Serialize(alarmData);
await notificationHubContext.Clients.All.SendAsync(
"PBL_lack_alarm",
alarmMessage
);
}
}
}
@@ -274,19 +327,32 @@ namespace DOAN.ServiceCore
{
using (var scope = DbScoped.SugarScope.CopyNew())
{
await scope.Updateable(updateStoragelocationList)
.IgnoreColumns(it => new { it.IsLight, it.IsLackAlarm, it.AlarmNum, it.MaxCapacity })
await scope
.Updateable(updateStoragelocationList)
.IgnoreColumns(it => new
{
it.IsLight,
it.IsLackAlarm,
it.AlarmNum,
it.MaxCapacity
})
.ExecuteCommandAsync();
}
// 发送库存变更Socket通知
string changeMessage = "库存变动";
await notificationHubContext.Clients.All.SendAsync("PBL_storagelocation_change", changeMessage);
await notificationHubContext.Clients.All.SendAsync(
"PBL_storagelocation_change",
changeMessage
);
}
// 插入库存变更日志
if (inventoryLogs.Any())
{
await DbScoped.SugarScope.CopyNew().Insertable(inventoryLogs).ExecuteCommandAsync();
await DbScoped
.SugarScope.CopyNew()
.Insertable(inventoryLogs)
.ExecuteCommandAsync();
}
// 添加延迟以避免频繁查询(暂定3秒防误触)
await Task.Delay(2000, stoppingToken);
@@ -298,7 +364,9 @@ namespace DOAN.ServiceCore
}
catch (Exception ex)
{
Console.WriteLine($"DoanBackGround线程ExecuteAsync异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
Console.WriteLine(
$"DoanBackGround线程ExecuteAsync异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}"
);
}
}
@@ -338,7 +406,8 @@ namespace DOAN.ServiceCore
int newPackageCount,
int operation,
string operatorName,
List<Inventorylog> inventoryLogs)
List<Inventorylog> inventoryLogs
)
{
storageLocation.PackageNum = newPackageCount;
storageLocation.UpdatedBy = operatorName;
@@ -350,7 +419,9 @@ namespace DOAN.ServiceCore
RackCode = storageLocation.RackCode,
Partnumber = storageLocation.Partnumber,
Operation = operation,
PackageNum = Math.Abs(newPackageCount - storageLocation.PackageNum.GetValueOrDefault()),
PackageNum = Math.Abs(
newPackageCount - storageLocation.PackageNum.GetValueOrDefault()
),
CreatedBy = operatorName,
CreatedTime = DateTime.Now.ToLocalTime()
};
@@ -376,6 +447,3 @@ namespace DOAN.ServiceCore
}
}
}

View File

@@ -0,0 +1,420 @@
using DOAN.Infrastructure.PLC;
using DOAN.Model.PBL;
using DOAN.ServiceCore.Signalr;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
namespace DOAN.ServiceCore
{
/// <summary>
/// 永驻线程
/// 功能:检测传感器信号,判断箱子数
/// </summary>
public class DoanBackgroundService2 : BaseService<Storagelocation>, IHostedService, IDisposable
{
private readonly IHubContext<PBLhub> notificationHubContext;
private readonly CancellationTokenSource _cancellationTokenSource =
new CancellationTokenSource();
private Task _executingTask;
private PLCTool pLCTool;
private List<Storagelocation> storagelocationList = new List<Storagelocation>();
private List<PlcAddressTable> pointPositionList = new List<PlcAddressTable>();
private List<PlcButton> buttonPositionList = new List<PlcButton>();
private Timer refreshTimer;
public DoanBackgroundService2(IHubContext<PBLhub> hubContext)
{
notificationHubContext = hubContext;
}
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine($"PCL定时任务开启");
pLCTool = new PLCTool();
pLCTool.ConnectPLC();
// 初始化料架层和点位表数据
RefreshData(null);
// 启动后台任务
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
// 设置定时器每10分钟刷新一次料架层和点位表 .FromMinutes(1)
refreshTimer = new Timer(RefreshData, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
// 请求取消后台任务
_cancellationTokenSource.Cancel();
// 停止定时器
refreshTimer?.Change(Timeout.Infinite, Timeout.Infinite);
refreshTimer?.Dispose();
// 等待后台任务完成
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
private static bool GetInvertedBit(byte b, int position)
{
if (position < 0 || position > 7)
{
throw new ArgumentOutOfRangeException(
nameof(position),
"Position must be between 0 and 7" + position
);
}
byte mask = (byte)(1 << position);
bool isSet = (b & mask) != 0;
return !isSet; // 返回取反后的值
}
/// <summary>
/// 功能:检测传感器信号,判断箱子数并更新库存及日志
/// </summary>
/// <param name="stoppingToken">取消令牌</param>
/// <returns></returns>
private async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine($"PCL定时任务ExecuteAsync");
try
{
while (!stoppingToken.IsCancellationRequested)
{
// 读取PLC I/O状态12个字节
byte[] plcSensorValues;
try
{
// 读取1开头100,101。。。等全部地址数据
plcSensorValues = pLCTool.ReadAllValue("VB100", 13);
}
catch (Exception ex)
{
Console.WriteLine($"读取PLC数据异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
continue;
}
// 读取按钮PLC I/O状态
foreach (PlcButton button in buttonPositionList)
{
int row = button.Address - 100;
int col = button.Index;
if (
plcSensorValues != null
&& row >= 0
&& row < plcSensorValues.Length
&& !GetInvertedBit(plcSensorValues[row], col)
)
{
// 按钮按下灭灯
if (button.Code == "灭灯按钮")
{
var offLightstoragelocation = await Context
.Queryable<Storagelocation>()
.Where(it => it.Id == button.StoragelocationId)
.FirstAsync();
bool isSuccess = pLCTool.WriteBit(
offLightstoragelocation.PlcAddress,
false
);
if (isSuccess)
{
offLightstoragelocation.IsLight = 0;
await Context
.Updateable(offLightstoragelocation)
.ExecuteCommandAsync();
//灭灯日志
Light_Log light_Log = new Light_Log
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
LightOperation = 2,
LayerNum = offLightstoragelocation.LayerNum,
ShelfCode = offLightstoragelocation.RackCode,
IsSuccess = isSuccess,
Operationer = "按钮手动灭灯",
CreatedTime = DateTime.Now
};
await Context.Insertable(light_Log).ExecuteCommandAsync();
}
await notificationHubContext.Clients.All.SendAsync(
"PBL_storagelocation_change",
"手动灭灯"
);
}
// 镜体补料按钮
if (button.Code == "镜体补料按钮")
{
bool lightStatus = pLCTool.ReadBit("V208.4");
// 原本灭灯,亮灯补料
if (!lightStatus)
{
AlarmLog alarmLog = new AlarmLog
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
Name = "镜体需要补料",
Code = "镜体补料",
StoragelocationId = 0,
Type = 2,
Status = 1,
ActionTime = DateTime.Now,
EndTime = DateTime.Now
};
await Context.Insertable(alarmLog).ExecuteCommandAsync();
var alarmData = new
{
RackCode = "镜体需要补料",
LayerNum = 1,
CurrentPackageCount = 0,
AlarmThreshold = 1,
ActionTime = DateTime
.Now.ToString("yyyy-MM-dd HH:mm:ss")
.ToString(),
};
string alarmMessage = System.Text.Json.JsonSerializer.Serialize(
alarmData
);
await notificationHubContext.Clients.All.SendAsync(
"PBL_lack_alarm",
alarmMessage
);
}
else
{
// 原本亮灯,按一下灭灯
pLCTool.WriteBit("V208.4", false);
}
}
}
}
var updateStoragelocationList = new List<Storagelocation>();
var inventoryLogs = new List<Inventorylog>();
foreach (var layerItem in storagelocationList)
{
// 获取这个料架层的点位表
var layerPoints = await Context
.Queryable<PlcAddressTable>()
.Where(it => it.FkStorageId == layerItem.Id)
.ToListAsync();
if (layerPoints.Any())
{
int currentPackageCount = 0; // 默认最小值为0
foreach (var point in layerPoints)
{
int row = point.ByteNum - 100;
int col = point.BitNum - 1;
if (
plcSensorValues != null
&& row >= 0
&& row < plcSensorValues.Length
)
{
currentPackageCount += GetInvertedBit(plcSensorValues[row], col)
? 1
: 0;
}
}
// 检查箱子数量变化并记录日志
int previousPackageCount = layerItem.PackageNum.GetValueOrDefault();
if (currentPackageCount > previousPackageCount)
{
UpdateAndLog(
layerItem,
currentPackageCount,
2,
"出库",
inventoryLogs
);
updateStoragelocationList.Add(layerItem);
}
else if (currentPackageCount < previousPackageCount)
{
UpdateAndLog(
layerItem,
currentPackageCount,
1,
"入库",
inventoryLogs
);
updateStoragelocationList.Add(layerItem);
}
// 补料报警触发
if (
layerItem.IsLackAlarm == 1
&& currentPackageCount < layerItem.AlarmNum
)
{
// 是否已经报警过,并且
bool hasLastAlarm = await Context
.Queryable<AlarmLog>()
.Where(it => it.StoragelocationId == layerItem.Id)
.Where(it => it.Type == 1)
.Where(it => it.Status == 1)
.Where(it => DateTime.Now <= it.EndTime)
.OrderBy(it => it.ActionTime, OrderByType.Desc)
.AnyAsync();
if (!hasLastAlarm)
{
string layerName = layerItem.LayerNum == 1 ? "上层" : "中层";
AlarmLog alarmLog = new AlarmLog
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
Name =
$"镜壳需补料:{layerItem.RackCode} {layerName} 阈值{layerItem.AlarmNum}",
Code = "镜壳补料",
StoragelocationId = layerItem.Id,
Type = 1,
Status = 1,
ActionTime = DateTime.Now,
// 过X分钟超时jishi
EndTime = DateTime.Now.AddMinutes(2)
};
await Context.Insertable(alarmLog).ExecuteCommandAsync();
var alarmData = new
{
RackCode = layerItem.RackCode,
LayerNum = layerItem.LayerNum,
CurrentPackageCount = currentPackageCount,
AlarmThreshold = layerItem.AlarmNum,
ActionTime = DateTime
.Now.ToString("yyyy-MM-dd HH:mm:ss")
.ToString(),
};
string alarmMessage = System.Text.Json.JsonSerializer.Serialize(
alarmData
);
await notificationHubContext.Clients.All.SendAsync(
"PBL_lack_alarm",
alarmMessage
);
}
}
}
}
// 更新库存
if (updateStoragelocationList.Any())
{
await Context
.Updateable(updateStoragelocationList)
.IgnoreColumns(it => new
{
it.IsLight,
it.IsLackAlarm,
it.AlarmNum,
it.MaxCapacity
})
.ExecuteCommandAsync();
// 发送库存变更Socket通知
string changeMessage = "库存变动";
await notificationHubContext.Clients.All.SendAsync(
"PBL_storagelocation_change",
changeMessage
);
}
// 插入库存变更日志
if (inventoryLogs.Any())
{
await Context.Insertable(inventoryLogs).ExecuteCommandAsync();
}
// 添加延迟以避免频繁查询(暂定3秒防误触)
await Task.Delay(2000, stoppingToken);
}
}
catch (OperationCanceledException)
{
Console.WriteLine("任务已取消");
}
catch (Exception ex)
{
Console.WriteLine(
$"DoanBackGround线程ExecuteAsync异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}"
);
}
}
/// <summary>
/// 刷新料架层和点位表数据
/// </summary>
/// <param name="state"></param>
private async void RefreshData(object state)
{
try
{
Console.WriteLine($"刷新料架点位和按钮");
storagelocationList = await Context.Queryable<Storagelocation>().ToListAsync();
pointPositionList = await Context.Queryable<PlcAddressTable>().ToListAsync();
buttonPositionList = await Context.Queryable<PlcButton>().ToListAsync();
}
catch (Exception ex)
{
Console.WriteLine($"刷新数据异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
// 如果刷新失败,保持现有数据不变
}
}
/// <summary>
/// 更新料架位置的箱子数量并记录库存日志
/// </summary>
/// <param name="storageLocation">料架位置对象</param>
/// <param name="newPackageCount">新的箱子数量</param>
/// <param name="operation">操作类型 (1-出库, 2-入库)</param>
/// <param name="operatorName">操作员名称</param>
/// <param name="inventoryLogs">库存日志列表</param>
private void UpdateAndLog(
Storagelocation storageLocation,
int newPackageCount,
int operation,
string operatorName,
List<Inventorylog> inventoryLogs
)
{
storageLocation.PackageNum = newPackageCount;
storageLocation.UpdatedBy = operatorName;
storageLocation.UpdatedTime = DateTime.Now;
var inventoryLog = new Inventorylog
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
RackCode = storageLocation.RackCode,
Partnumber = storageLocation.Partnumber,
Operation = operation,
PackageNum = Math.Abs(
newPackageCount - storageLocation.PackageNum.GetValueOrDefault()
),
CreatedBy = operatorName,
CreatedTime = DateTime.Now.ToLocalTime()
};
inventoryLogs.Add(inventoryLog);
}
public void Dispose()
{
try
{
_cancellationTokenSource.Cancel();
_executingTask?.GetAwaiter().GetResult(); // 确保任务完成
_cancellationTokenSource.Dispose();
refreshTimer?.Change(Timeout.Infinite, Timeout.Infinite);
refreshTimer?.Dispose();
pLCTool.ConnectClose();
}
catch (Exception ex)
{
Console.WriteLine("DoanBackGround线程Dispose异常:" + ex.Message);
}
}
}
}

View File

@@ -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();
}
}
}
}