using Mesnac.Compressor.Entity;
using Mesnac.Equips;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Mesnac.Compressor.Station
{
public class BasePLC
{
public BaseEquip _equip;
public void FindPLC(StationInfo staion)
{
foreach (BaseEquip equip in Factory.Instance.AllEquips.Values)
{
if (equip.Name==staion.PLCName)
{
_equip = equip;
break;
}
}
}
#region PLC操作
public bool WritePLCByByte(StationInfo sta,int start, object[] data)
{
bool result1 = false;
try
{
if (_equip == null)
{
FindPLC(sta);
}
object[] Writedata = new object[data.Length];
result1 = _equip.Write(sta.PLCBlock, start, data);
ICSharpCode.Core.LoggingService.Debug(sta.StationCode + sta.StationName + ":" + sta.PLCBlock + " 偏移量 " + start + "写入结果:" + result1);
return result1;
}
catch (Exception ex)
{
return result1;
}
}
///
/// 到位请求结果反馈
///
///
public void DaoWeiQingQiuHandle(StationInfo sta,int reslut)
{
string message = "";
try
{
if (_equip == null)
{
FindPLC(sta);
}
object[] data = new object[1];
data[0] = reslut;//对照与PLC定义的点位信息
switch (reslut)
{
// OK = 1,//可以工作
// NG = 2,//直接放行
// PreNoWork = 3,//上一工位未工作
// NoMatch = 4,//物料不匹配
// TakeNG = 5//NG物料拿取
case 1:
message = "下发可以工作指令";
break;
case 2:
message = "扫码超时";
break;
case 3:
message = "下发上一工位未工作指令";
break;
case 4:
message = "下发NG物料拿取指令";
break;
case 5:
message = "下发直接放行指令";
break;
case 6:
message = "扫码超时";
break;
default:
break;
}
int address = (int)PLCAddressList.AskWorkResult;
bool result1 = _equip.Write(sta.PLCBlock, address, data);
ICSharpCode.Core.LoggingService.Debug(sta.StationCode + sta.StationName + ":" + message +","+ sta.PLCBlock+" 偏移量 "+ address+"写入结果:"+ result1);
if (reslut == 5)
{
_equip.Write(sta.PLCBlock, (int)2, new object[] { 0 });
ICSharpCode.Core.LoggingService.Debug(sta.StationCode + sta.StationName + "块号" + sta.PLCBlock + "偏移" + (int)2 + ",置位放行请求信号");
}
}
catch (Exception ex)
{
}
}
///
/// 换线错误提示
///
///
public void ChangeError(StationInfo sta)
{
//plc.ChangeError(sta.plcAdress.Writeaddress);
//ICSharpCode.Core.LoggingService.Debug(sta.StationName + "已下发换线扫码成功信号");
}
///
/// 数据保存完成,放行
///
///
public void FinishSavePLCWork(StationInfo sta)
{
try
{
if (_equip == null)
{
FindPLC(sta);
}
object[] data = new object[1];
data[0] = 1;//对照与PLC定义的点位信息
_equip.Write(sta.PLCBlock, (int)PLCAddressList.SavaDataResult, data);
ICSharpCode.Core.LoggingService.Debug(sta.StationCode + sta.StationName + "块号"+ sta.PLCBlock+ "偏移"+(int)PLCAddressList.SavaDataResult + ":下发保存数据成功放行信号");
_equip.Write(sta.PLCBlock, (int)2, new object[] { 0 });
ICSharpCode.Core.LoggingService.Debug(sta.StationCode + sta.StationName + "块号" + sta.PLCBlock + "偏移" + (int)2 + ",置位放行请求信号");
//ICSharpCode.Core.LoggingService.Debug(sta.StationName + ":下发保存数据成功放行信号");
}
catch (Exception ex)
{
}
}
#endregion
}
///
/// PLC地址块,要写入的数据对应地址块的第几位
///
enum PLCAddressList
{
AskWorkResult=3,//工作请求地址,从0开始
SavaDataResult=4//工作完成地址
}
}