PLC获取80-1工位参数

This commit is contained in:
quowingwang 2026-01-24 15:03:13 +08:00
parent 08d8134b98
commit 7541a2ea90

View File

@ -306,10 +306,6 @@ namespace RIZO.Admin.WebApi.PLC.Service
}
}
private async Task<PlcProductionData?> ReadOP080_1DataAsync(Plc plc, string ip, string plcName)
{
throw new NotImplementedException();
}
#region
/// <summary>
@ -414,6 +410,239 @@ namespace RIZO.Admin.WebApi.PLC.Service
};
}
private async Task<PlcProductionData> ReadOP080_1DataAsync(Plc plc, string ip, string workstationCode)
{
// 1. 批量并行读取所有字段(沿用原有结构+增强型读取方法)
var (strFields, intFields, realFields) = await Task.Run(async () => (
// 字符串字段(增强空值和异常处理)
(
await ReadPlcStringAsync(plc, _op080_1StringMap["订单名称"].Addr, _op080_1StringMap["订单名称"].Len),
await ReadPlcStringAsync(plc, _op080_1StringMap["产品名称"].Addr, _op080_1StringMap["产品名称"].Len),
await ReadPlcStringAsync(plc, _op080_1StringMap["合装位机壳_SN"].Addr, _op080_1StringMap["合装位机壳_SN"].Len),
await ReadPlcStringAsync(plc, _op080_1StringMap["合装位PCB_SN"].Addr, _op080_1StringMap["合装位PCB_SN"].Len),
await ReadPlcStringAsync(plc, _op080_1StringMap["拧紧位机壳_SN"].Addr, _op080_1StringMap["拧紧位机壳_SN"].Len),
await ReadPlcStringAsync(plc, _op080_1StringMap["拧紧位PCB_SN"].Addr, _op080_1StringMap["拧紧位PCB_SN"].Len)
),
// Int/DInt字段使用增强版ReadPlcIntAsync/ReadPlcDIntAsync
(
// 基础状态字段
await ReadPlcIntAsync(plc, _op080_1IntMap["运行状态"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["设备模式"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["设备在线状态"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["ByPass"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["生产模式"]),
// 产量统计字段DInt类型需用ReadPlcDIntAsync
//await ReadPlcDIntAsync(plc, _op080_1IntMap["实际产量"]),
//await ReadPlcDIntAsync(plc, _op080_1IntMap["合格数量"]),
//await ReadPlcDIntAsync(plc, _op080_1IntMap["失败数量"]),
// 请求/托盘字段
await ReadPlcIntAsync(plc, _op080_1IntMap["合装工位查询请求"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["合装结果保存请求"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["拧紧结果保存请求"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["合装位托盘号"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["拧紧位托盘号"]),
// 螺钉结果字段Int类型
await ReadPlcIntAsync(plc, _op080_1IntMap["1号螺钉_结果"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["2号螺钉_结果"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["3号螺钉_结果"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["4号螺钉_结果"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["5号螺钉_结果"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["6号螺钉_结果"]),
await ReadPlcIntAsync(plc, _op080_1IntMap["7号螺钉_结果"])
),
// Real字段螺钉扭矩/深度/角度/时间,增强版读取方法)
(
// 1号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["1号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["1号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["1号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["1号螺钉_拧紧时间"]),
// 2号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["2号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["2号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["2号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["2号螺钉_拧紧时间"]),
// 3号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["3号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["3号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["3号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["3号螺钉_拧紧时间"]),
// 4号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["4号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["4号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["4号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["4号螺钉_拧紧时间"]),
// 5号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["5号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["5号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["5号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["5号螺钉_拧紧时间"]),
// 6号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["6号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["6号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["6号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["6号螺钉_拧紧时间"]),
// 7号螺钉Real字段
await ReadPlcRealAsync(plc, _op080_1IntMap["7号螺钉_扭矩"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["7号螺钉_深度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["7号螺钉_角度"]),
await ReadPlcRealAsync(plc, _op080_1IntMap["7号螺钉_拧紧时间"])
)
));
// 2. 解构字段(保持原有逻辑,空值兜底)
var (orderName, productName, hzMachineSN, hzPcbsn, njMachineSN, njPcbsn) = strFields;
var (runStatus, machineModel, onlineStatus, byPass, produceModel,
hzQueryReq, hzSaveReq, njSaveReq,
hzTrayNo, njTrayNo, screw1Result, screw2Result, screw3Result,
screw4Result, screw5Result, screw6Result, screw7Result) = intFields;
// 解构螺钉Real字段按顺序对应
var (
// 1号螺钉
screw1Torque, screw1Depth, screw1Angle, screw1Time,
// 2号螺钉
screw2Torque, screw2Depth, screw2Angle, screw2Time,
// 3号螺钉
screw3Torque, screw3Depth, screw3Angle, screw3Time,
// 4号螺钉
screw4Torque, screw4Depth, screw4Angle, screw4Time,
// 5号螺钉
screw5Torque, screw5Depth, screw5Angle, screw5Time,
// 6号螺钉
screw6Torque, screw6Depth, screw6Angle, screw6Time,
// 7号螺钉
screw7Torque, screw7Depth, screw7Angle, screw7Time
) = realFields;
// 3. 写入保存请求(异步+增强异常日志)
//try
//{
// WritePlcValue(plc, _op080_1IntMap["合装结果保存请求"], "1");
// WritePlcValue(plc, _op080_1IntMap["拧紧结果保存请 求"], "1");
//}
//catch (Exception ex)
//{
// Console.WriteLine($"OP080-1({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 ? "离线" : "在线";
// 计算整体工位合格标识(所有螺钉合格则工位合格)
var allScrewQualified = new[] { screw1Result, screw2Result, screw3Result, screw4Result, screw5Result, screw6Result, screw7Result }
.All(s => s == 1);
string qualificationFlag = allScrewQualified ? "1" : "0";
// 调试日志:输出关键读取结果
Console.WriteLine($"OP080-1({ip})读取结果:产品名称={productName},运行状态={runStatusDesc},订单名称={orderName},螺钉整体合格={allScrewQualified}");
// 5. 构建数据实体严格匹配PlcProductionData字段定义
var plcData = new PlcProductionData
{
// 基础字段
PlcIp = ip.Trim(),
OccurTime = DateTime.Now,
LineCode = "line2", // 可根据实际产线调整
WorkstationCode = workstationCode,
ProductName = productName ?? string.Empty,
ProductCode = njPcbsn ?? string.Empty, // 拧紧位PCB SN作为产品编码
// 合格/返工标志
QualificationFlag = qualificationFlag,
ReworkFlag = reworkFlag,
// 设备状态相关
Automanual = machineModel, // 设备模式0-自动1-手动
Runstatus = runStatus, // 运行状态:1=空闲2=运行中3=故障
OnlineStatus = onlineStatusDesc, // 设备在线状态1=离线,0=在线
ProduceModel = produceModelDesc, // 生产模式描述
// 托盘号相关(拆分合装/拧紧位托盘号)
TrayNo = $"{hzTrayNo}!{njTrayNo}", // 合并显示
AssemblyTrayNo = hzTrayNo.ToString(), // 合装位托盘号
TightenTrayNo = njTrayNo.ToString(), // 拧紧位托盘号
// SN相关字段
SN1 = hzMachineSN ?? string.Empty, // 合装位机壳SN
SN2 = njMachineSN ?? string.Empty, // 拧紧位机壳SN
AssemblyHousingSN = hzMachineSN ?? string.Empty, // 合装位机壳SN
AssemblyPCBSN = hzPcbsn ?? string.Empty, // 合装位PCB SN
TightenHousingSN = njMachineSN ?? string.Empty, // 拧紧位机壳SN
TightenPCBSN = njPcbsn ?? string.Empty, // 拧紧位PCB SN
// 螺钉相关字段(严格匹配实体类)
Screw1Result = screw1Result.ToString(),
Screw1Torque = screw1Torque.ToString("0.00"),
Screw1Depth = screw1Depth.ToString("0.00"),
Screw1Angle = screw1Angle.ToString("0.00"),
Screw1TightenTime = screw1Time.ToString("0.00"),
Screw2Result = screw2Result.ToString(),
Screw2Torque = screw2Torque.ToString("0.00"),
Screw2Depth = screw2Depth.ToString("0.00"),
Screw2Angle = screw2Angle.ToString("0.00"),
Screw2TightenTime = screw2Time.ToString("0.00"),
Screw3Result = screw3Result.ToString(),
Screw3Torque = screw3Torque.ToString("0.00"),
Screw3Depth = screw3Depth.ToString("0.00"),
Screw3Angle = screw3Angle.ToString("0.00"),
Screw3TightenTime = screw3Time.ToString("0.00"),
Screw4Result = screw4Result.ToString(),
Screw4Torque = screw4Torque.ToString("0.00"),
Screw4Depth = screw4Depth.ToString("0.00"),
Screw4Angle = screw4Angle.ToString("0.00"),
Screw4TightenTime = screw4Time.ToString("0.00"),
Screw5Result = screw5Result.ToString(),
Screw5Torque = screw5Torque.ToString("0.00"),
Screw5Depth = screw5Depth.ToString("0.00"),
Screw5Angle = screw5Angle.ToString("0.00"),
Screw5TightenTime = screw5Time.ToString("0.00"),
Screw6Result = screw6Result.ToString(),
Screw6Torque = screw6Torque.ToString("0.00"),
Screw6Depth = screw6Depth.ToString("0.00"),
Screw6Angle = screw6Angle.ToString("0.00"),
Screw6TightenTime = screw6Time.ToString("0.00"),
Screw7Result = screw7Result.ToString(),
Screw7Torque = screw7Torque.ToString("0.00"),
Screw7Depth = screw7Depth.ToString("0.00"),
Screw7Angle = screw7Angle.ToString("0.00"),
Screw7TightenTime = screw7Time.ToString("0.00"),
// 系统字段
CreatedBy = "PLC",
CreatedTime = DateTime.Now,
// 可选字段(无值时置空)
ParamName = string.Empty,
ParamValue = string.Empty,
ProductionCycle = null,
Remark = string.Empty,
UpdatedBy = string.Empty,
UpdatedTime = null,
ProductModel = string.Empty, // 无产品型号数据时置空
WorkstationName = string.Empty, // 无工站名称数据时置空
CameraResult = string.Empty // 无相机结果数据时置空
};
return plcData;
}
#endregion
// 可选:添加连接池清理方法(防止长期运行导致连接泄露)