using Newtonsoft.Json;
|
using WIDESEA_Comm.LogInfo;
|
using WIDESEA_Core.FreeDB;
|
using WIDESEA_Core.Utilities;
|
using WIDESEA_WCS.WCSClient;
|
|
namespace WIDESEA_WCS
|
{
|
/// <summary>
|
/// PLC显示信息配置
|
/// </summary>
|
public class PLCInfo
|
{
|
/// <summary>
|
/// 所属设备类型
|
/// </summary>
|
public string eqType { get; set; }
|
|
/// <summary>
|
/// 信号中文名称
|
/// </summary>
|
public string io_name { get; set; }
|
|
/// <summary>
|
/// 显示的数据类型
|
/// </summary>
|
public string showType { get; set; }
|
|
/// <summary>
|
/// 数据地址
|
/// </summary>
|
public string dbAddress { get; set; }
|
|
/// <summary>
|
/// 值
|
/// </summary>
|
public object value { get; set; }
|
}
|
|
/// <summary>
|
/// 子车参数配置
|
/// </summary>
|
public class EqConfig
|
{
|
/// <summary>
|
/// 配置名称
|
/// </summary>
|
public string name { get; set; }
|
|
/// <summary>
|
/// 信号地址
|
/// </summary>
|
public string dbAddress { get; set; }
|
|
/// <summary>
|
/// 值
|
/// </summary>
|
public int value { get; set; }
|
|
/// <summary>
|
/// 获取配置字典
|
/// </summary>
|
/// <returns></returns>
|
public static Dictionary<string, short> GetConfigDic(List<EqConfig> configs)
|
{
|
Dictionary<string, short> config_dic = new Dictionary<string, short>();
|
foreach (EqConfig config in configs)
|
{
|
config_dic.Add(config.dbAddress, (short)config.value);
|
}
|
return config_dic;
|
}
|
}
|
|
public class ChilderServer
|
{
|
/*
|
//1、显示基本信息
|
//2、单独控制子车(显示实时位置)
|
//3、单独控制龙门(显示实时位置)
|
//3、单独控制升降(显示实时位置)
|
//4、单独控制夹爪(显示夹爪状态)
|
//5联动运行(现场调试)
|
*/
|
|
FreeDB freeDB = new FreeDB();
|
private static List<PLCInfo> plcInfos;
|
|
public ChilderServer()
|
{
|
if (plcInfos == null)
|
{
|
string config = freeDB.DataBase.Ado.QuerySingle<string>("select config from dt_equipmentinfo where equipment_name='子车1'");
|
plcInfos = JsonConvert.DeserializeObject<List<PLCInfo>>(config);
|
}
|
}
|
|
/// <summary>
|
/// 获取PLC信息
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent GetChidlerInfo()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
//长链接断开后,会自动默认使用短连接
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
GetChilderInfo(client);
|
content.Data = new { info = plcInfos };
|
content.OK();
|
}
|
catch (Exception ex)
|
{
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 获取子车参数
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent GetChidlerParm()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
string config = freeDB.DataBase.Ado.QuerySingle<string>("select eqconfig from dt_equipmentinfo where equipment_name='子车1'");
|
content.Data = config;
|
content.OK();
|
}
|
catch (Exception ex)
|
{
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
#region 子车操作
|
public void Remark()
|
{
|
//运行流程
|
//1、启动子车、连接子车PLC(刚启动时子车所有状态为初始状态)
|
//2、子车、龙门、升降、夹爪全部给使能。所有初始速度全部加载好
|
//3、点击回原点
|
//4、(设置为自动状态)依次控制子车、龙门、升降、夹爪运行
|
|
|
//完成后3种操作
|
//1、原地处理。所有操作不动。
|
//处理完成后
|
//处理完成或下降到之前的位置,
|
//松开夹爪。
|
//然后升降机回位。
|
//龙门还要回到原点
|
|
//2、龙门回到原点
|
//升降运动到指定位置(原点)
|
//人工处理故障板
|
//人工处理完成
|
//提起故障板
|
//龙门回到提故障板的位置
|
|
//3、龙门回到原点
|
//升降运动到指定位置(原点)
|
//子车回原点
|
//人工处理故障板
|
//人工处理完成
|
//在送回
|
}
|
|
|
/// <summary>
|
/// 回原点
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent ToBasePoint()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
var configDic = GetEqConfig();
|
client.Write("66", true);//子车伺服使能1
|
Thread.Sleep(1000);
|
client.Write("67", true);//子车伺服使能2
|
Thread.Sleep(1000);
|
client.Write("209", true);//龙门使能1
|
Thread.Sleep(1000);
|
client.Write("210", true); //龙门使能2
|
Thread.Sleep(1000);
|
client.Write("237", true);//升降使能
|
Thread.Sleep(1000);
|
client.Write("212", true);//夹紧使能
|
Thread.Sleep(1000);
|
|
client.Write("1036", configDic["1036"]);//子车行走速度
|
client.Write("1038", configDic["1038"]);//子车手动速度
|
client.Write("1040", configDic["1040"]); //子车扫码枪慢速定位速度
|
client.Write("1044", configDic["1044"]);//龙门行走速度
|
client.Write("1046", configDic["1046"]);//龙门手动速度
|
client.Write("1048", configDic["1048"]);//龙门回零速度
|
client.Write("1050", configDic["1050"]);//升降行走速度
|
client.Write("1052", configDic["1052"]);//升降手动速度
|
client.Write("1054", configDic["1054"]);//升降回零速度
|
client.Write("1056", configDic["1056"]);//夹紧行走速度
|
client.Write("1058", configDic["1058"]);//夹紧手动速度
|
client.Write("1060", configDic["1060"]);//夹紧回零速度
|
client.Write("1042", configDic["1042"]);//子车回零速度
|
|
Thread.Sleep(500);
|
client.Write("241", true);//请求回原点信号
|
|
WriteLog.Write_Log("手动操作", "子车", "手动回原点成功", configDic);
|
content.OK("回原点成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "手动回原点异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 启动子车
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent Run_Child(int point)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
WriteRun(client, 1010, point, "64");
|
|
WriteLog.Write_Log("手动操作", "子车", "启动子车" + point.ToString());
|
content.OK("启动成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "启动子车异常" + point.ToString(), ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
|
/// <summary>
|
/// 停止子车当前动作(子车运动、龙门运动、升降运动、夹爪运动)
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent Stop_Child()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
//写入停止
|
client.Write("242", true);
|
|
WriteLog.Write_Log("手动操作", "子车", "停止子车");
|
content.OK("停止成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "停止子车异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
|
/// <summary>
|
/// 复位子车报警
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent Reset_Child()
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
client.Write("244", true);
|
Thread.Sleep(2000);
|
client.Write("244", false);
|
|
WriteLog.Write_Log("手动操作", "子车", "复位子车成功");
|
content.OK("复位成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "复位子车异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 启动龙门
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public WebResponseContent Run_Gantry(int point)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
WriteRun(client, 1028, point, "208");
|
|
WriteLog.Write_Log("手动操作", "子车", "启动龙门" + point.ToString());
|
content.OK("启动成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "启动龙门异常" + point.ToString(), ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
|
/// <summary>
|
/// 启动升降
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public WebResponseContent Run_Lifting(int point)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
WriteRun(client, 1032, point, "211");
|
|
WriteLog.Write_Log("手动操作", "子车", "启动升降" + point.ToString());
|
content.OK("启动成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "启动升降异常" + point.ToString(), ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 夹紧夹爪
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public WebResponseContent Colse_Handle(int point)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
//夹紧夹爪
|
client.Write("213", true);
|
|
WriteLog.Write_Log("手动操作", "子车", "夹紧夹爪");
|
content.OK("操作成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "夹紧夹爪异常" + point.ToString(), ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 松开夹爪
|
/// </summary>
|
/// <param name="point"></param>
|
/// <returns></returns>
|
public WebResponseContent Open_Handle(int point)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
//松开夹爪
|
client.Write("214", true);
|
|
WriteLog.Write_Log("手动操作", "子车", "松开夹爪");
|
content.OK("操作成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "松开夹爪异常" + point.ToString(), ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 更改自动状态
|
/// </summary>
|
/// <param name="state"></param>
|
/// <returns></returns>
|
public WebResponseContent ChangeAutoState(string state)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
if (state == "手动")
|
{
|
client.Write("233", true); //手动状态
|
}
|
else if (state == "自动")
|
{
|
client.Write("233", false); //自动状态
|
}
|
|
WriteLog.Write_Log("手动操作", "子车", "更改设备手自动状态为:" + state + "成功");
|
content.OK("操作成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "更改设备手自动状态为:" + state + "异常", ex.Message);
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
/// <summary>
|
/// 修改子车参数
|
/// </summary>
|
/// <param name="state"></param>
|
/// <returns></returns>
|
public WebResponseContent SetChilderParm(List<EqConfig> configs)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
var client = PLCClient.Clients.Where(t => t.PLCName == "子车1").FirstOrDefault();
|
if (client == null || !client.IsConnected)
|
{
|
return content.Error($"PLC{"子车1"}未连接");
|
}
|
|
var configDic = EqConfig.GetConfigDic(configs);
|
|
client.Write("1036", configDic["1036"]);//子车行走速度
|
client.Write("1038", configDic["1038"]);//子车手动速度
|
client.Write("1040", configDic["1040"]); //子车扫码枪慢速定位速度
|
client.Write("1044", configDic["1044"]);//龙门行走速度
|
client.Write("1046", configDic["1046"]);//龙门手动速度
|
client.Write("1048", configDic["1048"]);//龙门回零速度
|
client.Write("1050", configDic["1050"]);//升降行走速度
|
client.Write("1052", configDic["1052"]);//升降手动速度
|
client.Write("1054", configDic["1054"]);//升降回零速度
|
client.Write("1056", configDic["1056"]);//夹紧行走速度
|
client.Write("1058", configDic["1058"]);//夹紧手动速度
|
client.Write("1060", configDic["1060"]);//夹紧回零速度
|
client.Write("1042", configDic["1042"]);//子车回零速度
|
|
string configJson = JsonConvert.SerializeObject(configs);
|
int res = freeDB.DataBase.Ado.ExecuteNonQuery("update dt_equipmentinfo set eqconfig=@eqconfig where equipment_name='子车1'", new { eqconfig = configJson });
|
|
WriteLog.Write_Log("手动操作", "子车", "修改子车参数成功", configs);
|
content.OK("操作成功!");
|
}
|
catch (Exception ex)
|
{
|
WriteLog.Write_Log("手动操作", "子车", "修改子车参数异常", new { 内容 = configs, 错误 = ex.Message });
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
|
|
/// <summary>
|
/// 一键运行子车
|
/// </summary>
|
/// <returns></returns>
|
public WebResponseContent RunChilder()
|
{
|
WebResponseContent content = new WebResponseContent();
|
return content.Error("开发调试中……");
|
|
ChilderCarJob.CurrentState = "子车开始";
|
ChilderCarJob.childerPoint = 1200;
|
ChilderCarJob.gantryPoint = 1500;
|
return content.OK("启动成功");
|
}
|
#endregion
|
|
/// <summary>
|
/// 写入设备运行
|
/// </summary>
|
/// <param name="address">位置地址</param>
|
/// <param name="point">位置值</param>
|
/// <param name="runAddress">启动地址</param>
|
private void WriteRun(PLCClient client, int address, int point, string runAddress)
|
{
|
client.Write(runAddress, false);//复位启动命令
|
Thread.Sleep(1000);
|
|
int startPoint = point / 1000;
|
int endPoint = point % 1000;
|
string address1 = address.ToString();
|
string address2 = (address + 1).ToString();
|
|
client.Write(address1, (short)startPoint);
|
client.Write(address2, (short)endPoint);//写入目的地址
|
|
var point1 = client.Read<short>(address1);
|
var point2 = client.Read<short>(address2);
|
if (startPoint != point1)
|
{
|
client.Write(address1, (short)startPoint);
|
WriteLog.Write_Log("手动操作", "子车", $"启动子车值不一致写入{startPoint},复读{point1}");
|
}
|
if (endPoint != point2)
|
{
|
client.Write(address2, (short)endPoint);
|
WriteLog.Write_Log("手动操作", "子车", $"启动子车值不一致写入{endPoint},复读{point2}");
|
}
|
|
Thread.Sleep(1000);//必须延时
|
client.Write(runAddress, true);//写入启动命令
|
}
|
|
/// <summary>
|
/// 读取子车信息
|
/// </summary>
|
/// <param name="client"></param>
|
private void GetChilderInfo(PLCClient client)
|
{
|
foreach (var item in plcInfos)
|
{
|
if (item.showType.Contains("bool"))
|
{
|
item.value = client.Read<bool>(item.dbAddress);
|
}
|
else if (item.showType == "int")
|
{
|
var address = int.Parse(item.dbAddress) + 1;
|
item.value = client.Read<short>(item.dbAddress) * 1000 + client.Read<short>(address + "");
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取子车配置
|
/// </summary>
|
/// <returns></returns>
|
private Dictionary<string, short> GetEqConfig()
|
{
|
string configJson = freeDB.DataBase.Ado.QuerySingle<string>("select eqconfig from dt_equipmentinfo where equipment_name='子车1'");
|
var configList = JsonConvert.DeserializeObject<List<EqConfig>>(configJson);
|
var config = EqConfig.GetConfigDic(configList);
|
return config;
|
}
|
}
|
}
|