Esempio n. 1
0
        public string GetPlcId()
        {
            lock (idPlc.ToString())
            {
                string errText = string.Empty;
//                string plcId = PlcFactory.Instance().ReadPlcDbValue("读取任务号%", ref errText);
                string plcId = PlcFactory.Instance().ReadPlcDbValue("FromPlc_seq%", ref errText);

                if (plcId == "01")
                {
                    idPlc = idPlc + 1;
                    plcId = idPlc.ToString();
                }

                if (int.Parse(plcId) > 99999)
                {
                    plcId = "1";
                    idPlc = 1;
                }
                else
                {
                    plcId = int.Parse(plcId).ToString();
                }

                return(plcId);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 通过Singleton模式返回当前实例
 /// </summary>
 public static PlcFactory Instance()
 {
     if (m_instance == null)
     {
         lock (m_lock)
         {
             if (m_instance == null)
             {
                 m_instance = new PlcFactory();
             }
         }
     }
     return(m_instance);
 }
Esempio n. 3
0
        //  public string SeqId = string.Empty;
        private void HandleMessageReceived(ClientManager cm, TimedEventArgs ea) //处理接收信息
        {
            ChatSocket chatSocket = cm.ChatSocket;


            if (chatSocket.Command == ChatCommand.PublicMessage)
            {
                // SendResponsePacketTo(aClient, chatSocket.Command, pck); if (chatSocket.Command == ChatCommand.PrivateMessage)
            }
            else
            {
                string     returnStr   = "";
                ChatSocket chatSockets = cm.ChatSocket;

                byte[] recby = chatSocket.ReceiveByte;
                string pck   = chatSockets.Metatype;
                //数据判断及解析
                if (recby != null)
                {
                    if (recby.Length < 3)
                    {
                        return;
                    }

                    for (int i = 0; i < recby.Length; i++)
                    {
                        returnStr += recby[i].ToString("X2"); //ToString("X2") 为C#中的字符串格式控制符
                    }
                }

                if (returnStr.Trim().Length < 5)
                {
                    return;
                }

                // 开始逻辑判断
                returnStr = returnStr.Substring(2);
                if (returnStr.Trim().Length == 0)
                {
                    return;
                }
                if (returnStr.Length < 12)
                {
                    return;
                }

                string seqWms = returnStr.Substring(4, 8);
                string flag   = returnStr.Substring(12, 2);

                if (flag == "84")
                {
                    Log.WriteLog(DateTime.Now + "接受顺丰落袋反馈消息:" + returnStr);
                    return;
                }

                if (flag == "85")
                {
                    Log.WriteLog(DateTime.Now + "接受顺丰打印反馈消息:" + returnStr);
                    return;
                }

                if (flag == "86")
                {
                    Log.WriteLog(DateTime.Now + "接受顺丰满袋反馈消息:" + returnStr);
                    return;
                }

                if (flag == "83")
                {
                    lock (lckdata)
                    {
                        try
                        {
                            string    postStr   = "AA" + returnStr;
                            Post      post      = new Post(postStr);
                            Data83Rec data83Rec = new Data83Rec(post.Data);
                            string    codeProt  = data83Rec.Code;
                            string    sendIdt   = "" + data83Rec.Seq;
                            // noread的情况下,code长度为0
                            if (0 == data83Rec.CodeLen)
                            {
                                codeProt = map[sendIdt].ToString();
                            }

                            map.Remove(sendIdt);
                            setCode("", "");
                            object[] values = new object[3];
                            values[0] = GetPlcId();

                            Log.WriteLog("接受顺丰格口返回消息:" + returnStr);
                            string port1 = data83Rec.Port;
                            Log.WriteLog(port1 + "   wms return ");
                            int truePort = 0;
                            //if (port1.Contains("F") || port1.Contains("E") || port1.Contains("D"))
                            //{
                            //    values[1] = 13;
                            //}
                            //else
                            //{
                            truePort  = Convert.ToInt32(port1, 16);
                            values[1] = truePort;
                            //}
                            values[2] = sendIdt;

                            PlcFactory.Instance().WritePlcDataDB("ToPlc_seq%", new[] { values[2] });
                            PlcFactory.Instance().WritePlcDataDB("ToPlc_barcode%", new object[] { codeProt });

                            //格口状态检测,异常则去19
                            string errText = String.Empty;
                            //string SL_State = PlcFactory.Instance().ReadPlcDbValue("满袋" + values[1].ToString() + "%", ref errText);
                            //if (SL_State.Equals("-1"))
                            //{
                            //    values[1] = 19;
                            //}
                            PlcFactory.Instance().WritePlcDataDB("ToPlc_bag%", new object[] { values[1] });
                            Log.WriteLog(values[0].ToString() + "写入PLC成功" + port1 + " plc" + sendIdt + "WMS反馈落袋格口:");
                            if (codeProt.Trim() != "")
                            {
                                //原先是从记录表开始存储  然后现在添加到正式表并更改记录表里条码状态字段
                                //datalog.AddDjmx(codeProt, values[0].ToString(), values[1].ToString());
                                //现在的思路 在开始存储的时候就存在正式表里 然后在这里更改条码的一些字段
                                createdata.UpdateDjmx(codeProt, values[0].ToString(), values[1].ToString());
                            }

                            //孙 添加数量
                            //int sum = 0;
                            //DataTable tb = createdata.SelectSum(values[1].ToString());
                            //if (tb.Rows.Count > 0)
                            //{
                            //    int test = Convert.ToInt32(tb.Rows[0]["sts"]);
                            //    if (DBNull.Value != tb.Rows[0]["count"])
                            //    {
                            //        int i = Convert.ToInt32(tb.Rows[0]["count"]);
                            //        sum = i + 1;
                            //    }
                            //    else
                            //    {
                            //        sum = 1;
                            //    }
                            //    createdata.UpdateProtStsSum(values[1].ToString(), sum);
                            //}
                        }
                        catch (Exception ex)
                        {
                            Log.WriteLog(DateTime.Now + "接受顺丰格口返回消息异常 :" + returnStr);
                            setCode("", "");
                        }
                    }
                    return;
                }

                if (flag == "88")
                {
                    string port  = returnStr.Substring(26, 4); //格口
                    string error = returnStr.Substring(30, 1); //格口
                    string ss    = returnStr.Substring(30, 1);
                    System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                    byte[] byteArray    = new byte[] { byte.Parse(ss) };
                    string strCharacter = asciiEncoding.GetString(byteArray);
                    //datalog.UpdateErrProtSts(port, strCharacter, "2");
                    createdata.UpdateErrProtSts(port, strCharacter, "2");
                    List <byte> wmsSeq = new List <byte>();
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(0, 2), 16));
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(2, 2), 16));
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(4, 2), 16));
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(6, 2), 16));
                    string seqId = string.Empty;
                    ErrWmsSendData(0x88, 0x0A, wmsSeq, ref seqId);
                    return;
                }

                if (flag == "89")
                {
                    string seqId = string.Empty;
                    string port  = returnStr.Substring(26, 4); //格口
                    //datalog.UpdateErrProtSts(port, "", "0");
                    createdata.UpdateErrProtSts(port, "", "0");
                    List <byte> wmsSeq = new List <byte>();
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(0, 2), 16));
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(2, 2), 16));
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(4, 2), 16));
                    wmsSeq.Add(Convert.ToByte(seqWms.Substring(6, 2), 16));

                    ErrWmsSendData(0x89, 0x0A, wmsSeq, ref seqId);
                    return;
                }

                #region 心跳回复

                if (flag == "80") //心跳回复
                {
                    new Thread((ThreadStart) delegate
                    {
                        try
                        {
                            List <byte> wmsSeq = new List <byte>();
                            wmsSeq.Add(0x00);
                            wmsSeq.Add(Convert.ToByte(seqWms.Substring(0, 2), 16));
                            wmsSeq.Add(Convert.ToByte(seqWms.Substring(2, 2), 16));
                            wmsSeq.Add(Convert.ToByte(seqWms.Substring(4, 2), 16));
                            wmsSeq.Add(Convert.ToByte(seqWms.Substring(6, 2), 16));
                            string data  = string.Empty;
                            byte[] array = System.Text.Encoding.ASCII.GetBytes("LS01");
                            for (int j = 0; j < array.Length; j++)
                            {
                                wmsSeq.Add(array[j]);
                            }

                            string seqId = string.Empty;
                            ErrWmsSendData(0x80, 0x0E, wmsSeq, ref seqId);
                        }
                        catch (Exception ex)
                        {
                            Log.WriteLog(seqWms);
                        }
                    }).Start();
                    return;

                    ;
                }

                #endregion

                #region 自检消息回复

                if (flag == "81") //自检消息回复
                {
                    new Thread((ThreadStart) delegate
                    {
                        string errtext = string.Empty;

                        List <byte> wmsSeq = new List <byte>();
                        wmsSeq.Add(0x00);
                        wmsSeq.Add(Convert.ToByte(seqWms.Substring(0, 2), 16));
                        wmsSeq.Add(Convert.ToByte(seqWms.Substring(2, 2), 16));
                        wmsSeq.Add(Convert.ToByte(seqWms.Substring(4, 2), 16));
                        wmsSeq.Add(Convert.ToByte(seqWms.Substring(6, 2), 16));
                        string seqId = string.Empty;
                        ErrWmsSendData(0x81, 0x0A, wmsSeq, ref seqId);

                        #region 检测设备

                        string sts          = PlcFactory.Instance().ReadPlcDbValue("读取序号%", ref errtext).ToString();
                        List <byte> wcsData = new List <byte>();
                        wmsSeq.Clear();
                        if (sts != "01") //Plc故障
                        {
                            // List<byte> wmsSeq = new List<byte>();
                            wmsSeq.Add(0x00);

                            ErrWmsSendData(0x82, 0x06, wmsSeq, ref seqId);
                        }
                        else
                        {
                            wmsSeq.Add(0x01);

                            ErrWmsSendData(0x82, 0x06, wmsSeq, ref seqId);
                        }
                    }).Start();

                    #endregion
                }

                #endregion
            }
        }