using Hanhe.iWCS.Business; using Hanhe.iWCS.Common; using Hanhe.iWCS.Interface; using Hanhe.iWCS.TaizhouGEMTwoTCP; using Hanhe.iWCS.Model; using System; using System.Collections.Generic; using System.Text; using System.Linq; using MongoDB.Bson; using Hanhe.iWCS.MData; using Newtonsoft.Json; using MongoDB.Driver.Builders; using static Hanhe.iWCS.TaizhouGEMTwoProtocol.MESHelper; using MongoDB.Driver; using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ProcessHelper; using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ApiHelper; using System.Threading; using Hanhe.iWCS.Model.AMS; using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ERPService; using static Hanhe.iWCS.TaizhouGEMTwoProtocol.AMSHelper; using static Hanhe.iWCS.TaizhouGEMTwoProtocol.ProtocolAnalysis; using log4net.Appender; using System.Net.Configuration; using static System.Runtime.CompilerServices.RuntimeHelpers; using System.Reflection.Emit; using System.Text.RegularExpressions; namespace Hanhe.iWCS.TaizhouGEMTwoProtocol { public class PLCControl : IPLCControl { private EquipmentCommandEQBLL commandBLL = new EquipmentCommandEQBLL(); /// /// PLC 数据发送 /// /// /// public void SendMessage(EquipmentCommandEQ command) { bool sended = SessionInstance.Instance.PLCSend(command.EquipmentIP, int.Parse(command.EquipmentPort), command.CommandText); if (sended) { //CMMLog.Info("设备指令发送成功;EquipmentCode=" + command.EquipmentCode + "-指令=" + command.CommandText); commandBLL.UpdateCommandEQStatus(command._id, Constants.COMMANDEQ_STATUS_SENDED, command.SendCount); } else { //CMMLog.Info("设备指令发送失败;EquipmentCode=" + command.EquipmentCode + "-指令=" + command.CommandText); commandBLL.UpdateCommandEQStatus(command._id, Constants.COMMANDEQ_STATUS_SEND_ERRORS, command.SendCount); } } /// /// 处理光电信息 /// /// public static void Analysis(string data) { //3f 00 01 00 00 00 24 24 } #region 纯3楼设备任务 #region 3楼包装机取料至复称平台流程——无需回报WMS任务状态——3楼包装取料——已完成(待测试,时间戳处理) #region 收到包装机下料信号1,开始判断复称平台是否满足条件,选择性下发任务 /// /// 3楼包装机取料至复称平台流程——任务开始前,判断包装机有没有下线信号,并判断当前复称平台是否NG以及有没有任务在进行中 /// /// internal static void CheckPackingMachine(Settings.PlcInfo pmInfo) { //var count = MongoDBSingleton.Instance.FindOne<>//occupy var count = MongoDBSingleton.Instance.FindOne(Query.EQ("occupy", "1"), "MachineInfo"); //检查包装机通道0是否有下料信号,如果有生成下料任务。线程循环读取,无需设置循环。——通道0参数为1 if(count == null) { // var secondInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("Bit", pmInfo.location), "secondResult"); // if(secondInfo == null || secondInfo.Status == "OK") // { if (PickUpStartFree(pmInfo.location) && PickUpEndFree(pmInfo.location)) { try { var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = pmInfo.readAddr, host = pmInfo.ip, port = pmInfo.port }); if (result != null && result.errCode == 0) { CMMLog.Debug($"包装下线流程-{pmInfo.location}:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}"); //Console.WriteLine($"包装下线流程:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}"); if (result != null && result.errCode == 0 && result.result.Count() > 0) { //1:取料 if (result.result[0] == 1) { string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location; //包装下线任务,终点为复称位置(这里判断包装机起点,复称起点终点均无任务,则推送任务) if (ProcessHelper.PickUpStartFree(pmInfo.location) && ProcessHelper.CheckEndFree(PlcBit02) && ProcessHelper.PickUpStartFree(PlcBit02)) { //判断复称位置,没有货并且不为NG(ng参数0,1,2——0,不确定(包装机刚开始工作时)1,ok 2,ng) var state = GetSecondWeighState(pmInfo.location); CMMLog.Debug($"包装下线流程-{pmInfo.location}:判断复称位置空满状态为:{state.full},ng状态为:{state.ng}"); if (state.full == 0) { if (Settings.mesOpen == "0") { //可以生成任务,调mes接口获取生产信息 2022.7.28 变更 MES可能需要1~2年才能上线,先把MES功能注销 var info = MESHelper.GetPackingMachineInfo(pmInfo.location); if (info != null) { CMMLog.Debug($"包装下线流程:获取MES之后进入info判断!"); Console.WriteLine($"包装下线流程:获取MES之后进入info判断!"); var trayCode = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { addr = pmInfo.readAddr + 10, dataNum = 90,//ERP变更:90 原先:80 host = pmInfo.ip, port = pmInfo.port }); if (trayCode.errCode == 0 && trayCode.result.Length == 90) { //获取托盘码等信息 读取通道 10、11、12的数据作为托盘码 读取其它通道 重量 叠包等信息 所有数据存入MachineInfo表 string location = ""; GetMachineData(trayCode.result,ref location); var tray = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", pmInfo.location), "MachineInfo"); if (tray != null) { if (tray.addState == 0) { MongoDBSingleton.Instance.Remove(Query.EQ("machineNo", pmInfo.location), "MachineInfo", RemoveFlags.None); if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove(Query.EQ("machineNo", pmInfo.location), "MachineInfoTwo", RemoveFlags.Single); CMMLog.Debug($"包装下线流程:叠托层数为0,不允许生成任务"); } else if (tray.trayCode != "0" && tray.trayCode != "" && tray.trayCode != null) { //AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, GetSecondWeighBit(), "3楼包装取料", 1, JsonConvert.SerializeObject(info)); int i = 1; while (i == 1) { var amsResult = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, PlcBit02, "3楼包装取料", 0, tray.trayCode); if (amsResult.success) { i = 2; string weight = (int.Parse(tray.oneTrayWeight) / 100).ToString(); ERPService.packageInfo(tray.machineNo, tray.trayCode, "", weight); CMMLog.Debug($"包装下线流程:AMS调用API成功!"); } else CMMLog.Debug($"包装下线流程:AMS调用API失败,开始重新调用!"); } CMMLog.Debug($"包装下线流程:接受并读取包装机通道里面的物料信息,并将其写入MachineInfo中间表中!接收到的信息:{JsonConvert.SerializeObject(trayCode.result)}"); } } else { CMMLog.Debug($"包装下线流程:tray==null!"); Console.WriteLine($"包装下线流程:tray==null!"); } } else { CMMLog.Debug($"包装下线流程:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}"); Console.WriteLine($"包装下线流程:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}"); } } else CMMLog.Debug($"包装下线流程:MES接口数据返回空值!MES接口数据:{info}"); } else if (Settings.mesOpen == "1") { //可以生成任务,调mes接口获取生产信息 2022.7.28 变更 MES可能需要1~2年才能上线,先把MES功能注销 CMMLog.Debug($"包装下线流程-{pmInfo.location}:获取MES之后进入info判断!"); var trayCode = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { addr = pmInfo.readAddr + 10, dataNum = 90,////ERP变更:90 原先:80 host = pmInfo.ip, port = pmInfo.port }); if (trayCode.errCode == 0 && trayCode.result.Length == 90) { //获取托盘码等信息 读取通道 11、12、13的数据作为托盘码 读取其它通道 重量 叠包等信息 所有数据存入MachineInfo表 string location = ""; string flLoc = ""; GetMachineData(trayCode.result, ref location, false); if(pmInfo.location.Contains("FLZT")) { flLoc = pmInfo.location; pmInfo.location = location; } var tray = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", pmInfo.location), "MachineInfo"); if (tray != null) { bool IsContLaterCode = true; if (ERPService.ERPSwitch01 == "1") { string employeeId = "G" + tray.trayCodeWeight.PadLeft(7, '0'); // 判断当前【员工编号】通道信息读出来的员工编号是否已经存在于我们的员工信息表-ERPEmployeeTable(查询字段-employee_id) var erpEmployeeInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("employee_id", employeeId), "ERPEmployeeTable"); if (erpEmployeeInfo == null) IsContLaterCode = false; } if (IsContLaterCode) { if (tray.addState == 0) { MongoDBSingleton.Instance.Remove(Query.EQ("machineNo", pmInfo.location), "MachineInfo", RemoveFlags.None); if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove(Query.EQ("machineNo", pmInfo.location), "MachineInfoTwo", RemoveFlags.None); CMMLog.Debug($"包装下线流程-{pmInfo.location}:叠托层数为0,不允许生成任务"); } else if (tray.trayCode != "0" && !string.IsNullOrEmpty(tray.trayCode)) { string timeStamp = ProcessHelper.GetTimeStamp(31, 1, 1); pmInfo.location = !string.IsNullOrEmpty(flLoc) ? flLoc : pmInfo.location; HHAmsExecuteResult req = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, PlcBit02, "3楼包装取料", 0, tray.trayCode, timeStamp); if (req.success) { string weight = (double.Parse(tray.oneTrayWeight) / 100).ToString(); //int weight = int.Parse(tray.oneTrayWeight) / 100; ERPService.packageInfo(tray.machineNo, tray.trayCode, tray.lotNo, weight); } } } else { CMMLog.Info($"包装下线流程-{pmInfo.location}:员工编码不存在,员工编码:G{tray.trayCodeWeight.PadLeft(7, '0')}"); //检索员工信息失败写入对方通道值1 读取地址+3 var wirteall01 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.readAddr + 3, host = pmInfo.ip, port = pmInfo.port, data = 1 }); } } else CMMLog.Debug($"包装下线流程-{pmInfo.location}:tray==null!"); } else CMMLog.Debug($"包装下线流程-{pmInfo.location}:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}"); } } else { var task = MongoDBSingleton.Instance.FindOne(Query.Or(Query.EQ("CN_S_START_BIT", PlcBit02), Query.EQ("CN_S_END_BIT", PlcBit02)), "TN_I_TASK_MST"); if (task != null) CMMLog.Debug($"包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!任务号为:{task.CN_S_TASK_NO}"); else CMMLog.Debug("包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!"); } } else CMMLog.Debug($"包装下线流程-{pmInfo.location}:包装机起点或者复称起点终点有任务在执行中!包装机点位:{pmInfo.location},复称点位:{PlcBit02}"); } else CMMLog.Debug($"包装下线流程-{pmInfo.location}:包装机通道0里面的数据不为1!result为:{JsonConvert.SerializeObject(result)}"); } else CMMLog.Debug($"包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}"); } else CMMLog.Debug($"包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}"); } catch (Exception ex) { CMMLog.Debug($"包装下线流程-{pmInfo.location}:" +ex.Message); } } else { //var taskNo = MongoDBSingleton.Instance.FindOne(Query.Or(Query.EQ("CN_S_START_BIT", pmInfo.location), Query.EQ("CN_S_END_BIT", pmInfo.location)), "TN_I_TASK_MST"); //string task = "", startbit = "", endbit = ""; //if (taskNo != null) //{ // task = taskNo.CN_S_TASK_NO; // startbit = taskNo.CN_S_START_BIT; // endbit = taskNo.CN_S_END_BIT; //} CMMLog.Debug($"当前包装机起点终点有任务正在执行,请确认当前包装机的任务是否取货卸货完成!包装机货位编码:{pmInfo.location}"); } // } // else CMMLog.Info($"当前包装机出现故障,无法生成任务。包装机号:{pmInfo.location}"); } else CMMLog.Debug($"当前machineInfo中间表已存在数据:{JsonConvert.SerializeObject(count)}"); } #endregion #region 包装机MODBUS交互(开始取料,取料完成) /// 小车请求进料 1 AMS写,PLC读 1-请求进入;2-到位后,请求接驳滚动;3-取货完成;4-离开安全门外,清零通道 /// 设备允许出料 1 PLC写,AMS读 1-XXXXXXXX;2-允许进入;3-接驳滚动,送出物料 ;4-结束;5-安全门关闭 /// /// 小车到达包装机安全门口请求进入 /// /// internal static void PickUpFullDoorUnload(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); if(plc.device != "24") { //ASM写入通道0小车动作,1-取料 int[] num = new int[2] { 1, 1 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = ip, addr = plc.writeAddr, data = num,//原先是1,单个写入 port = plc.port }); DateTime dateTime = DateTime.Now; if (writeRes0.errCode == 0) { //小车请求进入安全门交互 //小车请求进料,并且查询设备是否允许AGV进入 //读地址1设备是否 2-可以进入 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = ip, addr = plc.readAddr + 1, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 2) { //设备允许进入,改参数通知AGV WorkFlowAction.TrackLog(taskNo, 10, 1012, "success"); TSHelper.GoToAGV(taskNo, 10, 1); } } else CMMLog.Debug($"包装下线:1012,readRes:{readRes},readRes.errCode:{readRes.errCode}"); } } else { var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 1 }); if (writeRes.errCode == 0) { //小车请求进入安全门交互 //小车请求进料,并且查询设备是否允许AGV进入 //读地址1设备是否 2-可以进入 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = ip, addr = plc.readAddr + 1, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 2) { //设备允许进入,改参数通知AGV WorkFlowAction.TrackLog(taskNo, 10, 1012, "success"); TSHelper.GoToAGV(taskNo, 10, 1); } } else CMMLog.Debug($"包装下线:1012,readRes:{readRes},readRes.errCode:{readRes.errCode}"); } } } /// /// 小车请求取货 /// /// /// internal static void PickUpFullUnload(string ip , string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); if (ip != "") { //小车请求出料,并且查询设备是否允许AGV进入 //读地址2设备是否 2-允许进料 保险起见,可以不读 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = ip, addr = plc.readAddr + 1, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 2) { //TSHelper.GoToAGV(taskNo, 10, 2);//原先先改AGV参数 var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = ip, addr = plc.writeAddr + 1, data = 2, port = plc.port }); if(writeRes1.errCode == 0) TSHelper.GoToAGV(taskNo, 10, 2); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2."); Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2."); } } else CMMLog.Debug($"包装下线,1112,readRes={readRes}"); } else CMMLog.Debug($"包装下线,1112,ip=null!"); } /// /// 小车取货完成--二期协议无此指令动作 /// /// /// internal static void PickUpFullComplete(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); if (ip != "") { //小车请求出料,并且查询设备是否允许AGV进入 //读地址2设备是否 2-允许进料 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = ip, addr = plc.readAddr+1, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); DateTime dateTime = DateTime.Now; if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 3) { var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = ip, addr = plc.writeAddr+1, data = 3, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为3."); Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为3."); } else if(readRes.result[0] == 4) { WorkFlowAction.TrackLog(taskNo, 10, 1212, "success"); TSHelper.GoToAGV(taskNo, 10, 5); } } else CMMLog.Debug($"包装下线,1212,readRes={readRes}!"); } else CMMLog.Debug($"包装下线,1212,ip=null!"); } /// /// 小车卸货完成离开安全门后通知包装机 /// /// /// internal static void PickUpFullDoorComplete(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); if (plc != null && !plc.location.Contains("FLZT")) { //写入包装机--安全门关门指令 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = ip, addr = plc.writeAddr + 1, data = 3, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为4."); Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为4."); } else CMMLog.Debug($"包装下线,4,ip=null!"); } #endregion #region 复称平台交互 ///小车请求进料 1 AMS写,PLC读 1-请求进入;2-小车进料,输送托盘;3-XXXXXXX ///设备允许出料 1 PLC写,AMS读 1-XXXXXXXX;2-允许进料,开始滚动;3-上料完成 ///复称平台小车动作: 1:上料复称;2:上砝码教称;3:取复称NG托盘,4:取砝码到包装机 /// ///复称平台安全请求进入——因为现场需求,所以额外增加一个类似安全门的交互,直接改参数就行 /// internal static void SecondWeightSafe(string ip,string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); //ASM写入小车动作,1:上料复称;2:上砝码教称;3:取复称NG托盘,4:取砝码到包装机 int[] num = new int[2] { 1, 1 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = ip, addr = plc.writeAddr, data = num,//原先是1,单个写入 port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}."); Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}."); ///小车和复称位对接 //小车请求进料,并且查询设备是否允许AGV进入 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = plc.ip, addr = plc.readAddr + 1, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); if (readRes != null && readRes.errCode == 0) { if(readRes.result[0] == 2) { WorkFlowAction.TrackLog(taskNo, 10, 1013, "success"); TSHelper.GoToAGV(taskNo, 10, 3); } } } /// /// 复称卸货 /// /// /// internal static void CheckUpReqUnload(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.location == ip).FirstOrDefault(); ///小车和复称位对接 if (plc != null) { //设备允许进入,改参数通知AGV WorkFlowAction.TrackLog(taskNo, 10, 1113, "success"); TSHelper.GoToAGV(taskNo, 10, 4); CMMLog.Debug($"改AGV参数:10,4"); //判断任务起点,找到起点包装机设备号,在中间表MachineInfo中查找上一台设备(包装机)的数据,通过modbus通道传入(数据需要转换,ASCII转16short,可能还需要数据拆分) var taskInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST"); if (plc.deviceType == "22") { //四钴车间 var machine = MongoDBSingleton.Instance.FindOne(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), "MachineInfoTetracobalt"); if (machine != null) { CMMLog.Debug("进入machine判断"); //向复称卸货复称,并将从包装机获取到的数据写入通道中 #region 多个数据写入MODBUS var arr = JsonConvert.DeserializeObject>(machine.jsonData); int[] num = new int[22]; for (int i = 0; i <= 21; i++) num[i] = arr[i]; // 14,15 替换为时间戳,后续处理 //num[14] = int.Parse(machine.overlappingLayers); //CMMLog.Debug($"machine.machinedown[14]:{num[14]}"); //num[15] = int.Parse(machine.bagNo); //CMMLog.Debug($"machine.machinedown[15]:{num[15]}"); //时间戳处理 //for (int b = 16; b <= 17; b++) //{ // int k = b % 2 == 0 ? 0 : 16; // num[b] = Convert.ToInt32(Convert.ToString(int.Parse(taskInfo.Ext2), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); //} CMMLog.Debug($"{JsonConvert.SerializeObject(num)}"); var wirteall = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { addr = plc.writeAddr + 5, host = plc.ip, data = num, port = plc.port }); var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr + 1, data = 2, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2."); Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2."); #endregion } else CMMLog.Debug($"machine判断错误,为null!"); } else { var machine = MongoDBSingleton.Instance.FindOne(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), "MachineInfo"); if (machine != null) { CMMLog.Debug("进入machine判断"); //向复称卸货复称,并将从包装机获取到的数据写入通道中 #region 多个数据写入MODBUS var arr = JsonConvert.DeserializeObject>(machine.jsonData); int[] num = new int[18]; for (int i = 0; i <= 15; i++) num[i] = arr[i]; // 14,15 替换为时间戳,后续处理 num[14] = int.Parse(machine.overlappingLayers); CMMLog.Debug($"machine.machinedown[14]:{num[14]}"); num[15] = int.Parse(machine.bagNo); CMMLog.Debug($"machine.machinedown[15]:{num[15]}"); //时间戳处理 for (int b = 16; b <= 17; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(taskInfo.Ext2), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } CMMLog.Debug($"{num}"); Console.WriteLine(num); var wirteall = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { addr = plc.writeAddr + 10, host = plc.ip, data = num, port = plc.port }); var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr + 1, data = 2, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2."); Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为2."); #endregion } else CMMLog.Debug($"machine判断错误,为null!"); } } else CMMLog.Debug($"包装下线,1113,ip=null!"); } /// /// 复称卸货完成 /// /// /// /// internal static void CheckUpUnloadComplete(string ip, string cN_S_TASK_NO, int port) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip && a.port == port).FirstOrDefault(); int[] num = new int[1] { 2 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = ip, addr = plc.writeAddr + 1, data = num,//原先是1,单个写入 port = plc.port }); var tray = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", cN_S_TASK_NO), "TN_I_TASK_MST"); var query = Query.And(Query.EQ("actionNo", "1"), Query.EQ("ext1", ip), Query.EQ("ext2", tray.CN_S_BATCH_NO)); var ActionControlInfo = MongoDBSingleton.Instance.FindOne(query, "ActionControlModel"); if (ActionControlInfo == null) { MongoDBSingleton.Instance.Insert(new ActionControlModel { actionNo = "1", ext1 = ip, ext2 = tray.CN_S_BATCH_NO, ext3 = tray.CN_S_START_BIT, ext4 = port, machince = tray.CN_S_START_BIT }, "ActionControlModel"); string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location; MongoDBSingleton.Instance.Update(Query.EQ("location", PlcBit02), Update.Set("full",1), "SecondWeighState", UpdateFlags.None); } } /// /// 复称平台安全退出(已退出)--二期无需执行此指令动作 /// /// /// /// internal static bool SecondWeightSafeComplete(string ip, string cN_S_TASK_NO) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); bool result = true; if(ip != "") { //var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // addr = plc.writeAddr, // data = 0, // host = ip, // port = plc.port //}); //CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为0."); //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr}里面数据为0."); //var wirte1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // addr = plc.writeAddr + 1, // data = 0, // host = ip, // port = plc.port //}); //CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为0."); //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为0."); result = true; } else { CMMLog.Debug($"包装下线,4,ip=null!"); } return result; } internal static void SecondWeightActionOne(ActionControlModel model) { var plc = Settings.GetPlcInfo().Where(a => a.ip == model.ext1 && a.port == model.ext4).FirstOrDefault(); if (plc != null) { bool req = false; var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 28, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); if (result.result.Length > 0) { if(plc.deviceType == "22") { var newWeight = result.result[19] + "," + result.result[20]; MongoDBSingleton.Instance.Update(Query.EQ("trayCode", model.ext2), Update.Set("secondNg", result.result[2]), UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("trayCode", model.ext2), Update.Set("secondNg", result.result[2]).Set("oneTrayWeightFC",newWeight), UpdateFlags.None); } else { MongoDBSingleton.Instance.Update(Query.EQ("trayCode", model.ext2), Update.Set("secondNg", result.result[2]), UpdateFlags.None); } //获取从复称平台读出的 时间戳,并转换处理为 string 字符串 //string timeStamp = Convert.ToInt32(Completion(result.result[26]) + Completion(result.result[27]), 2).ToString(); string timeStamp = Convert.ToInt32(Completion(result.result[25]) + Completion(result.result[26]), 2).ToString(); string wmstaskno = ""; string traycode = ""; CMMLog.Info($"SecondWeightActionOne:readData:{JsonConvert.SerializeObject(result.result)},timeStamp:{timeStamp}"); if (result.result[0] == 3) { int[] num = new int[1] { 0 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = plc.ip, addr = plc.writeAddr + 1, data = num,//原先是1,单个写入 port = plc.port }); } if (result.result[0] == 3 && result.result[2] == 1) //if (result.result[2] == 1) { CMMLog.Info($"SecondWeightActionOne:准备生成复称入缓存架任务,开始确定托盘号:{model.ext2}"); if(plc.deviceType == "22") { req = WMSHelper.WMSInTwo(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp); } else { req = WMSHelper.WMSIn(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp); } ERPService.updatePackageInfo(model.machince, model.ext2, result.result); //if (req) TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6); //else CMMLog.Debug($"WMS返回{req}"); //var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // addr = plc.writeAddr + 3, // host = plc.ip, // port = plc.port, // data = 1 //}); } if (result.result[2] == 2) { //复称平台复称货物NG——进入NG复称流程 调用WMS接口分配货架库位 //TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6); if(plc.deviceType == "22") { req = WMSHelper.WMSInTwo(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp); } else { req = WMSHelper.WMSIn(plc.location, model.ext2, ref wmstaskno, ref traycode, timeStamp); } if (req) CMMLog.Info($"SecondWeightActionOne:复称入缓存架NG流程:WMS生成任务成功!"); else CMMLog.Info($"SecondWeightActionOne:复称入缓存架NG流程:WMS生成任务失败!"); var plcInfo = Settings.GetPlcInfo().Where(a => a.location == model.ext3).FirstOrDefault(); if (plcInfo != null) { CMMLog.Info($"SecondWeightActionOne:复称入缓存架NG流程:写入包装机NG通道数据1!"); var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plcInfo.writeAddr + 29, host = plcInfo.ip, port = plcInfo.port, data = 1 }); } } string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location; UpdateBuilder update = Update.Set("ng", result.result[2]); MongoDBSingleton.Instance.Update(Query.EQ("location", PlcBit02), update, "SecondWeighState", UpdateFlags.None); if (req) MongoDBSingleton.Instance.Remove(Query.EQ("_id", model._id), "ActionControlModel", RemoveFlags.Single); } } else CMMLog.Debug($"包装下线,1213,ip=null!"); } #endregion #endregion #region 3楼复称平台入3楼双层缓存架流程——需回报WMS任务状态——3楼复称入缓存架——已完成(待测试) /// /// 判断复称通道3里面的值是否为1,并且复称平台上有货,调WMS生成入缓存架任务 /// /// internal static void SecondWeightInCache(Settings.PlcInfo pmInfo) { //检查复称平台通道3是否值为1,并且复称平台上有货 if(ProcessHelper.CheckStartFree(GetSecondWeighBit()) && ProcessHelper.CheckEndFree(GetSecondWeighBit())) { try { var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = pmInfo.readAddr + 3, host = pmInfo.ip, port = pmInfo.port }); if (result != null && result.errCode == 0) { //复称平台 重新复核称重后,发送结果给AMS 1:OK,2:NG,当检测到通道2或者4的值为 if (result.result[0] == 1) { //判断一下当前复称点有没有任务占用中 if (ProcessHelper.CheckStartFree(GetSecondWeighBit()) && ProcessHelper.CheckEndFree(GetSecondWeighBit())) { //判断复称位置,产品NG合格值为1并且复称上有货 //var state = GetSecondWeighState(pmInfo.location); //if (state.full == 1 && state.ng == 1) //{ //#region 获取托盘码 //CMMLog.Info("复称入缓存:获取托盘,fc01"); //string traycode = GetMachineTrayCode(); //#endregion //string wmstaskno = ""; //bool req = WMSHelper.WMSIn(pmInfo.location, traycode, ref wmstaskno, ref traycode); //if (req) //{ // CMMLog.Info($"复称入缓存架:WMS生成任务成功!"); //} //else //{ // CMMLog.Info($"复称入缓存架:WMS生成任务失败!"); //} //} } } } } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } } #region 复称交互 public static int SecondRelax = 0; /// /// 请求进入复称取货 /// /// /// internal static void SecondWeightInCache1012(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if(ip != null) { ///小车和复称位对接 if (ip != "") { //小车请求进料,并且查询设备是否允许AGV进入 WorkFlowAction.TrackLog(taskNo, 10, 1012, "success"); TSHelper.GoToAGV(taskNo, 10, 1); SecondRelax = 0; } else CMMLog.Info($"复称入缓存架,1012,ip=null!"); } } /// /// 取货完成,离开复称,复位通道,并读取设备通道数据 /// /// /// internal static void SecondWeightInCache4(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); var trayCodeTask = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST"); if(trayCodeTask != null) { string trayCodeNo = trayCodeTask.CN_S_BATCH_NO; if(trayCodeNo != "") { var status = MongoDBSingleton.Instance.FindOne(Query.EQ("location", GetSecondWeighBit()), "SecondWeighState"); //UpdateBuilder update = Update.Set("full", 0); //if (status.ng != 2) //{ // update = Update.Set("ng", 0).Set("full", 0); //} UpdateBuilder update = Update.Set("ng", 0).Set("full", 0); CMMLog.Debug($"开始更改复称状态中间表状态:full:0"); CMMLog.Debug($"三楼双层缓存架卸货交互:trayCode:{trayCodeTask.CN_S_BATCH_NO}"); if (plc.deviceType =="22") { MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfoTetracobalt", RemoveFlags.Single); if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfoTetracobalt", RemoveFlags.Single); } else { MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfo", RemoveFlags.Single); if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", trayCodeTask.CN_S_BATCH_NO), "MachineInfoTwo", RemoveFlags.Single); } //MongoDBSingleton.Instance.Update(Query.EQ("CN_S_TASK_NO", taskNo), Update.Set("CN_S_BATCH_NO", trayCodeNo), UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("location", GetSecondWeighBit()), update, "SecondWeighState", UpdateFlags.None); SecondRelax = 1; } } if (plc != null) { var writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr, data = 0, port = plc.port }); writeRes1 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr + 3, data = 1, port = plc.port }); } } #endregion #region 三楼双层缓存架交互 /// /// 请求进入三楼双层缓存架 /// /// /// internal static void SecondWeightInCache1013(string taskNo) { TSHelper.GoToAGV(taskNo,10,3); } /// /// 离开三楼双层缓存架回报信息 /// /// /// internal static bool SecondWeightInCache6(string ip, string cN_S_TASK_NO) { //CMMLog.Info($"三楼双层缓存架卸货交互:trayCode:{trayCode}"); //var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); //if (plc != null) //{ // var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel // { // dataNum = 28, // addr = plc.readAddr + 1, // host = plc.ip, // port = plc.port // }); // var tray = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", cN_S_TASK_NO), "TN_I_TASK_MST"); // MongoDBSingleton.Instance.Update(Query.EQ("trayCode", tray.CN_S_BATCH_NO), Update.Set("secondNg", result.result[2]), UpdateFlags.None); // //获取从复称平台读出的 时间戳,并转换处理为 string 字符串 // string timeStamp = Convert.ToInt32(Completion(result.result[26]) + Completion(result.result[27]), 2).ToString(); // string wmstaskno = ""; // string traycode = ""; // if (result.result[0] == 3 && result.result[2] == 1) // { // CMMLog.Debug($"准备生成复称入缓存架任务,开始确定托盘号:{tray.CN_S_BATCH_NO}"); // bool req = WMSHelper.WMSIn(plc.location, tray.CN_S_BATCH_NO, ref wmstaskno, ref traycode, timeStamp); // if (req) TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6); // else CMMLog.Debug($"WMS返回{req}"); // } // if (result.result[2] == 2) // { // //复称平台复称货物NG——进入NG复称流程 调用WMS接口分配货架库位 // TSHelper.GoToAGV(cN_S_TASK_NO, 10, 6); // bool req = WMSHelper.WMSIn(plc.location, tray.CN_S_BATCH_NO, ref wmstaskno, ref traycode, timeStamp); // if (req) CMMLog.Debug($"复称入缓存架NG流程:WMS生成任务成功!"); // else CMMLog.Debug($"复称入缓存架NG流程:WMS生成任务失败!"); // } // string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location; // UpdateBuilder update = Update.Set("ng", result.result[2]).Set("full", 1); // MongoDBSingleton.Instance.Update(Query.EQ("location", PlcBit02), update, UpdateFlags.None); //} //else CMMLog.Debug($"包装下线,1213,ip=null!"); return true; } #endregion #endregion internal static void writeStackingMouth6(string loca, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.location == loca && a.enable == 1).FirstOrDefault(); CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询设备信息:{JsonConvert.SerializeObject(plc)}。"); if (plc != null) { var task = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST"); CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询任务信息:{JsonConvert.SerializeObject(task)}。"); var machine = MongoDBSingleton.Instance.FindOne(Query.EQ("trayCode", task.CN_S_BATCH_NO), "WMSInfo"); CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询WMSInfo表信息:{JsonConvert.SerializeObject(machine)}。"); if (machine != null) { //int[] num = DiePan(machine); //CMMLog.Info($"send num:{JsonConvert.SerializeObject(num)}"); //var wirteall01 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti //{ // addr = plc.writeAddr + 10, // host = plc.ip, // port = plc.port, // data = num //}); WriteCacheStackingData(plc, task, machine); var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, port = plc.port, data = 2 }); } } } #region 3楼双层缓存货架到三楼叠托口——需回报WMS任务状态——3楼缓存架入叠托——半完成(待测试) /// /// 判断三楼叠托口是否有允许上料请求(通道1参数1),有就调WMS生成缓存架到叠托口任务 /// /// internal static void CacheStackingMouth(Settings.PlcInfo pmInfo) { //需要设置时间延迟 try { //读取通道1里面参数是否为1,判断叠盘机是否需要上料 var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 2, addr = pmInfo.readAddr + 1, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Info($"获取碟盘入口信息:{JsonConvert.SerializeObject(result)}"); if (result != null && result.errCode == 0) { //参数1表示叠托点申请入料 if (result.result[0] == 1) { var tasks = MongoDBSingleton.Instance.Find(Query.EQ("CN_S_END_BIT", pmInfo.location), "TN_I_TASK_MST"); //判断一下当前叠托点是否有任务占用 if (ProcessHelper.CheckEndFree(pmInfo.location) && tasks.Count == 0) { //可以生成任务,调WMS接口获取任务信息 bool req = WMSHelper.WMSOut(pmInfo.location, ""); if (req) CMMLog.Debug($"调用WMS获取碟盘出库生成任务成功!");//现在任务由WMS自己下发,AMS做拦截处理(查询ext1里面对应的任务类型,并更改任务类型) else CMMLog.Debug($"调用WMS获取碟盘出库生成任务失败!"); } else CMMLog.Debug($"检查当前叠托点是否有任务占用,或者MST主表中有缓存架入叠盘机的任务!"); } if (pmInfo.deviceType == "23" && result.result[1] == 1) { //四钴车间,人工上料模式 result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 21, addr = pmInfo.readAddr + 120, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Info($"获取碟盘人工上料写入信息:{JsonConvert.SerializeObject(result)}"); if (result != null && result.errCode == 0) { string lotNo = RemoveNull(GetTrayCode(result.result.Skip(0).Take(20).ToArray()).Trim().ToString()); //批次号 string bagNumber = Convert.ToInt32(Completion(result.result[20]), 2).ToString(); var machinInfo = MongoDBSingleton.Instance.FindOne(Query.And(Query.EQ("lotNo", lotNo), Query.EQ("packageCode", bagNumber)), "MachineInfoTwoTetracobalt"); if(machinInfo != null) { TN_I_TASK_MST task = new TN_I_TASK_MST { CN_S_BATCH_NO = machinInfo.trayCode }; WMSInfo machine = new WMSInfo { location = machinInfo.location, productWeight = machinInfo.productWeight, oneTrayWeight = machinInfo.oneTrayWeight, addState = machinInfo.addState, packageCode = machinInfo.packageCode, itemCode = machinInfo.materialCode }; PLCControl.WriteCacheStackingData(pmInfo, task,machine); machinInfo = MongoDBSingleton.Instance.FindOne(Query.And(Query.EQ("lotNo", lotNo), Query.EQ("packageCode", bagNumber)), "MachineInfoTwoTetracobalt"); if(machinInfo == null) { //信息写入成功 var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.writeAddr + 2, host = pmInfo.ip, port = pmInfo.port, data = 1 }); CMMLog.Info("人工叠包信息查询成功,写入1"); } } else { var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.writeAddr + 2, host = pmInfo.ip, port = pmInfo.port, data = 2 }); CMMLog.Info("人工叠包信息查询失败,写入2"); } } } } else CMMLog.Debug($"缓存入叠盘机,创建任务,result={result.errMsg}"); } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } #region 三楼叠托机交互 /// /// 请求进入三楼叠托机 /// /// /// internal static void CacheStackingMouth1013(string ip,string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if(plc != null) { var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, port = plc.port, data = 2 }); var readres = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); if (readres.result[0] == 0 || readres.result[0] == 1) { wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, port = plc.port, data = 1 }); } else if (readres.result[0] == 2) { TSHelper.GoToAGV(taskNo, 10, 3); } } else CMMLog.Debug($"缓存入叠盘机,1013,plc=null!"); } public static void WriteCacheStackingData(Settings.PlcInfo plc, TN_I_TASK_MST task, WMSInfo machine) { if (plc.deviceType == "23") { //四钴车间 var machineTwo = MongoDBSingleton.Instance.FindOne(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwoTetracobalt"); CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询MachineInfoTwoTetracobalt表信息:{JsonConvert.SerializeObject(machineTwo)}。"); if (machineTwo != null) { CMMLog.Info($"3楼缓存架入叠托:获取数据:{JsonConvert.SerializeObject(machineTwo)},IPort:{plc.ip},{plc.port}"); int[] num = DiePanTwoTetracobalt(machine, machineTwo); CMMLog.Info($"3楼缓存架入叠托:写入数据:{JsonConvert.SerializeObject(num)},IPort:{plc.ip},{plc.port}"); var wirteall01 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { addr = plc.writeAddr + 5, host = plc.ip, port = plc.port, data = num }); CMMLog.Info($"3楼缓存架入叠托:返回数据:{JsonConvert.SerializeObject(wirteall01)},IPort:{plc.ip},{plc.port}"); if (wirteall01.errCode == 0) { MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwoTetracobalt", RemoveFlags.Single); } } } else { var machineTwo = MongoDBSingleton.Instance.FindOne(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwo"); CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询MachineInfoTwo表信息:{JsonConvert.SerializeObject(machineTwo)}。"); if (machineTwo != null) { CMMLog.Info($"3楼缓存架入叠托:获取数据:{JsonConvert.SerializeObject(machineTwo)},IPort:{plc.ip},{plc.port}"); int[] num = DiePanTwo(machine, machineTwo); CMMLog.Info($"3楼缓存架入叠托:写入数据:{JsonConvert.SerializeObject(num)},IPort:{plc.ip},{plc.port}"); var wirteall01 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { addr = plc.writeAddr + 10, host = plc.ip, port = plc.port, data = num }); CMMLog.Info($"3楼缓存架入叠托:返回数据:{JsonConvert.SerializeObject(wirteall01)},IPort:{plc.ip},{plc.port}"); if (wirteall01.errCode == 0) { MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", task.CN_S_BATCH_NO), "MachineInfoTwo", RemoveFlags.Single); } } } } private static int[] DiePanTwoTetracobalt(WMSInfo machine, MachineInfoTwoTetracobalt machineInfoTwo) { var ErpItemInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("item_code", machineInfoTwo.materialCode), "ERPItemTable"); CMMLog.Info($"3楼缓存架入叠托:收到信号4,查询ERPItemTable表信息:{JsonConvert.SerializeObject(ErpItemInfo)}。"); //偏移量 +11 通道范围: 40311 ~ 40390 CMMLog.Info($"叠盘机数据处理."); //10~19通道 int[] num = new int[105];//总长度:105 #region 0~23 => 6~29 int a = 0;//员工编号处理 for (int b = 0; b <= 4; b++) { num[b] = int.Parse(AsciiToTen(machineInfoTwo.empCode.Substring(a, 2))); } a = 0;//托盘码处理 for (int b = 5; b <= 7; b++) { //num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2))); //a = a + 2; num[b] = 0; } num[8] = int.Parse(AsciiToTen(machine.location));//包装机号 for (int b = 9; b <= 10; b++) { int k = b % 2 == 0 ? 16 : 0; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 11; b <= 12; b++) { int k = b % 2 == 0 ? 16 : 0; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machineInfoTwo.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 13; b <= 14; b++) { int k = b % 2 == 0 ? 16 : 0; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 15; b <= 16; b++) { var arrTwo = machineInfoTwo.oneTrayWeightFC.Split(',').ToList(); num[b] = int.Parse(arrTwo[b - 15]);//复称重量 } //for (int b = 15; b <= 16; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0 num[17] = int.Parse(machineInfoTwo.trayType); num[18] = machine.addState;//是否需要叠包 num[19] = int.Parse(machine.packageCode);//袋号 for (int b = 20; b <= 21; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machineInfoTwo.productTime), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } //时间戳处理 num[22] = 0; num[23] = 0; #endregion string pcHead = ""; string pcTail = ""; string cpHead = ""; string cpTail = ""; string mtHead = "";// 设备的真实物料编码 #region 35~54 => 41~60 产品批次号 string data = machineInfoTwo.lotNo; CMMLog.Info($"41~60data1_1:{data}"); if (data.Length % 2 != 0) { //data = "0" + machineInfoTwo.lotNo; //pcHead = "" + machineInfoTwo.lotNo.Substring(0, 1); pcTail = machineInfoTwo.lotNo.Substring(data.Length - 1, 1) + ""; CMMLog.Info($"pcTail:{pcTail}"); data = machineInfoTwo.lotNo + " "; } CMMLog.Info($"41~60data1_2:{data}"); //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道 int maxLength = 34 + (data.Length / 2);// data.Length / 2 至少为 1 int maxLength2 = maxLength; int aa = 0; for (int i = 35; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2))); aa = aa + 2; } for (int x = maxLength + 1; x <= 54; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0 CMMLog.Info($"num:{JsonConvert.SerializeObject(num)}"); #endregion #region 55~64 => 61~70 产品型号(四钴产品型号有中文和英文) int maxLength3 = 0; if (Regex.IsMatch(ErpItemInfo.item_spec, @"[\u4e00-\u9fff]")) { ERPService.HandleItemInfoChina(ErpItemInfo.item_spec, 55, 64, num);// 中文处理 55~64 61~70 } else { data = ErpItemInfo.item_spec; CMMLog.Info($"61~70data1_1:{data}"); if (data.Length % 2 != 0) { //data = "0" + machineInfoTwo.lotNo; //pcHead = "" + machineInfoTwo.lotNo.Substring(0, 1); cpTail = ErpItemInfo.item_spec.Substring(data.Length - 1, 1) + ""; CMMLog.Info($"cpTail:{cpTail}"); data = ErpItemInfo.item_spec + " "; } CMMLog.Info($"61~70data1_2:{data}"); //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道 maxLength = 54 + (data.Length / 2);// data.Length / 2 至少为 1 maxLength3 = maxLength; aa = 0; for (int i = 55; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2))); aa = aa + 2; } for (int x = maxLength3 + 1; x <= 64; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0 CMMLog.Info($"num:{JsonConvert.SerializeObject(num)}"); //ERPService.HandleItemInfo(ErpItemInfo.item_spec, 55, 64, num); } //string data2 = machine.itemCode; //CMMLog.Info($"51~50data2_1:{data2}"); //if (data2.Length % 2 != 0) //{ // data2 = "0" + machine.itemCode; // cpHead = "" + machine.itemCode.Substring(0, 1); //} //CMMLog.Info($"51~60data2_2:{data2}"); //maxLength = 39 + (data2.Length / 2); //int aaa = 0; //for (int i = 40; i <= maxLength; i++) //{ // num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2))); // aaa = aaa + 2; //} //for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion #region 65~79 => 71~85 物料编码 string data3 = machineInfoTwo.materialCode; CMMLog.Info($"71~85 data3_1:{data3}"); if (data3.Length % 2 != 0) { //data3 = "0" + machineInfoTwo.materialCode; mtHead = "" + machineInfoTwo.materialCode.Substring(0, 1); } CMMLog.Info($"71~85 data3_2:{data3}"); maxLength = 64 + (data3.Length / 2); int aaaa = 0; for (int i = 65; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data3.Substring(aaaa, 2))); aaaa = aaaa + 2; } for (int x = maxLength + 1; x <= 79; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion ERPService.HandleItemInfoChina(ErpItemInfo.item_name, 80, 99, num);// 中文处理 80~99 86~105 ERPService.HandleItemInfo(ErpItemInfo.item_uom, 100, 104, num);// 100~104 106~110 // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道 //if (pcHead != "") num[35] = int.Parse(AsciiToTen(pcHead)); if (pcTail != "") num[maxLength2] = tailToTen(pcTail); //if (cpHead != "") num[55] = int.Parse(AsciiToTen(cpHead)); if (cpHead != "") num[maxLength3] = int.Parse(AsciiToTen(cpTail)); if (mtHead != "") num[65] = int.Parse(AsciiToTen(mtHead)); if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[35]},{num[55]}"); CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}"); return num; } private static int tailToTen(string Tail) { char inputChar = Tail[0]; // 获取字符串的第一个字符 int asciiValue = (int)inputChar; // 转换为ASCII码 string hexValue = asciiValue.ToString("X2"); // 转换为16进制 string binaryValue = Convert.ToString(asciiValue, 2).PadLeft(8, '0'); // 转换为8位二进制 string finalBinaryValue = binaryValue + "00000000"; // 补8个二进制0 int finalDecimalValue = Convert.ToInt32(finalBinaryValue, 2); // 转换为10进制 return finalDecimalValue; } /// /// 三楼叠托机扫码 /// /// /// internal static void CacheStackingMouth1313(string ip,string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if(plc != null) { if(plc.deviceType == "23") { var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, data = 3, port = plc.port }); if (wirte.errCode == 0) { var taskInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST"); MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), RemoveFlags.Single); wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, data = 0, port = plc.port }); TSHelper.GoToAGV(taskNo, 10, 7); } } else { OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, data = 2, port = plc.port }); var readres = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 2, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); if (readres != null) { var taskInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST"); if (readres.result[0] == 3 && readres.result[1] == 1) { CMMLog.Debug($"3楼缓存架入叠托OK:TaskNo:{taskInfo.CN_S_TASK_NO},叠包OK写入值3。"); //读码信息比对结果:OK 读到通道2参数为1时,小车将托盘放置在叠盘机上(改参数10为7) var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, data = 3, port = plc.port }); if (wirte.errCode == 0) { //并删除WMSInfo中间表中对应托盘号的数据(也可在小车离开叠盘机之后删除,暂定通道2参数1) CMMLog.Debug($"读码信息比对结果:OK ,并删除WMSInfo中间表中对应托盘号的数据"); MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), RemoveFlags.Single); CMMLog.Debug($"3楼缓存架入叠托OK:TaskNo:{taskInfo.CN_S_TASK_NO},叠包OK写入值4。"); if (CacheStackingMouth8(plc)) { CMMLog.Debug($"3楼缓存架入叠托OK:TaskNo:{taskInfo.CN_S_TASK_NO},叠包OK写入值成功,更改AGV参数10为7,让小车离开。"); TSHelper.GoToAGV(taskNo, 10, 7); } } } else if (readres.result[0] == 3 && readres.result[1] == 2) { CMMLog.Debug($"读码信息比对结果:条码与传送的数据不一致"); //二期--调用 WMS 改道接口 获取 目标点位,并 更改 AGV 站点 string ChangeBit = WMSHelper.WmsUpdateWay(taskInfo.CN_S_SOURCE_NO, plc.Extend); if (!string.IsNullOrEmpty(ChangeBit)) { int[] parms = { StockInstance.Instance.GetAGVCodeForBitCode(taskInfo.CN_S_END_BIT), StockInstance.Instance.GetAGVCodeForBitCode(ChangeBit) }; TSHelper.ChangeParam(taskNo, 1, parms); TSHelper.GoToAGV(taskNo, 3, 1);// 泰州格林美更改起点层数为1-荆门也需要更改 //TSHelper.ChangeParam(taskNo, 1, StockInstance.Instance.GetAGVCodeForBitCode(taskInfo.CN_S_END_BIT)); //TSHelper.ChangeParam(taskNo, 2, StockInstance.Instance.GetAGVCodeForBitCode(ChangeBit)); TSHelper.GoToAGV(taskNo, 10, 8); MongoDBSingleton.Instance.Update(Query.EQ("CN_S_TASK_NO", taskNo), Update.Set("CN_S_END_BIT", ChangeBit).Set("CN_S_START_BIT", taskInfo.CN_S_END_BIT), "TN_I_TASK_MST", UpdateFlags.None); } else CMMLog.Debug($"调用WMS改道接口,未获取到可用点位。"); Console.WriteLine($"读码信息比对结果:条码与传送的数据不一致"); } //PLCControl.CacheStackingMouth6(plc); } } } } /// /// 叠托OK写入4 /// /// /// internal static void CacheStackingMouth6(Settings.PlcInfo plc, string taskNo) { var taskInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_TASK_NO", taskNo), "TN_I_TASK_MST"); MongoDBSingleton.Instance.Remove(Query.EQ("trayCode", taskInfo.CN_S_BATCH_NO), RemoveFlags.Single); var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, data = 3, host = plc.ip, port = plc.port }); Thread.Sleep(2000); wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, data = 0, host = plc.ip, port = plc.port }); } /// /// 卸货离开三楼叠托机--二期:需要和TS对接,让小车 卸货完成等到 小车离开之后再发送 /// /// /// internal static bool CacheStackingMouth8(Settings.PlcInfo plc) { bool result = false; var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, data = 4, host = plc.ip, port = plc.port }); if (wirte.errCode == 0) result = true; return result; } #endregion #endregion #region 3楼包装机空托补给3楼包装机流程——无需回报WMS任务状态——3楼包装补空——已完成(待测试) /// /// 3楼包装机补空托流程——任务开始前,判断包装机有没有下线信号,并判断当前包装机对应的地堆位是否有任务占用 /// /// internal static void PickUpBlank(Settings.PlcInfo pmInfo) { /* 包装补空流程: * 读取 包装机 是否需要补空托,如果有请求,则继续判断当前包装机无任务占用,以及当前包装机所对应的 地堆位 无任务占用 * 任务创建方式:调用WMS,传输 终点(当前包装机所对应的 地堆位) * **/ try { //读取输入寄存器还是输出寄存器待定,读取通道0的数据 var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = pmInfo.readAddr, host = pmInfo.ip, port = pmInfo.port }); if (result != null && result.errCode == 0) { //1:取料;3:人工叫空托盘 if (result.result[0] == 3) { CMMLog.Info("包装机补空托流程:判断包装机通道0值为3"); //空托库区 -> 包装机对应地堆空托缓存位 if (ProcessHelper.PickUpEndFree(pmInfo.Extend)) { bool req = WMSHelper.WMSOut(pmInfo.Extend, ""); if (req) { //任务完成之后,需要复位此通道数据为 0 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = pmInfo.ip, addr = pmInfo.writeAddr + 2, data = 1, port = pmInfo.port }); } } } } } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } /// /// 包装补空任务完成--复位 收到上空信号 为 0 /// /// internal static void PickUpBlankComplete(string PlcBitCache01) { string PlcBit01 = PlcBitCache01; var plcInfo = Settings.GetPlcInfo().Where(a => a.location == PlcBit01).FirstOrDefault(); var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plcInfo.ip, addr = plcInfo.writeAddr + 2, data = 0, port = plcInfo.port }); } #endregion #region 3楼补给成品空托区到3楼拆盘机流程——需回报WMS任务状态——3楼拆盘补空——已完成(待测试) /// /// 线程循环查询拆盘机是否需要补空托 /// /// internal static void CheckPackingLineEmpty(Settings.PlcInfo plc) { //读plc信号,看有没有叫料请求,判断是否已经有任务 //没有任务,调WMS生成空托补充任务 try { //RegisterReadInPut 读取输入寄存器还是输出寄存器待定,读取通道1的数据 var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr+1, host = plc.ip, port = plc.port }); if (result != null && result.errCode == 0) { //1.补空托盘 if (result.result[0] == 1) { //判断3楼拆盘机(补空托盘点位)终点是否有任务占用,没有则生成任务 if (ProcessHelper.CheckEndFree(plc.location)) { CMMLog.Debug("拆盘补空流程调用。。。"); string wmstaskno = ""; string traycode = ""; string trayType = ""; if (result.result[1] == 1) trayType = "田字托"; else trayType = "川字托"; bool req = WMSHelper.WMSEmptyOut(plc.location, "", ref wmstaskno, ref traycode, trayType); if (req) CMMLog.Debug($"调用WMS获取三楼拆盘机生成任务成功!"); else CMMLog.Debug($"调用WMS获取三楼拆盘机生成任务失败!"); } } } } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } #region 拆盘机交互 /// 小车请求出料 1 AMS写,PLC读 1-请求出料;2-出料进行中;3-动作完成 /// 设备允许进料 1 PLC写,AMS读 1-允许进料;2-进料进行中;3-动作完成 /// /// 向拆盘机请求卸货 /// /// internal static void DiscRemoverReqUnload(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if (plc != null) { var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = plc.ip, port = plc.port, addr = plc.readAddr + 1 }); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 0 || readRes.result[0] == 1) { //不允许进料,ams写入请求出料 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 1 }); } else if (readRes.result[0] == 2) { //设备允许进料 //改参数通知agv if (TSHelper.GoToAGV(taskNo, 10, 3)) { WorkFlowAction.TrackLog(taskNo, 10, 1013, "success"); //改成出料进行中 CMMLog.Debug("三楼拆盘机:小车正在卸空盘中!"); var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 2 }); } else CMMLog.Debug($"三楼拆盘补空,1013,TS,10,3,false!"); } } } } /// /// 小车卸货完成离开通知拆盘机 /// /// /// internal static bool DiscRemoverUnloadComplete(string ip, string taskNo) { var result = false; var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if (plc != null) { //1.0 读地址1设备是否动作完成 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = plc.ip, port = plc.port, addr = plc.readAddr + 1 }); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 2) { var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 3 }); result = true; CMMLog.Debug("读取设备【参数不为0】,AMS设置小车【动作完成】,等待设备【参数为0】时再改参数通知agv"); } } } return result; } #endregion #endregion #region 3楼叠盘机满托下线入库流程——需回报WMS任务状态——3楼叠盘下线--已完成(待测试) /// /// 线程循环查询叠盘机是否需要下满托 /// /// internal static void StackingLineEmpty(Settings.PlcInfo plc) { //读plc信号,看有没有叫料请求,判断是否已经有任务 //没有任务,调WMS生成空托补充任务 try { //RegisterReadInPut 读取输入寄存器还是输出寄存器待定,读取通道1的数据 var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 2, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); CMMLog.Info($"叠盘下线读取通道{plc.ip},port:{plc.port},地址:{plc.readAddr + 1}的值为{JsonConvert.SerializeObject(result)}"); if (result != null && result.errCode == 0) { //1.满托下线 if (result.result[0] == 1) { //判断3楼叠盘起点是否有任务占用,没有则生成任务 if (ProcessHelper.CheckStartFree(plc.location)) { string wmstaskno = ""; string traycode = ""; string trayType = ""; if (plc.deviceType == "25") { trayType = result.result[1] == 1 ? "川字托" : result.result[1] == 2 ? "田字托" : ""; if (string.IsNullOrEmpty(trayType)) { return; } } bool req = WMSHelper.WMSIn(plc.location, "", ref wmstaskno, ref traycode,"","",false, trayType); if (req) { CMMLog.Debug($"调用WMS获取三楼叠盘机生成任务成功!"); if (plc.deviceType == "25") { //记录托盘类型 var trayTypeInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("locCode", plc.location), "trayTypeTable"); if (trayTypeInfo == null) { MongoDBSingleton.Instance.Insert(new trayTypeTable { locCode = plc.location, trayType = result.result[1].ToString() }); } } } else CMMLog.Debug($"调用WMS获取三楼叠盘机生成任务失败!"); } } } } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } #region 叠盘机交互 /// 小车请求出料 1 AMS写,PLC读 1-请求出料;2-出料进行中;3-动作完成 /// 设备允许进料 1 PLC写,AMS读 1-允许进料;2-进料进行中;3-动作完成 /// /// 向叠盘机请求取货 /// /// internal static void StackingReqUnload(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if (plc != null) { var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, host = plc.ip, port = plc.port, data = 1 }); var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = plc.ip, port = plc.port, addr = plc.readAddr + 1 }); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 0 || readRes.result[0] == 1) { //不允许进料,ams写入请求出料 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 1 }); } else if (readRes.result[0] == 2) { //设备允许进料 //改参数通知agv if (TSHelper.GoToAGV(taskNo, 10, 1)) { WorkFlowAction.TrackLog(taskNo, 10, 1013, "success"); //改成出料进行中 CMMLog.Debug("三楼叠盘机:小车正在取托盘中!"); var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 2 }); } else CMMLog.Debug($"三楼叠盘机,1013,TS,10,3,false!"); } } } #region //var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); //if (plc != null) //{ // var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel // { // dataNum = 1, // host = plc.ip, // port = plc.port, // addr = plc.readAddr + 1 // }); // if (readRes != null && readRes.errCode == 0) // { // if (readRes.result[0] == 0 || readRes.result[0] == 1) // { // //不允许进料,ams写入请求出料 // var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel // { // host = plc.ip, // port = plc.port, // addr = plc.writeAddr + 1, // data = 1 // }); // } // else if (readRes.result[0] == 2) // { // //设备允许进料 // //改参数通知agv // if (TSHelper.GoToAGV(taskNo, 10, 3)) // { // WorkFlowAction.TrackLog(taskNo, 10, 1013, "success"); // //改成出料进行中 // CMMLog.Debug("三楼叠盘机:小车正在取托盘中!"); // var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel // { // host = plc.ip, // port = plc.port, // addr = plc.writeAddr + 1, // data = 2 // }); // } // else CMMLog.Debug($"三楼叠盘机,1013,TS,10,3,false!"); // } // } //} #endregion } /// /// 小车取货完成离开通知叠盘机 /// /// /// internal static bool StackingComplete(string ip, string taskNo) { var result = false; var plc = Settings.GetPlcInfo().Where(a => a.deviceType == ip).FirstOrDefault(); if (plc != null) { //1.0 读地址1设备是否动作完成 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr + 1, data = 3 }); result = true; CMMLog.Debug("读取设备【参数不为0】,AMS设置小车【动作完成】,等待设备【参数为0】时再改参数通知agv"); } return result; } #endregion #endregion #endregion #region 跨楼层任务 #region 货物入库流程——满托转运 #region 收到打包机下线信号,判断条件是否允许,选择性下发任务 /// /// 3楼打包机下线,判断打包机是否由生成打包下线的任务,如果有,自己生成两条任务 /// /// internal static void CheckPackingLineFull(Settings.PlcInfo plc) { //读plc信号,看有没有下线请求,判断是否已经有任务 //没有任务,继续plc,包装下线的物料信号 //检查打包机通道1是否有出料任务信号,如果有生成取料任务。 if (CheckStartFree(plc.location)) { try { var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); if (result != null && result.errCode == 0) { if (result.result[0] == 1) { //获取MODBUS通道里面存放的时间戳 var resultTime = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { addr = plc.readAddr + 10, host = plc.ip, port = plc.port, dataNum = 2 }); if (resultTime.errCode == 0) { string wmstaskno = ""; string traycode = ""; if (Settings.packChange == "0") { string timeCuoData = (resultTime.result[0] * 65536 + resultTime.result[1]).ToString(); CMMLog.Info($"获取到的MODBUS时间戳:{timeCuoData}"); Console.WriteLine($"获取到的MODBUS时间戳:{timeCuoData}"); //这里需要调用照相机接口,获取数据存入中间表,等到入库流程完成,将数据传送给WMS string deviceNo = ""; var timecuo = TimeCuo(timeCuoData, ref deviceNo); if (timecuo != null) { string timeStamp = timecuo.data.First().timeStamp.ToString(); string employeeId = timecuo.data.First().employeeID1.ToString(); // ERP变更-调用ERP接口传输相关数据 if (ERPService.ERPSwitch01 == "1") { // string employeeId = ERPService.GetEmployeeId(plc.readAddr + 18, plc.ip, plc.port); // MongoDBSingleton.Instance.Update(Query.EQ("timeStamp", int.Parse(timeStamp)), Update.Set("employeeId", employeeId), "TimeCuoInfoCom", UpdateFlags.None); // if (ERPService.ERPApiFunc(timeStamp)) // WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp, employeeId); WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp, employeeId); } else WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeStamp, timeStamp); } else CMMLog.Info($"时间戳接口返回值为空!"); } else if(Settings.packChange == "1") { bool req = WMSHelper.WMSIn(plc.location, "", ref wmstaskno, ref traycode, ""); } } else CMMLog.Info($"MODBUS时间戳数据返回值错误:返回值解析:{JsonConvert.SerializeObject(result)}"); } } } catch (Exception ex) { CMMLog.Error(ex.Message); } } } #endregion #region 打包机出口MODBUS交互(开始取料,取料完成) /// 小车请求进料 1 AMS写,PLC读 1-AGV到位信号;2-到位后,请求接驳滚动;3-取货完成; /// 设备允许出料 1 PLC写,AMS读 1-设备请求出料;2-允许进入;3-确认货物取走,送出物料; ///打包线下线——小车开始取货 internal static void PackingLineUnload1012(string ip, string taskNo, int port) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip && a.port == port).FirstOrDefault(); if (plc != null) { try { var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 1) { var write = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, data = 1, host = plc.ip, port = plc.port }); } else if (readRes.result[0] == 2) { TSHelper.GoToAGV(taskNo, 10, 1); var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, data = 2, host = plc.ip, port = plc.port }); } } } catch (Exception ex) { CMMLog.Info(ex.Message); } } } /// /// 打包线下线——小车取货完成 /// /// /// internal static void PackingLineComplete4(string ip, string taskNo, int port) { //取货完成后需要读取设备时间戳里面的数据,并将其暂存至中间表等到入库任务完成传输给WMS var plc = Settings.GetPlcInfo().Where(a => a.ip == ip && a.port == port).FirstOrDefault(); if (plc != null) { try { var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 3) { var write = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plc.writeAddr + 1, data = 3, host = plc.ip, port = plc.port }); } } } catch (Exception ex) { CMMLog.Info(ex.Message); } } } #endregion #region 电梯安全门对接 /// /// 入库一段任务请求进入 /// /// /// internal static void RuKuOne1013(TN_I_TASK_MST mst, int startfloor) { //对电梯的modbus通道进行安全开门对接, //var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "8").FirstOrDefault(); var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == mst.Ext3).FirstOrDefault(); if (pmInfo != null) { try { //进入Agv状态 var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.writeAddr + 2, data = 1, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr + 2}里面数据为1."); var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = pmInfo.ip, port = pmInfo.port, addr = pmInfo.readAddr + 1 }); CMMLog.Debug($"读取设备{pmInfo.location}通道{pmInfo.readAddr + 1}里面数据为{JsonConvert.SerializeObject(readRes)}."); if (readRes != null && readRes.errCode == 0) { //判断电梯状态是否是停梯状态,停梯状态则发对应的信号 if (readRes.result[0] == 0) { int num = startfloor == 1 ? 1 : startfloor == 2 ? 4 : startfloor == 3 ? 6 : 0; //写入电梯楼层 wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.writeAddr + 1, data = num, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr + 1}里面数据为{num}."); //读取输入寄存器还是输出寄存器待定,读取通道0的数据 var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 2, addr = pmInfo.readAddr + 2, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Debug($"读取设备{pmInfo.location}通道{pmInfo.readAddr + 2}里面数据为{JsonConvert.SerializeObject(result)}."); if (result != null && result.errCode == 0) { if (result.result[0] == 2 && result.result[1] == num) { var eleBit = MongoDBSingleton.Instance.FindOne(Query.EQ("taskNo", mst.CN_S_SOURCE_NO), "ElevatorTask"); //改终点为开启的电梯 TSHelper.GoToAGV(mst.CN_S_TASK_NO, 2, StockInstance.Instance.GetAGVCodeForBitCode(eleBit.endBit)); } } } } } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{mst.Ext3}"); } /// /// 入库一段任务请求退出 /// /// /// internal static bool RuKuOne6(TN_I_TASK_MST mst, int endfloor) { bool req = false; //对电梯的modbus通道进行安全开门对接, var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == mst.Ext3).FirstOrDefault(); if (pmInfo != null) { try { //写入楼层信号和关门信号 int floorNum = endfloor == 1 ? 1 : endfloor == 2 ? 4 : endfloor == 3 ? 6 : 0; int[] num = new int[2] { 4, floorNum }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = pmInfo.ip, addr = pmInfo.writeAddr, data = num,//原先是1,单个写入 port = pmInfo.port }); CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}."); req = true; } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{mst.Ext3}"); return req; } /// /// 入库二段任务请求进入 /// /// /// internal static void RuKuOne1012(TN_I_TASK_MST mst, int endfloor) { //对电梯的modbus通道进行安全开门对接, var eleBit = MongoDBSingleton.Instance.FindOne(Query.EQ("taskNo", mst.CN_S_SOURCE_NO), "ElevatorTask"); if(eleBit == null) CMMLog.Info($"当前任务来源号:{mst.CN_S_SOURCE_NO},请在ElevatorTask表中查询字段taskNo为当前任务来源号"); var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == eleBit.endBit02).FirstOrDefault(); CMMLog.Info($"二段任务 1312 1 bit:{eleBit.endBit02} deivicetype:{pmInfo.deviceType}"); if (pmInfo != null) { try { int num = endfloor == 1 ? 1 : endfloor == 2 ? 4 : endfloor == 3 ? 6 : 0; var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 3, addr = pmInfo.readAddr + 1, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Debug($"读取设备{pmInfo.location}通道{pmInfo.readAddr + 1}里面数据为{JsonConvert.SerializeObject(result)}."); if (result != null && result.errCode == 0) { if (result.result[0] == 0 && result.result[1] == 2 && result.result[2] == num) { TSHelper.GoToAGV(mst.CN_S_TASK_NO, 1, StockInstance.Instance.GetAGVCodeForBitCode(eleBit.endBit02)); } } #region //var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel //{ // dataNum = 1, // addr = pmInfo.readAddr + 5, // host = pmInfo.ip, // port = pmInfo.port //}); //var result2 = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel //{ // dataNum = 1, // addr = pmInfo.readAddr + 6, // host = pmInfo.ip, // port = pmInfo.port //}); //if (result != null && result2 != null) //{ // if (result.result[0] == 1 && result2.result[0] == endfloor) // { // //TSHelper.GoToAGV(mst.CN_S_TASK_NO, 10, 1); // //改终点为开启的电梯 // TSHelper.GoToAGV(mst.CN_S_TASK_NO, 1, StockInstance.Instance.GetAGVCodeForBitCode(eleBit.endBit02)); // } //} #endregion } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{eleBit.endBit02}"); } /// /// 入库二段任务请求退出 /// /// /// internal static bool RuKuOne4(TN_I_TASK_MST mst, int endfloor) { bool req = false; var eleBit = MongoDBSingleton.Instance.FindOne(Query.EQ("taskNo", mst.CN_S_SOURCE_NO), "ElevatorTask"); if (eleBit == null) CMMLog.Info($"当前任务来源号:{mst.CN_S_SOURCE_NO},请在ElevatorTask表中查询字段taskNo为当前任务来源号"); var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == eleBit.endBit02).FirstOrDefault(); CMMLog.Info($"二段任务 1312 2 bit:{eleBit.endBit02} deivicetype:{pmInfo.deviceType}"); if (pmInfo != null) { try { int[] num = new int[4] { 4, 0,0,0 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = pmInfo.ip, addr = pmInfo.writeAddr, data = num,//原先是1,单个写入 port = pmInfo.port }); CMMLog.Debug($"写入设备{pmInfo.location}通道{pmInfo.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}."); //var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // addr = pmInfo.writeAddr + 1, // data = 3, // host = pmInfo.ip, // port = pmInfo.port //}); req = true; } catch (Exception ex) { CMMLog.Error(ex.Message, ex); } } else CMMLog.Info($"请检查电梯配置中心有没有开启,当前电梯点位:{eleBit.endBit02}"); return req; } #endregion #endregion #region 电梯选择以及电梯点位赋值 /// /// 读取MODBUS返回允许使用的电梯点位,如果没有电梯可以使用,返回0 /// /// internal static string ElevatorTaskBit(int bit) { Console.WriteLine("进入电梯选择判断!"); CMMLog.Info("进入电梯选择判断!"); string location = ""; CMMLog.Info("满托转运流程,判断三楼两部电梯设备"); //满托转运流程,判断三楼两部电梯设备(获取到可用电梯之后,需要判断这部电梯的一楼以及三楼是否有任务占用,如果有,则不可用) var plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "8").FirstOrDefault(); var plc2 = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "11").FirstOrDefault(); if(bit == 2) { plc = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "12").FirstOrDefault(); plc2 = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "13").FirstOrDefault(); } if (plc != null || plc2 != null) { if (plc != null) { CMMLog.Info("进入3楼电梯1判断是否开启"); var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { addr = plc.readAddr, dataNum = 1,//数据个数要增加 addr以及dataNum的具体数据为16个 host = plc.ip, port = plc.port }); if (result != null && result.result.Length > 0) { if (result.result[0] == 3) { // 判断当前电梯的 一楼以及三楼 均没有任务占用,则可以使用 // (三楼电梯不能有任务执行中,一楼电梯取货完成可以使用) var oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "12").FirstOrDefault(); if (bit == 2) { oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "8").FirstOrDefault(); } var startTask = MongoDBSingleton.Instance.FindOne(Query.EQ("Ext3",location), "TN_I_TASK_MST"); bool endTaskState = false; var endTask = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_START_BIT", oneFloor.location), "TN_I_TASK_MST"); if (endTask != null) { if (WorkFlowAction.ExistsTrackLogs(endTask.CN_S_TASK_NO, 1, 4)) endTaskState = true; } else endTaskState = true; if (startTask == null && endTaskState) location = plc.location; } } } if (string.IsNullOrEmpty(location) && plc2 != null) { CMMLog.Info("进入三楼电梯2判断是否开启"); var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { addr = plc2.readAddr, dataNum = 1,//数据个数要增加 addr以及dataNum的具体数据为16个 host = plc2.ip, port = plc2.port }); CMMLog.Info($"获取电梯状态:{JsonConvert.SerializeObject(result)}"); if (result != null && result.result.Length > 0) { if (result.result[0] == 3) { // 判断当前电梯的 一楼以及三楼 均没有任务占用,则可以使用 var oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "13").FirstOrDefault(); if (bit == 2) { oneFloor = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "11").FirstOrDefault(); } var startTask = MongoDBSingleton.Instance.FindOne(Query.EQ("Ext3", location), "TN_I_TASK_MST"); bool endTaskState = false; var endTask = MongoDBSingleton.Instance.FindOne(Query.EQ("CN_S_START_BIT", oneFloor.location), "TN_I_TASK_MST"); if (endTask != null) { if (WorkFlowAction.ExistsTrackLogs(endTask.CN_S_TASK_NO, 1, 4)) endTaskState = true; } else endTaskState = true; if (startTask == null && endTaskState) location = plc2.location; } } } if (location == "") { Console.WriteLine("无电梯开启!"); CMMLog.Info("无电梯开启!"); location = "0"; } } else { CMMLog.Info("检查配置文件里面两部电梯是否有开启!"); return "0"; } Console.WriteLine("电梯选择判断结束!"); CMMLog.Info("电梯选择判断结束!"); return location; } /// /// 获取电梯点位赋值 /// /// /// internal static string ElevatorTaskBit02(string eleBit) { CMMLog.Info("赋值阶段2任务电梯起点"); string bit = ""; if (eleBit != "") { CMMLog.Info("两部电梯,四个配置均需要开启"); //1号电梯三楼和一楼电梯点位 var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "8").FirstOrDefault(); var plc2 = Settings.GetPlcInfo().Where(a => a.deviceType == "12").FirstOrDefault(); //2号电梯三楼和一楼电梯点位 var plc3 = Settings.GetPlcInfo().Where(a => a.deviceType == "11").FirstOrDefault(); var plc4 = Settings.GetPlcInfo().Where(a => a.deviceType == "13").FirstOrDefault(); if (eleBit == plc.location) bit = plc2.location; else if(eleBit == plc2.location) bit = plc.location; else if(eleBit == plc3.location) bit = plc4.location; else if(eleBit == plc4.location) bit = plc3.location; } else CMMLog.Info("EleBit is null!"); CMMLog.Info("获取电梯二段任务电梯点位:"+bit); return bit; } internal static void ElevatorTaskFloor(int startFloor,string bit) { CMMLog.Info($"终点安全对接:电梯卸货:呼叫楼层:{startFloor},当前电梯点位:{bit}"); var pmInfo = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.location == bit).FirstOrDefault(); if (pmInfo != null) { int[] num = new int[4] { startFloor, 0, 1, 1 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = pmInfo.ip, addr = pmInfo.writeAddr, data = num,//原先是1,单个写入 port = pmInfo.port }); } else CMMLog.Info($"终点安全对接:电梯卸货:未当前电梯点位的配置数据:{bit}"); //var write = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // addr = pmInfo.writeAddr+2, // data = 1, // host = pmInfo.ip, // port = pmInfo.port //}); //var write0 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // addr = pmInfo.writeAddr, // data = startFloor, // host = pmInfo.ip, // port = pmInfo.port //}); } #endregion #endregion public static DateTime TenMin = DateTime.MinValue; /// /// 每隔十分钟往所有开启的打包下线口MODBUS通道里面写小车电量 /// 其实地址26,连续往里面写入四个值 /// internal static void WriteBattery() { CMMLog.Info("写入小车电量:进入写入电量信息流程!"); //Console.WriteLine("写入小车电量:进入写入电量信息流程!"); if(TenMin == DateTime.MinValue) TenMin = DateTime.Now; else if (DateTime.Now.Subtract(TenMin).TotalMinutes >= 10) { Console.WriteLine("写入小车电量:写入所有开启的打包下线口MODBUS通道电量值,并重置时间为当前时间"); CMMLog.Info("写入小车电量:写入所有开启的打包下线口MODBUS通道电量值,并重置时间为当前时间"); //写入所有开启的打包下线口MODBUS通道电量值,并重置时间为当前时间 //获取所有电量值 int[] num = ProcessHelper.GetBatteryHang(); CMMLog.Info($"写入小车电量:写入数据:{JsonConvert.SerializeObject(num)}"); Console.WriteLine($"写入小车电量:写入数据:{JsonConvert.SerializeObject(num)}"); var PlcInfo04 = Settings.GetPlcInfo().Where(a => a.enable == 1 && a.deviceType == "4").FirstOrDefault();//plcInfo if (PlcInfo04 != null) { var wirteall = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { addr = PlcInfo04.writeAddr + 5, host = PlcInfo04.ip, data = num, port = PlcInfo04.port }); } TenMin = DateTime.Now; } } #region 数据处理业务 /// /// 获取设备的包装物料信息(并写入中间表) /// /// /// /// true : MES交互方式 false : 无MES交互方式 private static void GetMachineData(int[] result, ref string loc, bool noPack = true) { CMMLog.Info($"数据处理流程:进入数据处理流程!"); string trayCode = GetTrayCode(result.Take(3).ToArray());//托盘码 string location = GetTrayCode(result.Skip(3).Take(1).ToArray());//设备货位编码 loc = location; if(trayCode != null && location != null) { CMMLog.Info($"数据处理流程:trayCode:{trayCode},location:{location}"); var json = ""; if (!noPack) { CMMLog.Info($"数据处理流程:包装机无MES情况下,自动生成以当前包装机号{location}为首的数据"); var task = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", location), "MachineInfo"); if(task == null) { MongoDBSingleton.Instance.Insert(new MachineInfo { machineNo = location }); MongoDBSingleton.Instance.Insert(new MachineInfoTwo { machineNo = location, trayCode = trayCode }); } else CMMLog.Info($"数据处理流程:包装机无MES情况下,模拟MES数据,中间表中已有当前包装机号{location}为首的数据"); } var info = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", location), "MachineInfo"); if (info != null) { CMMLog.Info($"数据处理流程:info:{info}"); #region 获取设备通道信息 #region 转换后 info.location = location; info.trayCode = trayCode; //获取重量 叠包等其它信息 info.productWeight = Convert.ToInt32(Completion(result[4]) + Completion(result[5]), 2).ToString(); // ERP变更:根据最新信号协议 trayCodeWeight 变更为 【员工编号】通道 info.trayCodeWeight = Convert.ToInt32(Completion(result[6]) + Completion(result[7]), 2).ToString(); info.oneTrayWeight = Convert.ToInt32(Completion(result[8]) + Completion(result[9]), 2).ToString(); info.addWeight = Convert.ToInt32(Completion(result[10]) + Completion(result[11]), 2).ToString(); info.packNg = 1;//不需要此参数 result[12] info.secondNg = Convert.ToInt32(Completion(result[13]), 2); info.addState = Convert.ToInt32(Completion(result[14]), 2); if (info.addState == 0) CMMLog.Error($"数据处理流程:获取MODBUS信息异常:是否叠包值为0."); info.packageCode = Convert.ToInt32(Completion(result[15]), 2).ToString(); CMMLog.Info($"数据处理流程:获取MODBUS转换后的数据信息:location:{info.location},trayCode:{info.trayCode},productWeight:{info.productWeight}" + $",trayCodeWeight:{info.trayCodeWeight},oneTrayWeight:{info.oneTrayWeight},addWeight:{info.addWeight}" + $",packNg:{info.packNg},secondNg:{info.secondNg},addState:{info.addState},packageCode:{info.packageCode}"); #endregion #region 转换前 for (int i = 0; i <= 15; i++) { info.machinedown[i] = result[i]; } //将数组数据接受转换为JSON数据并在后续方法判断中存入中间表 json = JsonConvert.SerializeObject(info.machinedown); CMMLog.Info($"数据处理流程:获取MODBUS转换前的数据信息:{json}"); #endregion #endregion } else CMMLog.Info($"数据处理流程:请查询表中machineNo字段是否为当前包装机号,并且trayCode是否为0"); var infoPack = MongoDBSingleton.Instance.FindOne(Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0")), "MachineInfo"); if (infoPack != null) { //包装机下线至复称,读取包装机平台数据 CMMLog.Info("数据处理流程:进入info判断01"); //AMS包装机下线首先调用MES,之后获取新的设备数据存入中间表 //根据托盘号获取指定的通道的JSON数据,将其转换为数组类型——写入通道数据时会使用 //var arr = JsonConvert.DeserializeObject>(json); if(!noPack) { CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,并原样写入原先MES字段中。"); //info.lotNo = RemoveNull(GetTrayCode(result.Skip(20).Take(30).ToArray()).Trim().ToString()); //info.productType = RemoveNull(GetTrayCode(result.Skip(50).Take(10).ToArray()).Trim().ToString()); //info.materialCode = RemoveNull(GetTrayCode(result.Skip(60).Take(20).ToArray()).Trim().ToString()); info.lotNo = RemoveNull(GetTrayCode(result.Skip(20).Take(20).ToArray()).Trim().ToString()); // info.productType = RemoveNull(GetTrayCode(result.Skip(40).Take(10).ToArray()).Trim().ToString()); info.productType = ""; info.materialCode = RemoveNull(GetTrayCode(result.Skip(50).Take(20).ToArray()).Trim().ToString()); CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,新获得数据,lotNo:{info.lotNo},productType:{info.productType},materialCode:{info.materialCode}"); //注意:这里赋值中间表参数请对照信息交互表具体通道值对应 UpdateBuilder update = Update.Set("palletLayers", info.secondNg.ToString()).Set("overlappingLayers", info.addState.ToString()).Set("bagNo", info.packageCode). Set("lotNo", info.lotNo).Set("productType", info.productType).Set("materialCode", info.materialCode); MongoDBSingleton.Instance.Update(Query.EQ("machineNo", location), update, "MachineInfo", UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("trayCode", trayCode), update, "MachineInfoTwo", UpdateFlags.None); } //在中间表中找到刚刚插入的MES数据(目前还没有读取并写入设备数据) var query1 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0")); UpdateBuilder updateBuider = Update.Set("location", info.location). Set("trayCode", info.trayCode).Set("productWeight", info.productWeight). Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight). Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("addState", info.addState). Set("packageCode", info.packageCode).Set("jsonData", json).Set("modify", DateTime.Now); MongoDBSingleton.Instance.Update(query1, updateBuider, "MachineInfo", UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("trayCode", trayCode), updateBuider, "MachineInfoTwo", UpdateFlags.None); CMMLog.Info($"数据处理流程:更新MachineInfo中间表刚刚插入的MES数据!设备号为:{location},托盘号为:{trayCode}"); } else { //复称入缓存架,读取复称平台数据 CMMLog.Info("数据处理流程:进入info判断02"); //读取除包装机外其他设备的信息,更新包装机下线时的数据 var query2 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", trayCode)); var info2 = MongoDBSingleton.Instance.FindOne(query2, "MachineInfo"); if (info2 != null) { UpdateBuilder updateBuider = Update.Set("location", info.location). Set("trayCode", info.trayCode).Set("productWeight", info.productWeight). Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight). Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("addState", info.addState). Set("packageCode", info.packageCode).Set("jsonData", json).Set("modify", DateTime.Now); MongoDBSingleton.Instance.Update(query2, updateBuider, "MachineInfo", UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("trayCode", trayCode), updateBuider, "MachineInfoTwo", UpdateFlags.None); CMMLog.Info($"数据处理流程:更新MachineInfo中间表其他设备的数据!设备号为:{location},托盘号为:{trayCode}"); } else CMMLog.Info($"数据处理流程:无法在MachineInfo中间表中找到当前设备编号的数据!当前设备编号为:{location},托盘码为:{trayCode}"); } } else CMMLog.Error("数据处理流程:数据流程处理托盘号或包装机号为空值!"); CMMLog.Info($"数据处理流程:数据处理流程结束!"); } /// /// 获取设备的包装物料信息(并写入中间表) /// /// /// /// true : MES交互方式 false : 无MES交互方式 private static void GetMachineDataTetracobalt(int[] result, ref string timeStamp, bool noPack = true) { CMMLog.Info($"数据处理流程:进入数据处理流程!"); string empCode = RemoveNull(GetTrayCode(result.Take(5).ToArray()));//员工编码 string trayCode = GetTrayCode(result.Skip(5).Take(3).ToArray());//托盘码 trayCode = "0"; string location = GetTrayCode(result.Skip(8).Take(1).ToArray());//设备货位编码 if(trayCode != null && location != null) { CMMLog.Info($"数据处理流程:trayCode:{trayCode},location:{location},empCode:{empCode}"); var json = ""; if (!noPack) { CMMLog.Info($"数据处理流程:包装机无MES情况下,自动生成以当前包装机号{location}为首的数据"); var task = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", location), "MachineInfo"); if(task == null) { MongoDBSingleton.Instance.Insert(new MachineInfoTetracobalt { machineNo = location }); MongoDBSingleton.Instance.Insert(new MachineInfoTwoTetracobalt { machineNo = location, trayCode = trayCode }); } else CMMLog.Info($"数据处理流程:包装机无MES情况下,模拟MES数据,中间表中已有当前包装机号{location}为首的数据"); } var info = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", location), "MachineInfoTetracobalt"); if (info != null) { CMMLog.Info($"数据处理流程:info:{info}"); #region 获取设备通道信息 #region 转换后 info.empCode = empCode; info.location = location; info.trayCode = trayCode; //获取重量 叠包等其它信息 info.productWeight = Convert.ToInt32(Completion(result[9]) + Completion(result[10]), 2).ToString(); // ERP变更:根据最新信号协议 trayCodeWeight 变更为 【员工编号】通道 info.trayCodeWeight = Convert.ToInt32(Completion(result[11]) + Completion(result[12]), 2).ToString(); info.oneTrayWeight = Convert.ToInt32(Completion(result[13]) + Completion(result[14]), 2).ToString(); info.trayType = Convert.ToInt32(Completion(result[17]), 2).ToString(); info.addState = Convert.ToInt32(Completion(result[18]), 2); if (info.addState == 0) CMMLog.Error($"数据处理流程:获取MODBUS信息异常:是否叠包值为0."); info.packageCode = Convert.ToInt32(Completion(result[19]), 2).ToString(); info.productTime = Convert.ToInt32(Completion(result[20]) + Completion(result[21]), 2).ToString(); //info.addWeight = Convert.ToInt32(Completion(result[10]) + Completion(result[11]), 2).ToString(); //info.packNg = 1;//不需要此参数 result[12] //info.secondNg = Convert.ToInt32(Completion(result[13]), 2); CMMLog.Info($"数据处理流程:获取MODBUS转换后的数据信息:empCoe:{info.empCode},location:{info.location},trayCode:{info.trayCode},productWeight:{info.productWeight}" + $",trayCodeWeight:{info.trayCodeWeight},oneTrayWeight:{info.oneTrayWeight},trayType:{info.trayType}" + $",productTime:{info.productTime},addState:{info.addState},packageCode:{info.packageCode}"); #endregion #region 转换前 for (int i = 0; i <= 21; i++) { info.machinedown[i] = result[i]; } //将数组数据接受转换为JSON数据并在后续方法判断中存入中间表 json = JsonConvert.SerializeObject(info.machinedown); CMMLog.Info($"数据处理流程:获取MODBUS转换前的数据信息:{json}"); #endregion #endregion } else CMMLog.Info($"数据处理流程:请查询表中machineNo字段是否为当前包装机号,并且trayCode是否为0"); var infoPack = MongoDBSingleton.Instance.FindOne(Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0")), "MachineInfoTetracobalt"); if (infoPack != null) { //包装机下线至复称,读取包装机平台数据 CMMLog.Info("数据处理流程:进入info判断01"); //AMS包装机下线首先调用MES,之后获取新的设备数据存入中间表 //根据托盘号获取指定的通道的JSON数据,将其转换为数组类型——写入通道数据时会使用 //var arr = JsonConvert.DeserializeObject>(json); if(!noPack) { CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,并原样写入原先MES字段中。"); //info.lotNo = RemoveNull(GetTrayCode(result.Skip(20).Take(30).ToArray()).Trim().ToString()); //info.productType = RemoveNull(GetTrayCode(result.Skip(50).Take(10).ToArray()).Trim().ToString()); //info.materialCode = RemoveNull(GetTrayCode(result.Skip(60).Take(20).ToArray()).Trim().ToString()); info.lotNo =RemoveNull(GetTrayCode(result.Skip(35).Take(20).ToArray()).Trim().ToString()).TrimStart(); info.productType = RemoveNull(GetTrayCode(result.Skip(55).Take(10).ToArray()).Trim().ToString()).TrimStart(); info.materialCode = RemoveNull(GetTrayCode(result.Skip(65).Take(15).ToArray()).Trim().ToString()).TrimStart(); info.materialName = RemoveNull(GetTrayCode(result.Skip(80).Take(20).ToArray()).Trim().ToString()).TrimStart(); info.measurementUnit = RemoveNull(GetTrayCode(result.Skip(100).Take(5).ToArray()).Trim().ToString()).TrimStart(); CMMLog.Info($"数据处理流程:包装机无MES情况下,获取以往MES传输的数据,新获得数据,lotNo:{info.lotNo},productType:{info.productType},materialCode:{info.materialCode},materialName:{info.materialName},measurementUnit:{info.measurementUnit}"); //注意:这里赋值中间表参数请对照信息交互表具体通道值对应 UpdateBuilder update = Update.Set("palletLayers", info.secondNg.ToString()).Set("overlappingLayers", info.addState.ToString()).Set("bagNo", info.packageCode). Set("lotNo", info.lotNo).Set("productType", info.productType).Set("materialCode", info.materialCode).Set("materialName", info.materialName).Set("measurementUnit", info.measurementUnit); MongoDBSingleton.Instance.Update(Query.EQ("machineNo", location), update, "MachineInfoTetracobalt", UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("trayCode", trayCode), update, "MachineInfoTwoTetracobalt", UpdateFlags.None); } //在中间表中找到刚刚插入的MES数据(目前还没有读取并写入设备数据) timeStamp = ProcessHelper.GetTimeStamp(31, 1, 1); trayCode = "VW" + timeStamp; var query1 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", "0")); UpdateBuilder updateBuider = Update.Set("empCode", info.empCode).Set("location", info.location). Set("trayCode", trayCode).Set("productWeight", info.productWeight). Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight).Set("trayType", info.trayType).Set("addState", info.addState). Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("productTime", info.productTime).Set("packageCode", info.packageCode). Set("jsonData", json).Set("modify", DateTime.Now); MongoDBSingleton.Instance.Update(query1, updateBuider, "MachineInfoTetracobalt", UpdateFlags.None); MongoDBSingleton.Instance.Update(query1, updateBuider, "MachineInfoTwoTetracobalt", UpdateFlags.None); CMMLog.Info($"数据处理流程:更新MachineInfoTetracobalt中间表刚刚插入的MES数据!设备号为:{location},托盘号为:{trayCode}"); } else { //复称入缓存架,读取复称平台数据 CMMLog.Info("数据处理流程:进入info判断02"); //读取除包装机外其他设备的信息,更新包装机下线时的数据 var query2 = Query.And(Query.EQ("machineNo", location), Query.EQ("trayCode", trayCode)); var info2 = MongoDBSingleton.Instance.FindOne(query2, "MachineInfo"); if (info2 != null) { UpdateBuilder updateBuider = Update.Set("empCode", info.empCode).Set("location", info.location). Set("trayCode", info.trayCode).Set("productWeight", info.productWeight). Set("trayCodeWeight", info.trayCodeWeight).Set("oneTrayWeight", info.oneTrayWeight). Set("addWeight", info.addWeight).Set("packNg", info.packNg).Set("secondNg", info.secondNg).Set("productTime", info.productTime). Set("jsonData", json).Set("modify", DateTime.Now); MongoDBSingleton.Instance.Update(query2, updateBuider, "MachineInfoTetracobalt", UpdateFlags.None); MongoDBSingleton.Instance.Update(Query.EQ("trayCode", trayCode), updateBuider, "MachineInfoTwoTetracobalt", UpdateFlags.None); CMMLog.Info($"数据处理流程:更新MachineInfoTetracobalt中间表其他设备的数据!设备号为:{location},托盘号为:{trayCode}"); } else CMMLog.Info($"数据处理流程:无法在MachineInfoTetracobalt中间表中找到当前设备编号的数据!当前设备编号为:{location},托盘码为:{trayCode}"); } } else CMMLog.Error("数据处理流程:数据流程处理托盘号或包装机号为空值!"); CMMLog.Info($"数据处理流程:数据处理流程结束!"); } /// /// 获取复称点状态信息 /// /// public static SecondWeighState GetSecondWeighState(string location) { //传入当前设备编号,并将传入的设备编号放入复称中间表的来源变量中 string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault().location; var query = Query.EQ("location", PlcBit02); var model = MongoDBSingleton.Instance.FindOne(query, "SecondWeighState"); if (model == null) { MongoDBSingleton.Instance.Insert(new SecondWeighState { location = PlcBit02, from = location,full = 0,ng = 1 }); CMMLog.Debug($"判断复称状态信息服务,复称中间表数据为空,开始插入默认值!"); } else MongoDBSingleton.Instance.Update(query, Update.Set("from", location), "SecondWeighState", UpdateFlags.None); return model; } /// /// 获取复称点状态信息(四钴) /// /// public static SecondWeighState GetSecondWeighStateTetracobalt(string location) { //传入当前设备编号,并将传入的设备编号放入复称中间表的来源变量中 string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "22").FirstOrDefault().location; var query = Query.EQ("location", PlcBit02); var model = MongoDBSingleton.Instance.FindOne(query, "SecondWeighState"); if (model == null) { MongoDBSingleton.Instance.Insert(new SecondWeighState { location = PlcBit02, from = location,full = 0,ng = 1 }); CMMLog.Debug($"判断复称状态信息服务,复称中间表数据为空,开始插入默认值!"); } else MongoDBSingleton.Instance.Update(query, Update.Set("from", location), "SecondWeighState", UpdateFlags.None); return model; } /// /// 复称平台数据 /// public class SecondWeighState { public ObjectId _id { get; set; } public string location { get; set; } /// /// 0未知 1合格 2不合格 /// public int ng { get; set; } /// /// 复称平台货从哪里来的 /// public string from { get; set; } /// /// 批次号 /// public string batchNo { get; set; } /// /// 包装机下线重量 /// public int weight { get; set; } /// /// 1 满 0 空 /// public int full { get; set; } public string mesInfo { get; set; } } /// /// 获取复称点位位置 /// /// /// public static string GetSecondWeighBit() { //改成读settings var plc = Settings.GetPlcInfo().Where(a => a.deviceType == "2").FirstOrDefault(); return plc.location; } /// /// 将设备通道里面读取的16位short转成ascii字符串 /// /// /// public static string GetTrayCode(int[] data) { StringBuilder sb = new StringBuilder(); data.ToList().ForEach(a => { var bytes = BitConverter.GetBytes((short)a).Reverse().ToArray(); sb.Append(Encoding.ASCII.GetString(bytes)); }); return sb.ToString(); } /// /// 十进制转换为2进制(自动补全16位) /// /// /// public static string Completion(int data) { var result = Convert.ToString(data, 2).PadLeft(16, '0'); return result; } /// /// ascii转10进制(通过16进制中转) /// /// /// public static string AsciiToTen(string data) { byte[] oneSixbad = System.Text.ASCIIEncoding.Default.GetBytes(data); StringBuilder oneSixsb = new StringBuilder(); foreach (byte b in oneSixbad) { oneSixsb.Append(b.ToString("x")); } int oneSixA2 = Convert.ToInt32(oneSixsb.ToString(), 16); return oneSixA2.ToString(); } #region 叠盘机批量写入数据 #region 产品型号分离处理方法 //public static int[] DiePan(WMSInfo machine) //{ // //偏移量 +11 通道范围: 40311 ~ 40370 // CMMLog.Info($"进入DiePan数据处理方法"); // //10~19通道 // int[] num = new int[60]; // int[] num3 = new int[40]; // int a = 0;//托盘码处理 // for (int b = 0; b <= 2; b++) // { // num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2))); // a = a + 2; // } // num[3] = int.Parse(AsciiToTen(machine.location));//包装机号 // for (int b = 4; b <= 5; b++) // { // int k = b % 2 == 0 ? 0 : 16; // num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); // } // for (int b = 6; b <= 7; b++) // { // int k = b % 2 == 0 ? 0 : 16; // num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); // } // for (int b = 8; b <= 9; b++) // { // int k = b % 2 == 0 ? 0 : 16; // num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); // } // for (int b = 10; b <= 12; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0 // num[13] = string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers);//是否需要叠托盘 // num[14] = machine.addState;//是否需要叠包 // num[15] = int.Parse(machine.packageCode);//袋号 // //时间戳处理 // string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1); // for (int b = 16; b <= 17; b++) // { // int k = b % 2 == 0 ? 0 : 16; // num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); // } // for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0 // //19 = 30 31 ~ 60 // //30~59通道 60~70通道 // string pcHead = ""; // string cpHead = ""; // #region 31~60 // string data = machine.itemPCode; // CMMLog.Info($"31~60data1_1:{data}"); // if (data.Length % 2 != 0) // { // data = "0" + machine.itemPCode; // pcHead = "" + machine.itemPCode.Substring(0, 1); // } // CMMLog.Info($"31~60data1_2:{data}"); // int x = data.Length / 2; // int aa = 0; // for (int i = 0; i <= x - 1; i++) // { // num3[i] = int.Parse(AsciiToTen(data.Substring(aa, 2))); // aa = aa + 2; // } // for (int m = x; m <= 29; m++) num3[m] = 0;//将产品批次号无数据的通道全部置为 0 // CMMLog.Info($"itemPCode:" + JsonConvert.SerializeObject(num3)); // #endregion // #region 61~70 // string data2 = machine.itemCode; // CMMLog.Info($"61~70data2_1:{data2}"); // if (data2.Length % 2 != 0) // { // data2 = "0" + machine.itemCode; // cpHead = "" + machine.itemCode.Substring(0, 1); // } // CMMLog.Info($"61~70data2_2:{data2}"); // int y = data2.Length / 2; // CMMLog.Info($"{y}"); // int aaa = 0; // //4 2 32 9 41 // for (int i = 30; i <= y + 29; i++) // { // num3[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2))); // CMMLog.Info(JsonConvert.SerializeObject(num3[i])); // aaa = aaa + 2; // } // int mm = y + 30; // CMMLog.Info($"mm:{mm}"); // for (int mmm = mm; mmm <= 39; mmm++) // { // num3[mmm] = 0; // CMMLog.Info(JsonConvert.SerializeObject(num3[mmm])); // } // CMMLog.Info($"itemPCode+itemCode:{JsonConvert.SerializeObject(num3)}"); // #endregion // for (int i = 20; i <= 60; i++) num[i] = num3[i - 20]; // // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道 // if (pcHead != "" || cpHead != "") // { // if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead)); // if (cpHead != "") num[50] = int.Parse(AsciiToTen(cpHead)); // CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}"); // } // CMMLog.Info($"DiePan数据处理方法完毕"); // return num; //} #endregion public static int[] DiePan(WMSInfo machine) { //偏移量 +11 通道范围: 40311 ~ 40370 CMMLog.Info($"叠盘机数据处理."); //10~19通道 int[] num = new int[50];//ERP变更:变更后通道 50 变更前:60 #region 0~19 => 11~30 int a = 0;//托盘码处理 for (int b = 0; b <= 2; b++) { num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2))); a = a + 2; } num[3] = int.Parse(AsciiToTen(machine.location));//包装机号 for (int b = 4; b <= 5; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 6; b <= 7; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 8; b <= 9; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 10; b <= 12; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0 num[13] = string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers);//是否需要叠托盘 num[14] = machine.addState;//是否需要叠包 num[15] = int.Parse(machine.packageCode);//袋号 //时间戳处理 string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1); for (int b = 16; b <= 17; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0 #endregion string pcHead = ""; string cpHead = ""; #region 20~39 => 31~50 产品批次号 string data = machine.itemPCode; CMMLog.Info($"31~50data1_1:{data}"); if (data.Length % 2 != 0) { data = "0" + machine.itemPCode; pcHead = "" + machine.itemPCode.Substring(0, 1); } CMMLog.Info($"31~50data1_2:{data}"); //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道 int maxLength = 19 + (data.Length / 2);// data.Length / 2 至少为 1 int aa = 0; for (int i = 20; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2))); aa = aa + 2; } for (int x = maxLength + 1; x <= 39; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0 #endregion #region 40~49 => 51~60 产品型号 string data2 = machine.itemCode; CMMLog.Info($"51~60data2_1:{data2}"); if (data2.Length % 2 != 0) { data2 = "0" + machine.itemCode; cpHead = "" + machine.itemCode.Substring(0, 1); } CMMLog.Info($"51~60data2_2:{data2}"); maxLength = 39 + (data2.Length / 2); int aaa = 0; for (int i = 40; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2))); aaa = aaa + 2; } for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道 if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead)); if (cpHead != "") num[40] = int.Parse(AsciiToTen(cpHead)); if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}"); CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}"); return num; } public static int[] DiePanTwo(WMSInfo machine, MachineInfoTwo machineInfoTwo) { var ErpItemInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("item_code", machineInfoTwo.materialCode), "ERPItemTable"); CMMLog.Info($"3楼缓存架入叠托:收到信号5,查询MachineInfoTwo表信息:{JsonConvert.SerializeObject(ErpItemInfo)}。"); //偏移量 +11 通道范围: 40311 ~ 40390 CMMLog.Info($"叠盘机数据处理."); //10~19通道 int[] num = new int[90];//总长度:80 #region 0~19 => 11~30 int a = 0;//托盘码处理 for (int b = 0; b <= 2; b++) { num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2))); a = a + 2; } num[3] = int.Parse(AsciiToTen(machine.location));//包装机号 for (int b = 4; b <= 5; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 6; b <= 7; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 8; b <= 9; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 10; b <= 11; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0 num[12] = 1; num[13] = 2;//是否需要叠托盘 // string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers) num[14] = machine.addState;//是否需要叠包 num[15] = int.Parse(machine.packageCode);//袋号 //时间戳处理 string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1); for (int b = 16; b <= 17; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0 #endregion string pcHead = ""; string cpHead = ""; string mtHead = "";// 设备的真实物料编码 #region 20~39 => 31~50 产品批次号 string data = machineInfoTwo.lotNo; CMMLog.Info($"31~50data1_1:{data}"); if (data.Length % 2 != 0) { data = "0" + machineInfoTwo.lotNo; pcHead = "" + machineInfoTwo.lotNo.Substring(0, 1); } CMMLog.Info($"31~50data1_2:{data}"); //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道 int maxLength = 19 + (data.Length / 2);// data.Length / 2 至少为 1 int aa = 0; for (int i = 20; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2))); aa = aa + 2; } for (int x = maxLength + 1; x <= 39; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0 #endregion #region 40~49 => 51~60 产品型号 ERPService.HandleItemInfoChina(machine.itemCode, 40, 49, num);// 中文处理 70~84 81~95 //string data2 = machine.itemCode; //CMMLog.Info($"51~50data2_1:{data2}"); //if (data2.Length % 2 != 0) //{ // data2 = "0" + machine.itemCode; // cpHead = "" + machine.itemCode.Substring(0, 1); //} //CMMLog.Info($"51~60data2_2:{data2}"); //maxLength = 39 + (data2.Length / 2); //int aaa = 0; //for (int i = 40; i <= maxLength; i++) //{ // num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2))); // aaa = aaa + 2; //} //for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion #region 50~69 => 61~80 物料编码 string data3 = machineInfoTwo.materialCode; CMMLog.Info($"61~80data3_1:{data3}"); if (data3.Length % 2 != 0) { //data3 = "0" + machineInfoTwo.materialCode; mtHead = "" + machineInfoTwo.materialCode.Substring(0, 1); } CMMLog.Info($"61~80data3_2:{data3}"); maxLength = 49 + (data3.Length / 2); int aaaa = 0; for (int i = 50; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data3.Substring(aaaa, 2))); aaaa = aaaa + 2; } for (int x = maxLength + 1; x <= 69; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion ERPService.HandleItemInfoChina(ErpItemInfo.item_name, 70, 84, num);// 中文处理 70~84 81~95 ERPService.HandleItemInfo(ErpItemInfo.item_uom, 85, 89, num);// 85~89 96~100 // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道 if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead)); if (cpHead != "") num[40] = int.Parse(AsciiToTen(cpHead)); if (mtHead != "") num[50] = int.Parse(AsciiToTen(mtHead)); if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}"); CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}"); return num; } public static int[] DiePanThree(WMSInfo machine, MachineInfoTwo machineInfoTwo) { //偏移量 +11 通道范围: 40311 ~ 40390 CMMLog.Info($"叠盘机数据处理."); //10~19通道 int[] num = new int[80];//总长度:80 #region 0~19 => 11~30 int a = 0;//托盘码处理 for (int b = 0; b <= 2; b++) { num[b] = int.Parse(AsciiToTen(machine.trayCode.Substring(a, 2))); a = a + 2; } num[3] = int.Parse(AsciiToTen(machine.location));//包装机号 for (int b = 4; b <= 5; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.productWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 6; b <= 7; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.trayCodeWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 8; b <= 9; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(machine.oneTrayWeight), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 10; b <= 12; b++) num[b] = 0;// 叠包后实际重量 && 复称结果 默认:0 num[13] = string.IsNullOrEmpty(machine.palletLayers) ? 0 : int.Parse(machine.palletLayers);//是否需要叠托盘 num[14] = machine.addState;//是否需要叠包 num[15] = int.Parse(machine.packageCode);//袋号 //时间戳处理 string timeStamp = ProcessHelper.GetTimeStamp(32, 1, 1); for (int b = 16; b <= 17; b++) { int k = b % 2 == 0 ? 0 : 16; num[b] = Convert.ToInt32(Convert.ToString(int.Parse(timeStamp), 2).PadLeft(32, '0').ToString().Substring(k, 16), 2); } for (int b = 18; b <= 19; b++) num[b] = 0;//预留的两个通道 默认:0 #endregion string pcHead = ""; string cpHead = ""; string mtHead = "";// 设备的真实物料编码 #region 20~49 => 31~60 产品批次号 string data = machine.itemPCode; CMMLog.Info($"31~60data1_1:{data}"); if (data.Length % 2 != 0) { data = "0" + machine.itemPCode; pcHead = "" + machine.itemPCode.Substring(0, 1); } CMMLog.Info($"31~60data1_2:{data}"); //字符串长度 / 2 = 写入通道数量(两位一转) 列: 3L0050 写入三个通道 int maxLength = 19 + (data.Length / 2);// data.Length / 2 至少为 1 int aa = 0; for (int i = 20; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data.Substring(aa, 2))); aa = aa + 2; } for (int x = maxLength + 1; x <= 49; x++) num[x] = 0;//将产品批次号无数据的通道全部置为 0 #endregion #region 50~59 => 61~70 产品型号 string data2 = machine.itemCode; CMMLog.Info($"61~70data2_1:{data2}"); if (data2.Length % 2 != 0) { data2 = "0" + machine.itemCode; cpHead = "" + machine.itemCode.Substring(0, 1); } CMMLog.Info($"61~70data2_2:{data2}"); maxLength = 49 + (data2.Length / 2); int aaa = 0; for (int i = 50; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data2.Substring(aaa, 2))); aaa = aaa + 2; } for (int x = maxLength + 1; x <= 59; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion #region 60~79 => 71~90 产品型号 string data3 = machineInfoTwo.materialCode; CMMLog.Info($"71~90data3_1:{data3}"); if (data3.Length % 2 != 0) { data3 = "0" + machineInfoTwo.materialCode; mtHead = "" + machineInfoTwo.materialCode.Substring(0, 1); } CMMLog.Info($"71~90data3_2:{data3}"); maxLength = 59 + (data3.Length / 2); int aaaa = 0; for (int i = 60; i <= maxLength; i++) { num[i] = int.Parse(AsciiToTen(data3.Substring(aaaa, 2))); aaaa = aaaa + 2; } for (int x = maxLength + 1; x <= 79; x++) num[x] = 0;//将产品型号无数据的通道全部置为0 #endregion // 如果 产品批次号 或者 产品型号 位数 为 奇数,则将提前取出的 首字符 重新转码 写入 其所对应通道区域的 首通道 if (pcHead != "") num[20] = int.Parse(AsciiToTen(pcHead)); if (cpHead != "") num[50] = int.Parse(AsciiToTen(cpHead)); if (mtHead != "") num[60] = int.Parse(AsciiToTen(mtHead)); if (pcHead != "" || cpHead != "") CMMLog.Info($"产品型号或批次号数量为奇数,特将首位取出单独处理,以下为批次号和产品型号的十进制数值{num[20]},{num[50]}"); CMMLog.Info($"叠盘机数据处理完毕:{JsonConvert.SerializeObject(num)}"); return num; } #endregion #endregion public class Z_ChaiPanEmptyOrFull { public ObjectId _id { get; set; } /// /// 拆盘点位 /// public string ChaiPanBit { get; set; } /// /// 拆盘点位空满状态--空|满 /// public string ChaiPanState { get; set; } } public class ActionControlModel { public ObjectId _id { get; set; } /// /// 功能码-- 1-生成复称平台入缓存架任务 /// public string actionNo { get; set; } = "0"; /// /// 1-ip /// public string ext1 { get; set; } = ""; /// /// 1-CN_S_BATCH_NO /// public string ext2 { get; set; } = ""; public string ext3 { get; set; } = ""; public int ext4 { get; set; } = 0; public string machince { get; set; } = ""; } internal static void changeSignal() { CMMLog.Info("车辆状态处理:执行开始!"); try { //查询小车状态表,写入小车坐标和方向 并写入中间表 var agvState = MongoDBSingleton.Instance.FindAll(); if (agvState.Count > 0) { agvState.ForEach(a => { var stateInfo = Settings.GetAGVStateList().Where(it => it.AgvNo == a.forkliftNo).FirstOrDefault(); if (stateInfo != null) { int[] num = new int[6];//总长度:6 int divisor = 65536; // 除数 int xPos = int.Parse(a.xPos) * 10; int yPos = int.Parse(a.yPos) * 10; int xquotient = xPos / divisor; // 计算商数 int xremainder = xPos % divisor; // 计算余数 num[0] = xquotient; num[1] = xremainder; int yquotient = yPos / divisor; // 计算商数 int yremainder = yPos % divisor; // 计算余数 num[2] = yquotient; num[3] = yremainder; #region //int m = 0;//托盘码处理 //for (int b = 0; b <= 1; b++) //{ // num[b] = int.Parse(AsciiToTen(a.xPos.Substring(m, 2))); // m = m + 2; //} //m = 0; //for (int b = 2; b <= 3; b++) //{ // num[b] = int.Parse(AsciiToTen(a.yPos.Substring(m, 2))); // m = m + 2; //} #endregion num[4] = string.IsNullOrEmpty(a.Degree) ? 0 : int.Parse(a.Degree); //num[4] = int.Parse(a.Degree); num[5] = a.errCode != "0" || a.errCode2 != "0" || a.faildCode != "0" ? 2 : 0; //if (a.errCode != "0" || a.errCode2 != "0" || a.faildCode != "0") num[5] = 2; CMMLog.Info($"车辆状态处理:写入车辆数据:车号:{a.forkliftNo},ip:{stateInfo.ip},port:{stateInfo.port},data:{JsonConvert.SerializeObject(num)}"); var wirteal = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { addr = stateInfo.writeAddr, host = stateInfo.ip, port = stateInfo.port, data = num }); } else CMMLog.Info($"车辆状态处理:小车{a.forkliftNo}未配置"); }); } else CMMLog.Info("车辆状态处理:AGV_STATE 车辆状态表未获取到车辆状态数据!"); } catch(Exception ex) { CMMLog.Info($"车辆状态处理-异常:{ex.Message}!"); } CMMLog.Info("车辆状态处理:执行结束!"); Thread.Sleep(1000); } /// /// 返料站台送物料 /// /// internal static void LiftAction(Settings.PlcInfo plc, string taskNo, bool result) { if(plc.device == "24") { if (result) { //返料站台任务 //ASM写入通道0小车动作,1-取料 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr, data = 1 }); if (writeRes.errCode == 0) { //小车请求进入安全门交互 //小车请求进料,并且查询设备是否允许AGV进入 //读地址1设备是否 2-可以进入 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = plc.ip, addr = plc.readAddr, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 1}里面数据为{readRes.result[0]}."); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 2) { //设备允许进入,改参数通知AGV WorkFlowAction.TrackLog(taskNo, 10, 1013, "success"); TSHelper.GoToAGV(taskNo, 10, 1); } } else CMMLog.Debug($"入返料站台:1013,readRes:{readRes},readRes.errCode:{readRes.errCode}"); } } else { var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, port = plc.port, addr = plc.writeAddr, data = 3 }); } } } /// /// 3楼包装机取料至复称平台流程——任务开始前,判断包装机有没有下线信号,并判断当前复称平台是否NG以及有没有任务在进行中(四钴) /// /// internal static void CheckPackingMachineTetracobalt(Settings.PlcInfo pmInfo) { //var count = MongoDBSingleton.Instance.FindOne<>//occupy var count = MongoDBSingleton.Instance.FindOne(Query.EQ("occupy", "1"), "MachineInfoTetracobalt"); //检查包装机通道0是否有下料信号,如果有生成下料任务。线程循环读取,无需设置循环。——通道0参数为1 if (count == null) { // var secondInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("Bit", pmInfo.location), "secondResult"); // if(secondInfo == null || secondInfo.Status == "OK") // { if (PickUpStartFree(pmInfo.location) && PickUpEndFree(pmInfo.location)) { try { var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = pmInfo.readAddr, host = pmInfo.ip, port = pmInfo.port }); if (result != null && result.errCode == 0) { CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}"); //Console.WriteLine($"包装下线流程:读取包装机通道号为:{pmInfo.readAddr}里面的值为{result.result[0]}"); if (result != null && result.errCode == 0 && result.result.Count() > 0) { //1:取料 if (result.result[0] == 1 || result.result[0] == 5) { string PlcBit02 = Settings.GetPlcInfo().Where(a => a.deviceType == "22").FirstOrDefault().location; //包装下线任务,终点为复称位置(这里判断包装机起点,复称起点终点均无任务,则推送任务) if (ProcessHelper.PickUpStartFree(pmInfo.location) && ProcessHelper.CheckEndFree(PlcBit02) && ProcessHelper.PickUpStartFree(PlcBit02)) { //判断复称位置,没有货并且不为NG(ng参数0,1,2——0,不确定(包装机刚开始工作时)1,ok 2,ng) var state = GetSecondWeighStateTetracobalt(pmInfo.location); CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:判断复称位置空满状态为:{state.full},ng状态为:{state.ng}"); if (state.full == 0) { if (Settings.mesOpen == "1") { //可以生成任务,调mes接口获取生产信息 2022.7.28 变更 MES可能需要1~2年才能上线,先把MES功能注销 CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:获取MES之后进入info判断!"); var trayCode = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { addr = pmInfo.readAddr + 5, dataNum = 105,////ERP变更:90 原先:80 host = pmInfo.ip, port = pmInfo.port }); if (trayCode.errCode == 0 && trayCode.result.Length == 105) { //获取托盘码等信息 读取通道 11、12、13的数据作为托盘码 读取其它通道 重量 叠包等信息 所有数据存入MachineInfo表 string timeStamp = ""; GetMachineDataTetracobalt(trayCode.result, ref timeStamp, false); var tray = MongoDBSingleton.Instance.FindOne(Query.EQ("machineNo", pmInfo.location), "MachineInfoTetracobalt"); if (tray != null) { CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:MachineInfoTetracobalt数据插入成功!"); bool IsContLaterCode = true; if (ERPService.ERPSwitch01 == "1") { string employeeId = "G" + tray.empCode.PadLeft(7, '0'); CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:员工编码:{employeeId}"); // 判断当前【员工编号】通道信息读出来的员工编号是否已经存在于我们的员工信息表-ERPEmployeeTable(查询字段-employee_id) var erpEmployeeInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("employee_id", employeeId), "ERPEmployeeTable"); var erpItemInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("item_code", tray.materialCode), "ERPItemTable"); if (string.IsNullOrEmpty(tray.empCode) || string.IsNullOrEmpty(tray.materialCode) || erpEmployeeInfo == null || erpEmployeeInfo == null) { CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:人员表或者物料表未找到该数据,员工编号:{employeeId},物料编码:{tray.materialCode}!"); IsContLaterCode = false; } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:员工编码查询成功"); } if (IsContLaterCode) { //检索员工信息成功 var wirteall01 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.writeAddr, host = pmInfo.ip, port = pmInfo.port, data = 4 }); CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:ip:{pmInfo.ip},port:{pmInfo.port},addr:{pmInfo.writeAddr},写入4"); //string timeStamp = ProcessHelper.GetTimeStamp(31, 1, 1); //tray.trayCode = "VW" + timeStamp; //MongoDBSingleton.Instance.Update(Query.EQ("_id", tray._id), Update.Set("trayCode", tray.trayCode), UpdateFlags.None); //MongoDBSingleton.Instance.Update(Query.EQ("_id", tray._id), Update.Set("trayCode", tray.trayCode), UpdateFlags.None); CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:托盘码:{tray.trayCode}"); if (tray.addState == 0) { MongoDBSingleton.Instance.Remove(Query.EQ("machineNo", pmInfo.location), "MachineInfoTetracobalt", RemoveFlags.None); if (ERPService.ERPSwitch01 == "0") MongoDBSingleton.Instance.Remove(Query.EQ("machineNo", pmInfo.location), "MachineInfoTwo", RemoveFlags.None); CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:叠托层数为0,不允许生成任务"); } else if (tray.trayCode != "0" && !string.IsNullOrEmpty(tray.trayCode)) { if (result.result[0] == 1) { HHAmsExecuteResult req = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), pmInfo.location, PlcBit02, "3楼包装取料(四钴)", 0, tray.trayCode, timeStamp); if (req.success) { string weight = (double.Parse(tray.oneTrayWeight) / 100).ToString(); ERPService.packageInfo(tray.machineNo, tray.trayCode, tray.lotNo, weight); //记录托盘类型 var trayTypeInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("locCode", pmInfo.location), "trayTypeTable"); if (trayTypeInfo == null) { MongoDBSingleton.Instance.Insert(new trayTypeTable { locCode = pmInfo.location, trayType = tray.trayType }); } } } else { //var writeRes0 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // host = pmInfo.ip, // addr = pmInfo.writeAddr, // data = 5,//原先是1,单个写入 // port = pmInfo.port //}); //人工模式,无需生成任务 //删除MachineInfoTetracobalt表中的数据 MongoDBSingleton.Instance.Remove(Query.And(Query.EQ("machineNo", pmInfo.location), Query.EQ("trayCode", tray.trayCode)), "MachineInfoTetracobalt", RemoveFlags.None); //删除 MachineInfoTwoTetracobalt 重复的数据 var twoInfo = MongoDBSingleton.Instance.Find(Query.And(Query.EQ("lotNo", tray.lotNo), Query.EQ("packageCode",tray.packageCode)), "MachineInfoTwoTetracobalt"); if(twoInfo.Count > 1) { foreach(var a in twoInfo) { MongoDBSingleton.Instance.Remove(Query.EQ("_id", a._id),RemoveFlags.None); if (MongoDBSingleton.Instance.Find(Query.And(Query.EQ("lotNo", tray.lotNo), Query.EQ("packageCode", tray.packageCode)), "MachineInfoTwoTetracobalt").Count == 1) { break; } } } //记录包装间下料信息 string Weight = (decimal.Parse(tray.oneTrayWeight) / 100).ToString("F2"); MongoDBSingleton.Instance.Insert(new packageInfoModel { machineNo = tray.machineNo, trayCode = tray.trayCode, batchNo = tray.lotNo, time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), weight = Weight }); } } } else { CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:员工编码查询失败"); //检索员工信息失败写入对方通道值1 读取地址+3 var wirteall01 = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = pmInfo.readAddr + 3, host = pmInfo.ip, port = pmInfo.port, data = 1 }); } } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:tray==null!"); } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:3楼包装至复称获取MODBUS值错误!trayCode.errCode:{trayCode.errCode},trayCode.result.Length:{trayCode.result.Length}"); } } else { var task = MongoDBSingleton.Instance.FindOne(Query.Or(Query.EQ("CN_S_START_BIT", PlcBit02), Query.EQ("CN_S_END_BIT", PlcBit02)), "TN_I_TASK_MST"); if (task != null) CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!任务号为:{task.CN_S_TASK_NO}"); else CMMLog.Debug("(四钴车间)包装下线流程-{pmInfo.location}:检查复称中间表是否为空,不为空则检查其数据full以及ng是否为0和1!"); } } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:包装机起点或者复称起点终点有任务在执行中!包装机点位:{pmInfo.location},复称点位:{PlcBit02}"); } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:包装机通道0里面的数据不为1!result为:{JsonConvert.SerializeObject(result)}"); } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}"); } else CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:未读取到包装机通道0里面的数据!!!result:{JsonConvert.SerializeObject(result)}"); } catch (Exception ex) { CMMLog.Debug($"(四钴车间)包装下线流程-{pmInfo.location}:" + ex.Message); } } else { //var taskNo = MongoDBSingleton.Instance.FindOne(Query.Or(Query.EQ("CN_S_START_BIT", pmInfo.location), Query.EQ("CN_S_END_BIT", pmInfo.location)), "TN_I_TASK_MST"); //string task = "", startbit = "", endbit = ""; //if (taskNo != null) //{ // task = taskNo.CN_S_TASK_NO; // startbit = taskNo.CN_S_START_BIT; // endbit = taskNo.CN_S_END_BIT; //} CMMLog.Debug($"当前包装机起点终点有任务正在执行,请确认当前包装机的任务是否取货卸货完成!包装机货位编码:{pmInfo.location}"); } // } // else CMMLog.Info($"当前包装机出现故障,无法生成任务。包装机号:{pmInfo.location}"); } else CMMLog.Debug($"当前MachineInfoTetracobalt中间表已存在数据:{JsonConvert.SerializeObject(count)}"); } static object PickUpBlankCompleteLock = new object(); internal static void PickUpBlankCompleteTetracobalt(string PlcBitCache01, string Extend = "") { lock (PickUpBlankCompleteLock) { CMMLog.Info("包装线补空流程任务处理"); try { if (Extend != "") { CMMLog.Info($"当前周转托盘位:{Extend}"); int addr = 0; int qun = 0; //修改中间表的值,若中间表的数量为0,则调用解绑方法 var bzEmp = MongoDBSingleton.Instance.FindOne(Query.EQ("Bit", Extend), "BZEmptyPoint"); if (bzEmp != null) { CMMLog.Info($"当前货位的数量:{bzEmp.Quantity}"); if (bzEmp.Quantity == 1) { CMMLog.Info($"调用解绑接口"); //解绑 if (WMSHelper.WMSEmptyUnbind(Extend)) { //删除中间表数据 MongoDBSingleton.Instance.Remove(Query.EQ("Bit", Extend), RemoveFlags.Single); } } else { var quantity = bzEmp.Quantity - 1; var updateBuider = Update.Set("Quantity", quantity); MongoDBSingleton.Instance.Update(Query.EQ("Bit", Extend), updateBuider, UpdateFlags.None); CMMLog.Info($"减去当前周转托盘位数量之后,数量为:{quantity}"); qun = quantity; } } } } catch (Exception ex) { CMMLog.Error($"PickUpBlankComplete Error:" + ex.Message); } } } #region 包装线补空任务 internal static void PackingLine(string ip, string taskNo) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); //ASM写入小车动作,1:上料复称;2:上砝码教称;3:取复称NG托盘,4:取砝码到包装机 int[] num = new int[3] { 3, 0, 1 }; var writeRes0 = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = ip, addr = plc.writeAddr, data = num,//原先是1,单个写入 port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为{JsonConvert.SerializeObject(num)}."); //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为1."); ///小车和复称位对接 //小车请求进料,并且查询设备是否允许AGV进入 var readRes = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, host = plc.ip, addr = plc.readAddr + 2, port = plc.port }); CMMLog.Debug($"读取设备{plc.location}通道{plc.readAddr + 2}里面数据为{readRes.result[0]}."); //Console.WriteLine($"读取设备{plc.location}通道{plc.readAddr + 2}里面数据为{readRes.result[0]}."); if (readRes != null && readRes.errCode == 0) { if (readRes.result[0] == 2) { var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = ip, addr = plc.writeAddr + 2, data = 2, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为2."); //Console.WriteLine($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为2."); WorkFlowAction.TrackLog(taskNo, 10, 1013, "success"); TSHelper.GoToAGV(taskNo, 10, 3); } } } internal static void PackingLineComplete(string ip) { var plc = Settings.GetPlcInfo().Where(a => a.ip == ip).FirstOrDefault(); if (plc != null) { //写入包装机--安全门关门指令 var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = ip, addr = plc.writeAddr + 2, data = 3, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为3."); } else CMMLog.Debug($"包装线空托上线,3,ip=null!"); } internal static void PickUpBlankTwo(Settings.PlcInfo pmInfo) { //四钴车间 从地堆缓存位获取空托 if (ProcessHelper.PickUpEndFree(pmInfo.location)) { CMMLog.Info($"包装机补空托流程:查询包装机:{pmInfo.location}"); //读取输入寄存器还是输出寄存器待定,读取通道0的数据 var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = pmInfo.readAddr, host = pmInfo.ip, port = pmInfo.port }); CMMLog.Info($"包装机补空托流程:判断包装机通道{pmInfo.readAddr}值为{JsonConvert.SerializeObject(result)},ip:{pmInfo.ip}"); if (result != null && result.errCode == 0) { //3:人工叫空托盘(川字) 4:人工叫空托盘(田字) if ((pmInfo.deviceType == "1" && result.result[0] == 3)|| (pmInfo.deviceType == "21" &&(result.result[0] == 3 || result.result[0] == 4))) { //包装线补空流程 //判断空托缓存点(5 对 2)是否有空托,有空托判断是否有任务,有任务则判断另一个点位 var locInfo = Settings.GetAGVLocationList().Where(b => b.machineLoc.Contains(pmInfo.location) && b.Enabel == 1).First(); if (locInfo != null) { CMMLog.Info($"包装机点位:{pmInfo.location}"); foreach (var a in locInfo.ddLoc) { CMMLog.Info($"周转托盘位:{a}"); var ddInfo = Settings.GetDDSiteList().Where(b => b.ddLoc == a && b.Enable == 1).FirstOrDefault(); if (ddInfo != null) { if (ddInfo.trayType == result.result[0] - 2) { if (ProcessHelper.PickUpEndFree(pmInfo.location) && ProcessHelper.PickUpStartFree(a)) { var empInfo = MongoDBSingleton.Instance.FindOne(Query.EQ("Bit", a), "BZEmptyPoint"); if (empInfo != null && empInfo.Quantity > 0) { CMMLog.Info($"包装线补空任务,包装机点位:{pmInfo.location},周转托盘位:{a},周转托盘位数量:{empInfo.Quantity}"); HHAmsExecuteResult amsResult = AMSHelper.CreateTask(DateTime.Now.Ticks.ToString(), a, pmInfo.location, "包装线补空", 0, ""); if (amsResult.success) { CMMLog.Info($"包装线补空任务生成成功"); break; } } } } } } } else { CMMLog.Info("配置文件未配置包装机空托缓存点"); } } } else { CMMLog.Info($"err 包装机通道{pmInfo.readAddr} ip:{pmInfo.ip}"); } } } internal static void productOutWare(string endBit, string taskNo, bool action) { var plc = Settings.GetPlcInfo().Where(a => a.location == endBit).FirstOrDefault(); if (plc != null) { if (action) { int[] num = new int[3] {1,0,1}; var writeRes = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = plc.ip, addr = plc.writeAddr, data = num, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}."); var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 3, addr = plc.readAddr, host = plc.ip, port = plc.port }); CMMLog.Debug($"查询设备{plc.location}通道{plc.readAddr}里面数据为{result.result[0]}、{result.result[1]}、{result.result[2]}."); if (result != null && result.errCode == 0) { if (result.result[0] == 1 && result.result[1] == 1 && result.result[2] == 2) { //推送小车进入 TSHelper.GoToAGV(taskNo, 10, 3); int[] num1 = new int[2] { 1, 2}; writeRes = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = plc.ip, addr = plc.writeAddr + 1, data = num1, port = plc.port }); //writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel //{ // host = plc.ip, // addr = plc.writeAddr + 2, // data = 2, // port = plc.port //}); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 1}里面数据为{JsonConvert.SerializeObject(num1)}."); } } } else { var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr + 2, data = 3, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 2}里面数据为3."); } } } internal static void productInWare(string startBit, string taskNo, bool action) { var plc = Settings.GetPlcInfo().Where(a => a.location == startBit).FirstOrDefault(); if (plc != null) { if (action) { var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr + 3, host = plc.ip, port = plc.port }); CMMLog.Debug($"查询设备{plc.location}通道{plc.readAddr + 3}里面数据为{result.result[0]}."); if (result.result[0] == 1) { int[] num = new int[4] { 2, 0, 0, 1 }; var writeRes = OITcpHelper.RegisterWriteOutPutMulti(new OITcpHelper.RegisterWriteOutPutModelMulti { host = plc.ip, addr = plc.writeAddr, data = num, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr}里面数据为{JsonConvert.SerializeObject(num)}."); } result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 4, addr = plc.readAddr, host = plc.ip, port = plc.port }); CMMLog.Debug($"查询设备{plc.location}通道{plc.readAddr}里面数据为{result.result[0]}、{result.result[1]}、{result.result[3]}."); if (result != null && result.errCode == 0) { if (result.result[0] == 2 && result.result[1] == 2 && result.result[3] == 2) { //推送小车进入 TSHelper.GoToAGV(taskNo, 10, 1); var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr + 3, data = 2, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 3}里面数据为2."); } } } else { var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { host = plc.ip, addr = plc.writeAddr + 3, data = 3, port = plc.port }); CMMLog.Debug($"写入设备{plc.location}通道{plc.writeAddr + 3}里面数据为3."); } } } internal static void SendHeartBeat() { var taskInfo = MongoDBSingleton.Instance.Find(Query.Or(Query.EQ("CN_S_BUSS_TYPE", "电梯取货"), Query.EQ("CN_S_BUSS_TYPE", "电梯卸货")), "TN_I_TASK_MST"); if (taskInfo.Count > 0) { foreach (var item in taskInfo) { if (item.CN_S_BUSS_TYPE == "电梯卸货" || (item.CN_S_BUSS_TYPE == "电梯取货" && !WorkFlowAction.ExistsTrackLogs(item.CN_S_TASK_NO, 1, 4))) { //发送心跳信号 string DTBit = item.CN_S_BUSS_TYPE == "电梯卸货" ? item.CN_S_END_BIT : item.CN_S_START_BIT; var plcInfo = Settings.GetPlcInfo().Where(a => a.Extend == DTBit).FirstOrDefault(); if(plcInfo != null) { var wirte = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel { addr = plcInfo.writeAddr + 12, data = 1, host = plcInfo.ip, port = plcInfo.port }); } else { CMMLog.Info($"{DTBit}电梯未配置"); } } } } Thread.Sleep(100000); } internal static void CheckPackingLineFullThree(Settings.PlcInfo plc) { var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 1, addr = plc.readAddr + 1, host = plc.ip, port = plc.port }); CMMLog.Info($"下线口 ip:{plc.ip},port:{plc.port},addr:{plc.writeAddr + 1},reuslt:{JsonConvert.SerializeObject(result)}."); if(result != null && result.errCode == 0) { if (result.result[0] == 1) { var resultTime = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 26, addr = plc.readAddr + 8, host = plc.ip, port = plc.port }); CMMLog.Info($"下线口 ip:{plc.ip},port:{plc.port},addr:{plc.writeAddr + 8},reuslt:{JsonConvert.SerializeObject(resultTime)}."); if (resultTime.errCode == 0) { string timeCuoData = (resultTime.result[0] * 65536 + resultTime.result[1]).ToString(); string empCode = RemoveNull(GetTrayCode(resultTime.result.Skip(2).Take(5).ToArray()));//员工编码 string machineCode = RemoveNull(GetTrayCode(resultTime.result.Skip(10).Take(1).ToArray()));//包装机号 int needWeight = Convert.ToInt32(Completion(resultTime.result[11]) + Completion(resultTime.result[12]), 2);//产品需求重量 int oneWeight = Convert.ToInt32(Completion(resultTime.result[13]) + Completion(resultTime.result[14]), 2);//单托毛重 int readWeight = Convert.ToInt32(Completion(resultTime.result[15]) + Completion(resultTime.result[16]), 2);//单托净重 int fcWeight = Convert.ToInt32(Completion(resultTime.result[17]) + Completion(resultTime.result[18]), 2);//单托复称重量 int trayType = Convert.ToInt32(Completion(resultTime.result[19]), 2);//托盘类型 int addState = Convert.ToInt32(Completion(resultTime.result[20]), 2);//叠包层数 int bagNumber = Convert.ToInt32(Completion(resultTime.result[21]), 2);//袋号 int productTime = Convert.ToInt32(Completion(resultTime.result[22]) + Completion(resultTime.result[23]), 2);//生产日期 result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel { dataNum = 70, addr = plc.readAddr + 110, host = plc.ip, port = plc.port }); CMMLog.Info($"下线口,ip:{plc.ip},port:{plc.port},addr:{plc.readAddr + 140},result:{JsonConvert.SerializeObject(result)}"); string lotNo = RemoveNull(GetTrayCode(result.result.Skip(0).Take(20).ToArray()).Trim().ToString()); string materialCode = RemoveNull(GetTrayCode(result.result.Skip(30).Take(15).ToArray()).Trim().ToString()); CMMLog.Info($"下线口:timeCuoData:{timeCuoData},empCode:{empCode},machineCode:{machineCode},needWeight:{needWeight},oneWeight:{oneWeight},readWeight:{readWeight}" + $",fcWeight:{fcWeight},trayType:{trayType},addState:{addState},bagNumber:{bagNumber},productTime:{productTime},TimeCuo:{timeCuoData}"); var timeTwo = MongoDBSingleton.Instance.FindOne(Query.EQ("TimeCuo", timeCuoData), "TimeCuoInfoComTwo"); if (timeTwo == null) { MongoDBSingleton.Instance.Insert(new TimeCuoInfoComTwo { timeCuo = timeCuoData, employeeId = empCode, packingMachineNumber = machineCode, needWeight = needWeight, oneWeight = oneWeight, realWeight = readWeight, FCWeight = fcWeight, trayType = trayType.ToString(), isFold = addState, bagNumber = bagNumber, productData = productTime.ToString(), TimeCuo = timeCuoData, lotNo = lotNo, materialCode = materialCode }, "TimeCuoInfoComTwo"); } else { var query1 = Query.EQ("TimeCuo", timeCuoData); UpdateBuilder updateBuilder = Update.Set("employeeId", empCode).Set("packingMachineNumber", machineCode).Set("needWeight", needWeight) .Set("oneWeight", oneWeight).Set("realWeight", readWeight).Set("FCWeight", fcWeight).Set("trayType", trayType.ToString()) .Set("isFold", addState).Set("bagNumber", bagNumber).Set("productData", productTime.ToString()).Set("TimeCuo", timeCuoData) .Set("materialCode", materialCode); MongoDBSingleton.Instance.Update(query1, updateBuilder, "TimeCuoInfoComTwo", UpdateFlags.None); } string wmstaskno = ""; WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeCuoData, timeCuoData, empCode, true); } } } //string trayCode = ""; //var result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel //{ // dataNum = 25, // addr = plc.readAddr, // host = plc.ip, // port = plc.port //}); //CMMLog.Info($"贴标机获取信息,ip:{plc.ip},port:{plc.port},addr:{plc.readAddr},result:{JsonConvert.SerializeObject(result)}"); //if(result != null && result.errCode == 0) //{ // if (result.result[0] == 1) // { // //获取物料信息 根据托盘码区分 // trayCode = RemoveNull(GetTrayCode(result.result.Skip(6).Take(3).ToArray()));//托盘码 // // if (!string.IsNullOrEmpty(trayCode)) // { // var infoPack = MongoDBSingleton.Instance.FindOne(Query.And(Query.EQ("trayCode", trayCode)), "TimeCuoInfoComTwo"); // if (infoPack == null) // { // string empCode = RemoveNull(GetTrayCode(result.result.Skip(1).Take(5).ToArray()));//员工编码 // string machineCode = RemoveNull(GetTrayCode(result.result.Skip(9).Take(1).ToArray()));//包装机号 // int needWeight = Convert.ToInt32(Completion(result.result[10]) + Completion(result.result[11]), 2);//产品需求重量 // int oneWeight = Convert.ToInt32(Completion(result.result[12]) + Completion(result.result[13]), 2);//单托毛重 // int readWeight = Convert.ToInt32(Completion(result.result[14]) + Completion(result.result[15]), 2);//单托净重 // int fcWeight = Convert.ToInt32(Completion(result.result[16]) + Completion(result.result[17]), 2);//单托复称重量 // int trayType = Convert.ToInt32(Completion(result.result[18]), 2);//托盘类型 // int addState = Convert.ToInt32(Completion(result.result[19]), 2);//叠包层数 // int bagNumber = Convert.ToInt32(Completion(result.result[20]), 2);//袋号 // int productTime = Convert.ToInt32(Completion(result.result[21]) + Completion(result.result[22]), 2);//生产日期 // string timeCuoTwo = Convert.ToInt32(Completion(result.result[23]) + Completion(result.result[24]), 2).ToString(); //时间戳 // // UpdateBuilder updateBuilder = Update.Set("employeeId", empCode).Set("trayCode", trayCode).Set("packingMachineNumber", machineCode).Set("needWeight", needWeight) // .Set("oneWeight", oneWeight).Set("realWeight", readWeight).Set("FCWeight", fcWeight).Set("trayType", trayType) // .Set("isFold", addState).Set("bagNumber", bagNumber).Set("productData", productTime); // //产品批次号 产品型号 物料名称 计量单位均可在erp表中根据物料编码获取 // // result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel // { // dataNum = 15, // addr = plc.readAddr + 131, // host = plc.ip, // port = plc.port // }); // CMMLog.Info($"贴标机,ip:{plc.ip},port:{plc.port},addr:{plc.readAddr + 131},result:{JsonConvert.SerializeObject(result)}"); // string materialCode = RemoveNull(GetTrayCode(result.result.Skip(0).Take(15).ToArray()).Trim().ToString()); // UpdateBuilder update = Update.Set("materialCode", materialCode).Set("createTime", DateTime.Now); // MongoDBSingleton.Instance.Update(Query.EQ("trayCode", trayCode), update, UpdateFlags.None); // // var writeRes = OITcpHelper.RegisterWriteOutPut(new OITcpHelper.RegisterWriteOutPutModel // { // host = plc.ip, // addr = plc.writeAddr, // data = 1, // port = plc.port // }); // CMMLog.Debug($"贴标机,写入 ip:{plc.ip},port:{plc.port},addr:{plc.writeAddr}的数据为1."); // } // } // } //} // //var xxPlc = Settings.GetPlcInfo().Where(a => a.deviceType == "27" && a.enable == 1).FirstOrDefault(); //if(xxPlc != null) //{ // if (CheckStartFree(plc.location)) // { // result = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel // { // dataNum = 1, // addr = plc.readAddr + 1, // host = plc.ip, // port = plc.port // }); // // CMMLog.Debug($"下线口 ip:{plc.ip},port:{plc.port},addr:{plc.writeAddr + 1},reuslt:{JsonConvert.SerializeObject(result)}."); // if (result != null && result.errCode == 0) // { // if (result.result[0] == 1) // { // var resultTime = OITcpHelper.RegisterReadOutPut(new OITcpHelper.RegisterReadOutPutModel // { // dataNum = 2, // addr = plc.readAddr + 8, // host = plc.ip, // port = plc.port // }); // CMMLog.Debug($"下线口 ip:{plc.ip},port:{plc.port},addr:{plc.writeAddr + 8},reuslt:{JsonConvert.SerializeObject(result)}."); // if (resultTime.errCode == 0) // { // string timeCuoData = (resultTime.result[0] * 65536 + resultTime.result[1]).ToString(); // TimeCuoInfoComTwo time = null; // var timeTwo = MongoDBSingleton.Instance.Find(Query.EQ("trayCode", trayCode), "TimeCuoInfoComTwo"); // if (timeTwo.Count > 0) // { // time = timeTwo.OrderBy(a => a.createTime).FirstOrDefault(); // MongoDBSingleton.Instance.Update(Query.EQ("_id", time._id), Update.Set("TimeCuo", timeCuoData)); // } // string wmstaskno = ""; // WMSHelper.WMSIn(plc.location, "time", ref wmstaskno, ref timeCuoData, timeCuoData, time.employeeId, true); // } // } // } // } //} //else //{ // CMMLog.Info("下线口设备未配置"); //} } #endregion } }