Beispiel #1
0
        private void DataChange(string Data, int seq)
        {
            try
            {
                #region  快递路向分拣
                lock (DataReadLock)    //多线程避免同时运行造成数据混乱。lock的一般是对象,不是数值和字符串。
                {
                    string      code         = Data;
                    string      twoDimencode = string.Empty; //二维码
                    string      linercode    = string.Empty; //一维码
                    string      seqOlcId     = string.Empty;
                    List <byte> wmsSeq       = new List <byte>();
                    Log.WriteLog("扫描条码" + code);
                    var time0 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");

                    tb_message.AppendText(DateTime.Now.ToString() + "扫描条码" + code + "\r\n");
                    byte lendata = 0;
                    if (code == "noread")
                    {
                        tb_message.AppendText(DateTime.Now.ToString() + "扫码失败 包裹区异常口" + "\r\n");
                        code      = "noread";
                        lendata   = 0X0B;
                        linercode = "noread";
                    }
                    else
                    {
                        if (code.Contains("&"))
                        {
                            string[] codelist = code.Split('&');
                            twoDimencode = codelist[1];
                            linercode    = codelist[0];

                            if (twoDimencode.Contains("http"))
                            {
                                code = linercode;
                            }
                            else
                            {
                                // http://ucmp.sf-express.com/service/weixin/activity/wx_b2sf_order?p1=439038437579
                                // 处理异常条码,如: 43100002%MMMMMMMMMMMMMMMMMMMMMMMM={'k1':'574W','k2':'574TF','k3':'017','k4':'T4','k5':'049842774482','k6':''}
                                if (linercode.Length < 12)
                                {
                                    // 一维码长度小于12时,从二维码中取K5值
                                    try
                                    {
                                        string  jsonStr = code.Substring(twoDimencode.IndexOf("M=") + 2);
                                        JObject jo      = (JObject)JsonConvert.DeserializeObject(jsonStr);
                                        linercode = jo["k5"].ToString();
                                        code      = twoDimencode + "&" + linercode + "&*&*&*&*&*&*&";
                                    }
                                    catch (Exception e)
                                    {
                                        // 如果转二维码转json失败,说明二维码也不正确,此时code判定为noread
                                        linercode = "noread";
                                        code      = "noread";
                                    }
                                }
                                else
                                {
                                    // 正常情况下直接拼接一维码和二维码
                                    code = twoDimencode + "&" + linercode + "&*&*&*&*&*&*&";
                                }
                            }
                        }
                        else if (code.Length < 15)
                        {
                            linercode = code;
                            // code = "&" + code;
                            code = code;
                            //PlcFactory.Instance().WritePlcDataDB("ToPlc_seq%", new object[] { seq });
                            //PlcFactory.Instance().WritePlcDataDB("ToPlc_barcode%", new object[] { Data });
                            //if (i == 18)
                            //{
                            //    i = 0;
                            //    PlcFactory.Instance().WritePlcDataDB("ToPlc_bag%", new object[] { 19 });
                            //}
                            //else
                            //{
                            //    i++;
                            //    PlcFactory.Instance().WritePlcDataDB("ToPlc_bag%", new object[] { i });
                            //}
                            //datalog.UpdateDjmx(Data, seq.ToString(), i.ToString());
                            //Log.WriteLog2(DateTime.Now.ToString() + "----------¥¥¥¥¥¥¥¥¥-------" + (i.ToString()));

                            //int sum = 0;
                            //DataTable tb = datalog.SelectSum(i.ToString());
                            //if (tb.Rows.Count > 0)
                            //{
                            //    int test = Convert.ToInt32(tb.Rows[0]["sts"]);
                            //    if (DBNull.Value != tb.Rows[0]["count"])
                            //    {
                            //        int a = Convert.ToInt32(tb.Rows[0]["count"]);
                            //        sum = a + 1;
                            //    }
                            //    else
                            //    {
                            //        sum = 0;
                            //    }
                            //    datalog.UpdateProtStsSum(i.ToString(), sum);
                            //}
                            //BindData();
                        }
                        else
                        {
                            //只有二维码
                            //{'k1':'574W''k2':'574JF''k3':'008''k4':'T4''k5':'049842738896''k6':''}
                            try
                            {
                                string  linecode = code.Substring(code.IndexOf("M=") + 2);
                                JObject jo       = (JObject)JsonConvert.DeserializeObject(linecode);
                                code      = code + "&" + jo["k5"].ToString() + "&";
                                linercode = jo["k5"].ToString();
                            }
                            catch (Exception ex)
                            {
                                code      = "noread";
                                lendata   = 0X0B;
                                linercode = "noread";
                            }
                        }
                        lendata = byte.Parse((code.Length + 5).ToString());
                    }
                    if (code.Trim().Length == 0)
                    {
                        Log.WriteLog("scan error" + linercode);
                        return;
                    }
                    byte[] array = System.Text.Encoding.ASCII.GetBytes(code);
                    foreach (byte letter in array)
                    {
                        wmsSeq.Add(letter);
                    }
                    seqOlcId = seq.ToString();
                    WmsCommon.Instance().ErrWmsSendData(0X83, lendata, wmsSeq, ref seqOlcId);
                    WmsCommon.Instance().setCode(linercode, seqOlcId);
                    WmsCommon.Instance().map[seqOlcId] = linercode;

                    //datalog.InsertTask(linercode);

                    Log.WriteLog("发送给WMS:  " + linercode);
                    createdata.InsertTask(linercode, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }

                #endregion
            }
            catch (Exception ex)
            {
                Log.WriteLog("DataChange事件异常:" + ex.Message.ToString() + ":" + Data + ":");
                return;
            }
        }