PLC采集数据

This commit is contained in:
quowingwang 2026-01-25 14:34:14 +08:00
parent 25c626e4ae
commit 9c20d9f905

View File

@ -38,6 +38,33 @@ namespace RIZO.Admin.WebApi.PLC.Service
private readonly ConcurrentDictionary<string, (Plc Client, DateTime LastUsedTime)> _plcConnPool = new(); // 连接池
#region PLC地址块儿映射
// OP020-2 专属地址映射合盖工位DB1001
private readonly Dictionary<string, (string Addr, int Len)> _op020_2StringMap = new()
{
{ "报警信息", ("DB1001.DBB58", 48) }, // Array[1..48] of Byte
{ "产品型号", ("DB1001.DBB1000", 48) }, // String[48]
{ "产品名称", ("DB1001.DBB1054", 48) }, // String[48]
{ "SN", ("DB1001.DBB2106", 48) } // String[48]
};
private readonly Dictionary<string, string> _op020_2IntMap = new()
{
{ "运行状态", "DB1001.DBW0" }, // Int - 1=空闲2=运行中3=故障
{ "设备模式", "DB1001.DBW2" }, // Int - 1=空模式2=手动4=初始化8=自动16=CycleStop
{ "设备在线状态", "DB1001.DBW4" }, // Int - 1=离线,0=在线
//{ "ByPass", "DB1001.DBW6" }, // Int - 1=ByPass,0=正常模式
{ "生产模式", "DB1001.DBW8" }, // Int - 1=正常模式;2=清线模式;4=返工模式;8=换型模式;16=预热模式
//{ "实际产量", "DB1001.DBD1104" }, // DInt
//{ "合格数量", "DB1001.DBD1108" }, // DInt
//{ "失败数量", "DB1001.DBD1112" }, // DInt
{ "上传请求", "DB1001.DBW2002" }, // Int - 1=请求开始0=无请求
{ "托盘号", "DB1001.DBW2054" }, // Int
{ "总结果", "DB1001.DBW2158" }, // Int - 一个托盘上传四次结果
//{ "节拍时间", "DB1001.DBD2988" } // Real
};
// OP070-1 专属地址映射
private readonly Dictionary<string, (string Addr, int Len)> _op070_1StringMap = new()
{
@ -333,7 +360,12 @@ namespace RIZO.Admin.WebApi.PLC.Service
int iQueryRequest = 0;
int iSaveRequest = 0;
string strSaveRequest = "";
if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3")
if (plcName == "OP020-2")
{
//查询请求20260125 PM
iSaveRequest = await ReadPlcIntAsync(plc, _op020_2IntMap["上传请求"]);
}
else if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3")
{
iQueryRequest = await ReadPlcIntAsync(plc, _op070_1IntMap["查询请求"]);
iSaveRequest = await ReadPlcIntAsync(plc, _op070_1IntMap["保存请求"]);
@ -381,7 +413,15 @@ namespace RIZO.Admin.WebApi.PLC.Service
if (iSaveRequest == 1)
{
// 5. 多工位专属数据读取(预留扩展,逻辑隔离)
if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3")
if (plcName == "OP020-2")
{
prodData = await ReadOP020_2DataAsync(plc, ip, plcName);
}
if (plcName == "OP020-3")
{
prodData = await ReadOP020_2DataAsync(plc, ip, plcName);
}
else if (plcName == "OP070-1" || plcName == "OP070-2" || plcName == "OP070-3")
{
prodData = await ReadOP070_1DataAsync(plc, ip, plcName);
}
@ -443,6 +483,93 @@ namespace RIZO.Admin.WebApi.PLC.Service
#region
/// <summary>
/// 读取OP020-2数据合盖工位
/// </summary>
private async Task<PlcProductionData> ReadOP020_2DataAsync(Plc plc, string ip, string workstationCode)
{
try
{
// 1. 批量并行读取所有字段
var (strFields, intFields) = await Task.Run(async () => (
// 字符串字段
(
await ReadPlcStringAsync(plc, _op020_2StringMap["产品型号"].Addr, _op020_2StringMap["产品型号"].Len),
await ReadPlcStringAsync(plc, _op020_2StringMap["产品名称"].Addr, _op020_2StringMap["产品名称"].Len),
await ReadPlcStringAsync(plc, _op020_2StringMap["SN"].Addr, _op020_2StringMap["SN"].Len)
),
// Int字段
(
await ReadPlcIntAsync(plc, _op020_2IntMap["运行状态"]),
await ReadPlcIntAsync(plc, _op020_2IntMap["设备模式"]),
await ReadPlcIntAsync(plc, _op020_2IntMap["设备在线状态"]),
await ReadPlcIntAsync(plc, _op020_2IntMap["生产模式"]),
await ReadPlcIntAsync(plc, _op020_2IntMap["托盘号"]),
await ReadPlcIntAsync(plc, _op020_2IntMap["总结果"])
)
));
// 2. 解构字段
var (productModel, productName, sn) = strFields;
var (runStatus, machineModel, onlineStatus, produceModel,trayNo, totalResult) = intFields;
// 3. 复位上传请求
//try
//{
// WritePlcValue(plc, _op020_2IntMap["上传请求"], "0");
//}
//catch (Exception ex)
//{
// Console.WriteLine($"OP020-2({ip})写上传请求失败:{ex.Message}");
//}
// 4. 业务逻辑计算
var reworkFlag = produceModel == 4 ? "1" : "0";
string produceModelDesc = produceModel switch
{
1 => "正常模式",
2 => "清线模式",
4 => "返工模式",
8 => "换型模式",
16 => "预热模式",
_ => $"未知({produceModel})"
};
string runStatusDesc = runStatus switch { 1 => "空闲", 2 => "运行中", 3 => "故障", _ => $"未知({runStatus})" };
string onlineStatusDesc = onlineStatus == 1 ? "离线" : "在线";
string qualificationFlag = totalResult switch { 1 => "1", 2 => "0", _ => totalResult.ToString() };
// 调试日志
Console.WriteLine($"OP020-2({ip})读取结果:产品型号={productModel},运行状态={runStatusDesc},总结果={qualificationFlag}");
// 5. 构建数据实体
return new PlcProductionData
{
PlcIp = ip.Trim(),
OccurTime = DateTime.Now,
LineCode = "line2",
WorkstationCode = workstationCode,
ProductModel = productModel ?? string.Empty,
ProductName = productName ?? string.Empty,
ProductCode = sn ?? string.Empty,
SN1 = sn ?? string.Empty,
SN2 = sn ?? string.Empty,
QualificationFlag = qualificationFlag,
ReworkFlag = reworkFlag,
Automanual = machineModel,
Runstatus = runStatus,
OnlineStatus = onlineStatusDesc,
ProduceModel = produceModelDesc,
TrayNo = trayNo.ToString(),
CreatedBy = "PLC",
CreatedTime = DateTime.Now
};
}
catch (Exception ex)
{
Console.WriteLine($"OP020-2({ip})数据读取异常:{ex.Message}");
return null;
}
}
/// <summary>
/// OP070-1数据读取
/// </summary>
private async Task<PlcProductionData> ReadOP070_1DataAsync(Plc plc, string ip,string workstationCode)