예제 #1
0
        /// <summary>
        /// 轮询
        /// </summary>
        private void Polling()
        {
            //定时轮询PLC
            while (true)
            {
                try
                {
                    //根据PLCCode加载呼叫盒子信息
                    CallBoxInfo CallInfo = CoreData.AllCallBoxes.FirstOrDefault(P => P.CallBoxID == PLCCode);
                    if (CallInfo == null)
                    {
                        LogHelper.WriteCallBoxLog("未匹配到呼叫器:" + PLCCode + "档案信息!");
                        return;
                    }

                    int readLen = CallInfo.ReadLenth;
                    if (readLen == 0)
                    {
                        LogHelper.WriteCallBoxLog("未配置呼叫器:" + PLCCode + "读取长度!");
                        return;
                    }
                    if (CallInfo.CallType == 1)//呼叫类型的
                    {
                        var t = CoreData.AllCallBoxDetail.Where(z => z.CallBoxID == PLCCode).Select(k => k.DBAddress).Distinct();
                        foreach (var address in t)
                        {
                            OperateResult <short[]> read = melsecMc.ReadInt16(address, (ushort)readLen);
                            if (!read.IsSuccess)
                            {
                                LogHelper.WriteCallBoxLog("读取呼叫器失败!  " + string.Format("[{0},{2},{1}]", PLCCode, address, ConnParam.ServerIP));
                                OperateResult opr = melsecMc.ConnectServer();
                                melsecMc.ConnectClose();
                                melsecMc.Dispose();
                                Thread.Sleep(5000);
                                melsecMc = new SiemensS7Net(SiemensPLCS.S1200)
                                {
                                    IpAddress = this.ConnParam.ServerIP, Port = this.ConnParam.Port
                                };
                                melsecMc.SetPersistentConnection();

                                LogHelper.WriteCallBoxLog("重连呼叫器!  " + string.Format("[{0},{2},{1}] ", PLCCode, address, ConnParam.ServerIP));
                                continue;
                            }
                            string SenDLog = "";
                            foreach (short item in read.Content)
                            {
                                SenDLog += ((int)item).ToString("X") + " ";
                            }
                            LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, address, ConnParam.ServerIP) + "内容为:" + SenDLog);
                            int site            = Convert.ToInt32(read.Content[0]);
                            int PlcRequestState = Convert.ToInt32(read.Content[2]);
                            if (PlcRequestState == 1)
                            {
                                int  RequestContent   = Convert.ToInt32(read.Content[1]);
                                var  cdetail          = CoreData.AllCallBoxDetail.FirstOrDefault(p => p.CallBoxID == PLCCode && p.DBAddress == address && p.ButtonID == RequestContent);
                                bool HandleCallResult = HandleCallBoxCall(CallInfo, cdetail, cdetail.ButtonID);
                                if (!HandleCallResult)
                                {
                                    LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "地址 :" + address + " 呼叫失败!");
                                }
                                #region 处理需要回写的子任务
                                IList <DispatchTaskDetail> detaillist = AGVClientDAccess.LoadTaskDetailByPLCCode(PLCCode, cdetail.LocationID);
                                foreach (var detail in detaillist)
                                {
                                    if (detail != null && (!string.IsNullOrEmpty(detail.dispatchNo)))
                                    {
                                        //写确认信息
                                        List <byte> write = new List <byte>();
                                        //{ BitConverter.GetBytes(read.Content[0]), BitConverter.GetBytes(read.Content[1]) , BitConverter.GetBytes(read.Content[2])}
                                        write.Add(BitConverter.GetBytes(read.Content[0])[1]);
                                        write.Add(BitConverter.GetBytes(read.Content[0])[0]);
                                        write.Add(BitConverter.GetBytes(read.Content[1])[1]);
                                        write.Add(BitConverter.GetBytes(read.Content[1])[0]);
                                        write.Add(BitConverter.GetBytes(read.Content[2])[1]);
                                        write.Add(BitConverter.GetBytes(read.Content[2])[0]);
                                        OperateResult opr       = melsecMc.Write(address.Replace(".0", ".6"), write.ToArray());
                                        int           writcount = 1;
                                        while (!opr.IsSuccess)
                                        {
                                            if (writcount > 3)
                                            {
                                                opr = melsecMc.Write(address, write.ToArray());
                                                LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "地址 :" + address + "失败!");
                                                break;
                                            }
                                            writcount++;
                                            Thread.Sleep(1000);
                                        }
                                        if (opr.IsSuccess)
                                        {
                                            AGVClientDAccess.UpdatePlcCallBackState(detail.dispatchNo, detail.DetailID);
                                            LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "地址 :" + address + "成功!");
                                        }
                                    }
                                }
                                #endregion

                                #region 处理需要回写物料信息的子任务
                                IList <DispatchTaskDetail> goodsdetaillist = AGVClientDAccess.LoadGoodsInfoTaskDetailByPLCCode(PLCCode, cdetail.LocationID);
                                foreach (var detail in goodsdetaillist)
                                {
                                    if (detail != null && (!string.IsNullOrEmpty(detail.dispatchNo)) && (!string.IsNullOrEmpty(detail.GoodsInfo)))
                                    {
                                        //写确认信息
                                        List <byte> write = new List <byte>();
                                        string[]    arr   = detail.GoodsInfo.Split(',');
                                        foreach (var s in arr)
                                        {
                                            if (!string.IsNullOrEmpty(s))
                                            {
                                                short ts = short.Parse(s);
                                                write.Add(BitConverter.GetBytes(ts)[1]);
                                                write.Add(BitConverter.GetBytes(ts)[0]);
                                            }
                                        }
                                        OperateResult opr       = melsecMc.Write(address.Replace(".0", ".12"), write.ToArray());
                                        int           writcount = 1;
                                        while (!opr.IsSuccess)
                                        {
                                            if (writcount > 3)
                                            {
                                                opr = melsecMc.Write(address.Replace(".0", ".12"), write.ToArray());
                                                LogHelper.WriteCallBoxLog("回写呼叫器 物料编码:" + PLCCode + "地址 :" + address + "失败!");
                                                break;
                                            }
                                            writcount++;
                                            Thread.Sleep(1000);
                                        }
                                        if (opr.IsSuccess)
                                        {
                                            AGVClientDAccess.UpdateGoodsCallBackState(detail.dispatchNo, detail.DetailID);
                                            LogHelper.WriteCallBoxLog("回写呼叫器 物料编码:" + PLCCode + "地址 :" + address + "成功!");
                                        }
                                    }
                                }
                                #endregion
                            }
                        }

                        #region OlD
                        ////根据当前呼叫器ID加载呼叫器明细
                        //OperateResult<short[]> read = melsecMc.ReadInt16(ReadAddr, (ushort)readLen);
                        //if (!read.IsSuccess)
                        //{
                        //    LogHelper.WriteCallBoxLog("读取呼叫器:" + PLCCode + "失败!");
                        //    return;
                        //}

                        //string SenDLog = "";
                        //foreach (byte item in read.Content)
                        //{ SenDLog += ((int)item).ToString("X") + " "; }
                        //LogHelper.WriteCallBoxLog("读取呼叫器:" + PLCCode + "内容为:" + SenDLog);

                        //int site = Convert.ToInt32(read.Content[0]);
                        //int PlcRequestState = Convert.ToInt32(read.Content[1]);
                        //string[] codes = GetCode(read.Content);
                        //if (codes.Length != 2)
                        //{
                        //    LogHelper.WriteCallBoxLog("读取呼叫器:" + PLCCode + " 物料编码获取失败");
                        //}
                        //if (PlcRequestState == 1)
                        //{
                        //    int RequestContent = Convert.ToInt32(read.Content[2]);
                        //    CallBoxDetail CallDetail = CoreData.AllCallBoxDetail.FirstOrDefault(p => p.CallBoxID == PLCCode && p.ButtonID == RequestContent);
                        //    if (CallDetail == null)
                        //    {
                        //        LogHelper.WriteCallBoxLog("未配置呼叫器:" + PLCCode + "按钮号:" + RequestContent.ToString() + "明细!");
                        //        return;
                        //    }
                        //    bool HandleCallResult = HandleCallBoxCall(CallInfo, CallDetail, RequestContent, codes);

                        //    if (HandleCallResult)
                        //    {
                        //        ////写确认信息
                        //        //byte[] write = new byte[] { (byte)site, 0x00, (byte)RequestContent, (byte)site, 0x01, (byte)RequestContent };
                        //        //OperateResult opr = melsecMc.Write(ReadAddr, write);
                        //        //int writcount = 1;
                        //        //while (!opr.IsSuccess)
                        //        //{
                        //        //    if (writcount > 3)
                        //        //    {
                        //        //        opr = melsecMc.Write(ReadAddr, write);
                        //        //        LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "失败!");
                        //        //        break;
                        //        //    }
                        //        //    writcount++;
                        //        //    Thread.Sleep(1000);
                        //        //}
                        //        //if (opr.IsSuccess)
                        //        //{
                        //        //    LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "成功!");
                        //        //}

                        //        //LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "呼叫成功!");
                        //    }
                        //    else
                        //    {
                        //        LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "呼叫失败!");
                        //    }


                        //    #region 处理需要回写的子任务
                        //    IList<DispatchTaskDetail> detaillist = AGVClientDAccess.LoadTaskDetailByPLCCode(PLCCode, CallDetail.LocationID);
                        //    foreach (var detail in detaillist)
                        //    {
                        //        if (detail != null && (!string.IsNullOrEmpty(detail.dispatchNo)))
                        //        {
                        //            //写确认信息
                        //            List<byte> write = new List<byte>();
                        //            //{ BitConverter.GetBytes(read.Content[0]), BitConverter.GetBytes(read.Content[1]) , BitConverter.GetBytes(read.Content[2])}
                        //            write.Add(BitConverter.GetBytes(read.Content[0])[1]);
                        //            write.Add(BitConverter.GetBytes(read.Content[0])[0]);
                        //            write.Add(BitConverter.GetBytes(read.Content[1])[1]);
                        //            write.Add(BitConverter.GetBytes(read.Content[1])[0]);
                        //            write.Add(BitConverter.GetBytes(read.Content[2])[1]);
                        //            write.Add(BitConverter.GetBytes(read.Content[2])[0]);


                        //            OperateResult opr = melsecMc.Write(ReadAddr.Replace(".0", ".6"), write.ToArray());
                        //            int writcount = 1;
                        //            while (!opr.IsSuccess)
                        //            {
                        //                if (writcount > 3)
                        //                {
                        //                    opr = melsecMc.Write(ReadAddr, write.ToArray());
                        //                    LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "失败!");
                        //                    break;
                        //                }
                        //                writcount++;
                        //                Thread.Sleep(1000);
                        //            }
                        //            if (opr.IsSuccess)
                        //            {
                        //                AGVClientDAccess.UpdatePlcCallBackState(detail.dispatchNo, detail.DetailID);
                        //                LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "成功!");
                        //            }
                        //            LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "呼叫成功!");
                        //        }
                        //    }
                        //    #endregion

                        //}
                        #endregion
                    }
                    else if (CallInfo.CallType == 2)//监控
                    {
                        var t = CoreData.AllCallBoxDetail.Where(p => p.CallBoxID == CallInfo.CallBoxID);
                        foreach (var d in t)
                        {
                            //if (d.DBAddress != "DB1000.0")
                            //{
                            //    continue;
                            //}
                            if (d.LocationState == 0)
                            {
                                //根据当前呼叫器ID加载呼叫器明细
                                OperateResult <short[]> read = melsecMc.ReadInt16(d.DBAddress, 1);
                                if (!read.IsSuccess)
                                {
                                    LogHelper.WriteCallBoxLog("读取呼叫器失败!  " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP));


                                    melsecMc.ConnectClose();
                                    melsecMc.Dispose();
                                    Thread.Sleep(5000);
                                    melsecMc = new SiemensS7Net(SiemensPLCS.S1200)
                                    {
                                        IpAddress = this.ConnParam.ServerIP, Port = this.ConnParam.Port
                                    };
                                    melsecMc.SetPersistentConnection();

                                    LogHelper.WriteCallBoxLog("重连呼叫器!  " + string.Format("[{0},{2},{1}] ", PLCCode, d.DBAddress, ConnParam.ServerIP));
                                    continue;
                                }
                                string SenDLog = "";
                                foreach (short item in read.Content)
                                {
                                    SenDLog += ((int)item).ToString("X") + " ";
                                }
                                LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP) + "内容为:" + SenDLog);
                                if (Convert.ToInt32(read.Content[0]) == 1)
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 0);
                                }
                                else
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 1);
                                }
                            }
                            else if (d.LocationState == 2)
                            {
                                //根据当前呼叫器ID加载呼叫器明细
                                OperateResult <short[]> read = melsecMc.ReadInt16(d.DBAddress, 15);
                                if (!read.IsSuccess)
                                {
                                    LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP) + "失败!");
                                    continue;
                                }
                                string SenDLog = "";
                                foreach (short item in read.Content)
                                {
                                    SenDLog += ((int)item).ToString("X") + " ";
                                }
                                LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP) + "内容为:" + SenDLog);
                                string codes = "";
                                foreach (short s in read.Content.Skip(3))
                                {
                                    codes += s.ToString() + ",";
                                }
                                if (Convert.ToInt32(read.Content[0]) == 1)
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 2, codes);
                                }
                                else
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 0, codes);
                                }
                            }
                        }
                    }
                    else//无,呼叫器类型按照按钮来区分,秦川不支持
                    {
                    }
                }
                catch (Exception ex)
                { LogHelper.WriteCallBoxLog("处理呼叫器:" + PLCCode + "任务失败->" + ex.Message); }

                Thread.Sleep(1000 * 2);
            }
        }