fix - 配方写入PLC整块写入

master
SoulStar 2 months ago
parent fb3c6af3ca
commit 9f728c289d

@ -21,7 +21,7 @@ namespace HighWayIot.Plc.PlcHelper
/// <returns></returns> /// <returns></returns>
public bool UploadToPLC(ZxRecipeParaEntity paraEntity, List<ZxRecipePositionParaEntity> positionParaEntity) public bool UploadToPLC(ZxRecipeParaEntity paraEntity, List<ZxRecipePositionParaEntity> positionParaEntity)
{ {
//byte[] bytes = new byte[240]; byte[] bytes = new byte[240];
//SPEC编号写入 //SPEC编号写入
try try
{ {
@ -34,14 +34,15 @@ namespace HighWayIot.Plc.PlcHelper
} }
//SPEC名称写入 //SPEC名称写入
if (!PlcConnect.PlcWrite2("D290", paraEntity.SpecName, DataTypeEnum.String).IsSuccess) var specNameBytes = Encoding.ASCII.GetBytes(paraEntity.SpecName);
return false; Array.Copy(specNameBytes, 0, bytes, 0, specNameBytes.Length);
//if (!PlcConnect.PlcWrite2("D290", paraEntity.SpecName, DataTypeEnum.String).IsSuccess)
// return false;
//工位参数写入 //工位参数写入
foreach (ZxRecipePositionParaEntity e in positionParaEntity) foreach (ZxRecipePositionParaEntity e in positionParaEntity)
{ {
if (!SelectSetPositionPara(e)) bytes = SelectSetPositionPara(e, bytes);
return false;
} }
//公共参数写入 //公共参数写入
@ -56,23 +57,34 @@ namespace HighWayIot.Plc.PlcHelper
bitDatas[1] = bitDatas[1].SetBoolByIndex(0, paraEntity.S9 ?? false); bitDatas[1] = bitDatas[1].SetBoolByIndex(0, paraEntity.S9 ?? false);
ushort bitData = bitDatas.FromBytes(); ushort bitData = bitDatas.FromBytes();
if (!PlcConnect.PlcWrite2("D390", bitData, DataTypeEnum.UInt16).IsSuccess) //if (!PlcConnect.PlcWrite2("D390", bitData, DataTypeEnum.UInt16).IsSuccess)
return false; // return false;
if (!PlcConnect.PlcWrite2("D391", paraEntity.RimInch ?? 0, DataTypeEnum.UInt16).IsSuccess) //if (!PlcConnect.PlcWrite2("D391", paraEntity.RimInch ?? 0, DataTypeEnum.UInt16).IsSuccess)
return false; // return false;
if (!PlcConnect.PlcWrite2("D392", paraEntity.LightWidth ?? 0, DataTypeEnum.UInt16).IsSuccess) //if (!PlcConnect.PlcWrite2("D392", paraEntity.LightWidth ?? 0, DataTypeEnum.UInt16).IsSuccess)
return false; // return false;
if (!PlcConnect.PlcWrite2("D393", paraEntity.SlowDistance ?? 0, DataTypeEnum.UInt16).IsSuccess) //if (!PlcConnect.PlcWrite2("D393", paraEntity.SlowDistance ?? 0, DataTypeEnum.UInt16).IsSuccess)
return false; // return false;
if (!PlcConnect.PlcWrite2("D394", paraEntity.StopDistance ?? 0, DataTypeEnum.UInt16).IsSuccess) //if (!PlcConnect.PlcWrite2("D394", paraEntity.StopDistance ?? 0, DataTypeEnum.UInt16).IsSuccess)
return false; // return false;
if (!PlcConnect.PlcWrite2("D398", paraEntity.TireWeight ?? 0, DataTypeEnum.Float).IsSuccess) //if (!PlcConnect.PlcWrite2("D398", paraEntity.TireWeight ?? 0, DataTypeEnum.Float).IsSuccess)
return false; // return false;
var bytes1 = BitConverter.GetBytes(bitData);
var bytes2 = BitConverter.GetBytes((ushort?)paraEntity.RimInch ?? 0);
var bytes3 = BitConverter.GetBytes((ushort?)paraEntity.LightWidth ?? 0);
var bytes4 = BitConverter.GetBytes((ushort?)paraEntity.SlowDistance ?? 0);
var bytes5 = BitConverter.GetBytes((ushort?)paraEntity.StopDistance ?? 0);
var bytes6 = BitConverter.GetBytes(paraEntity.TireWeight ?? 0);
Array.Copy(bytes1, 0, bytes, (390 - 290) * 2, bytes1.Length);
Array.Copy(bytes2, 0, bytes, (391 - 290) * 2, bytes1.Length);
Array.Copy(bytes3, 0, bytes, (392 - 290) * 2, bytes1.Length);
Array.Copy(bytes4, 0, bytes, (393 - 290) * 2, bytes1.Length);
Array.Copy(bytes5, 0, bytes, (394 - 290) * 2, bytes1.Length);
Array.Copy(bytes6, 0, bytes, (398 - 290) * 2, bytes1.Length);
//return PlcConnect.MelsecInstance2.Write("D290", bytes).IsSuccess;
return true; return PlcConnect.MelsecInstance2.Write("D290", bytes).IsSuccess;
} }
/// <summary> /// <summary>
@ -80,38 +92,32 @@ namespace HighWayIot.Plc.PlcHelper
/// </summary> /// </summary>
/// <param name=""></param> /// <param name=""></param>
/// <returns></returns> /// <returns></returns>
private bool SelectSetPositionPara(ZxRecipePositionParaEntity entity) private byte[] SelectSetPositionPara(ZxRecipePositionParaEntity entity, byte[] bytes)
{ {
if (entity.Position == 1) if (entity.Position == 1)
{ {
if (!SetPositionPara(entity, 310)) return SetPositionPara(entity, 310, bytes);
return false;
} }
else if (entity.Position == 2) else if (entity.Position == 2)
{ {
if (!SetPositionPara(entity, 320)) return SetPositionPara(entity, 320, bytes);
return false;
} }
else if (entity.Position == 3) else if (entity.Position == 3)
{ {
if (!SetPositionPara(entity, 330)) return SetPositionPara(entity, 330, bytes);
return false;
} }
else if (entity.Position == 4) else if (entity.Position == 4)
{ {
if (!SetPositionPara(entity, 350)) return SetPositionPara(entity, 350, bytes);
return false;
} }
else if (entity.Position == 5) else if (entity.Position == 5)
{ {
if (!SetPositionPara(entity, 370)) return SetPositionPara(entity, 370, bytes);
return false;
} }
else else
{ {
return false; return null;
} }
return true;
} }
/// <summary> /// <summary>
@ -120,14 +126,14 @@ namespace HighWayIot.Plc.PlcHelper
/// <param name="entity"></param> /// <param name="entity"></param>
/// <param name="add"></param> /// <param name="add"></param>
/// <returns></returns> /// <returns></returns>
private bool SetPositionPara(ZxRecipePositionParaEntity entity, int add) private byte[] SetPositionPara(ZxRecipePositionParaEntity entity, int add, byte[] bytes)
{ {
for (int i = 1; i <= 10; i++) for (int i = 1; i <= 10; i++)
{ {
var prop = entity.GetType().GetProperty($"E{i}"); var prop = entity.GetType().GetProperty($"E{i}");
// 检查属性是否存在 // 检查属性是否存在
if (prop == null) if (prop == null)
return false; return null;
// 获取属性值 // 获取属性值
object value = prop.GetValue(entity); object value = prop.GetValue(entity);
@ -136,8 +142,9 @@ namespace HighWayIot.Plc.PlcHelper
if (value == null) if (value == null)
{ {
// 根据业务需求选择返回false或使用默认值例如0 // 根据业务需求选择返回false或使用默认值例如0
if (!PlcConnect.PlcWrite2($"D{add}", 0, DataTypeEnum.UInt16).IsSuccess) continue;
return false; //if (PlcConnect.PlcWrite2($"D{add}", 0, DataTypeEnum.UInt16).IsSuccess)
// return false;
} }
// 尝试转换值 // 尝试转换值
@ -149,16 +156,18 @@ namespace HighWayIot.Plc.PlcHelper
catch catch
{ {
// 转换失败(如类型不匹配) // 转换失败(如类型不匹配)
return false; continue;
} }
// 写入PLC并检查结果 // 写入PLC并检查结果
if (!PlcConnect.PlcWrite2($"D{add}", valueToWrite, DataTypeEnum.UInt16).IsSuccess) var resultbytes = BitConverter.GetBytes(valueToWrite);
return false; Array.Copy(resultbytes, 0, bytes, (add - 290) * 2, resultbytes.Length);
//if (!PlcConnect.PlcWrite2($"D{add}", valueToWrite, DataTypeEnum.UInt16).IsSuccess)
// return false;
add++; add++;
} }
return true; return bytes;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save