/// <summary> ///格口状态改变发送 WCS->WMS /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void MouthState() { try { //查询格口号状态为 1或0的格口 DataTable tbProt = createdata.GetProtSend(); if (tbProt != null) { if (tbProt.Rows.Count > 0) { string seqId = string.Empty; List <byte> wmsSeq = new List <byte>(); foreach (DataRow row in tbProt.Rows) { wmsSeq.Clear(); //清空byte集合 byte stsPort = 0X01; //锁定 if (row["sts"].ToString() == "0") { stsPort = 0X02;///解锁 } short prot = short.Parse(row["prot"].ToString()); byte[] array = BitConverter.GetBytes(prot); //以字节数组的形式返回返回指定的 16 位有符号整数值 wmsSeq.Add(array[1]); wmsSeq.Add(array[0]); wmsSeq.Add(stsPort); bool sendSts = WmsCommon.Instance().ErrWmsSendData(0X86, 0X08, wmsSeq, ref seqId); //bool 布尔类型 if (sendSts) { createdata.UpDataWcsWms(prot.ToString()); if (stsPort == 0x01) { Log.WriteLog2(prot + "发送格口状态成功: 锁定"); } else { Log.WriteLog2(prot + "发送格口状态成功: 解锁"); } tb_message.AppendText("格口 " + prot + " 发送状态成功\r\n"); } //发送给WMS 要求WMS打印 if (stsPort == 0X01) { wmsSeq.Clear(); array = BitConverter.GetBytes(1); wmsSeq.Add(array[1]); wmsSeq.Add(array[0]); WmsCommon.Instance().ErrWmsSendData(0X85, 0X07, wmsSeq, ref seqId); } } } } } catch (Exception ex) { Log.WriteLog("发送WMS格口状态 ex" + ex.Message); } }
/// <summary> /// Read a ChatSocket from the network stream asynchronously. /// </summary> public void Receive() { if (socket == null) { return; } try { if (socket.Connected) { // stores the received data // byte[] result = new byte[1024]; // int receiveNumber = socket.Receive(result); // Metatype = Encoding.ASCII.GetString(result, 0, receiveNumber); byte[] buffer = new byte[4]; socket.BeginReceive( buffer, 0, 4, SocketFlags.None, new AsyncCallback(InternalReceive), buffer); } else { WmsCommon.Instance().CloseSocket(this); } } catch (Exception exc) { WmsCommon.Instance().CloseSocket(this); if (SockUtils.HandleSocketError(exc)) { if (Disconnected != null) { Disconnected(this, EventArgs.Empty); } } //else //{ // throw exc; //} } }
/// <summary> /// 监控WCS跟WMS连接状态 /// </summary> public void ConnectState() { iptb.Rows.Clear(); //清空表数据 try { for (int i = 0; i < WmsCommon.Instance().clients.Count; i++) { if (i < WmsCommon.Instance().clients.Count) { SocketStatus wmssocket = WmsCommon.Instance().clients[i]; if (wmssocket != null) { if (wmssocket.scocKet.Connected) { i++; DataRow r = iptb.NewRow(); r["ip"] = wmssocket.Ip; r["prot"] = wmssocket.Port; iptb.Rows.Add(r); } else { wmssocket.scocKet.Close(); WmsCommon.Instance().clients.Remove(wmssocket); } } } } if (iptb != null) { gridControl1.DataSource = iptb; } } catch (Exception ex) { tb_message.AppendText("连接异常" + ex.Message + "\r\n"); Log.WriteLog2("连接异常" + ex.Message); return; } }
private void FrmMain_Load(object sender, EventArgs e) //事件 的 委托 { if (WmsCommon.Instance().WcsServerOpen()) { tb_message.Text = "端口号为 " + RfConfig.Create().WcsPort + "的服务已经开启!\r\n"; timer1.Start(); sts_wms.StateIndex = 3; sts_plc.StateIndex = 3; } else { sts_wms.StateIndex = 1; sts_plc.StateIndex = 1; timer1.Start(); } BindData(); selectold(); Thread tr = new Thread(new ThreadStart(ListenData)); tr.Start(); Thread t1 = new Thread(new ThreadStart(BagCount)); t1.Start(); }
/// <summary> /// Write out this ChatSocket object to the networkStream asynchronously. /// </summary> public void Send() { if (socket == null) { return; } try { byte[] buffer = new byte[4]; buffer = BitConverter.GetBytes((int)command); if (socket.Connected) { socket.Send(Encoding.ASCII.GetBytes(metatype.ToString())); if (WmsCommon.Instance().clients.Count(r => r.scocKet == socket) > 0) { WmsCommon.Instance().clients.Find(r => r.scocKet == socket).statusSocket = "1"; } } } catch (Exception exc) { WmsCommon.Instance().CloseSocket(this); if (SockUtils.HandleSocketError(exc)) { if (Disconnected != null) { Disconnected(this, EventArgs.Empty); } } //else //{ // throw; //} } }
/// <summary> /// 实际落袋发送 WCS->WMS /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void PocketSend() { try { DataTable tb = createdata.GetPlcSednCode(); if (tb != null) { if (tb.Rows.Count > 0) { foreach (DataRow row in tb.Rows) { string seqId = string.Empty; List <byte> wmsSeq = new List <byte>(); string code = row["ExpressNo"].ToString(); if (code.Trim().Length == 0) { continue; } // 如果code为noread,以12个0作为单号发送 if ("noread\r" == code) { code = "000000000000"; } byte[] arrayL = System.Text.Encoding.ASCII.GetBytes(code); if (row["endprot"].ToString().Trim().Length == 0) { return; } short endprot = short.Parse(row["endprot"].ToString()); byte[] array; //// 如果数据库查到的落袋格口为14,实际是因为格口锁定而落入13格口 //if (endprot == 14) //{ // short readport = 13; // array = BitConverter.GetBytes(readport); //} //else //{ array = BitConverter.GetBytes(endprot); //数据域 格口 2字节 //} wmsSeq.Add(array[1]); wmsSeq.Add(array[0]); wmsSeq.Add(byte.Parse(code.Length.ToString())); foreach (char letter in arrayL) { byte value = Convert.ToByte(letter); wmsSeq.Add(value); //数据域 运单号 动态长度 } short prot = short.Parse(row["prot"].ToString()); if (prot == 19) { wmsSeq.Add(0XF0); } else { if (endprot == 19) { wmsSeq.Add(0XF0); } // 如果因为满袋而未分拣,plc会返回落袋格口99 else if (endprot == 99) { wmsSeq.Add(0x02); } else { wmsSeq.Add(0X00); //数据域 异常码 1字节 } } wmsSeq.Add(0X02); //数据域 TAG 1字节 byte lenData = byte.Parse((5 + wmsSeq.Count).ToString()); bool sendSts = WmsCommon.Instance().ErrWmsSendData(0X84, lenData, wmsSeq, ref seqId); if (sendSts) { string plcId = row["plcid"].ToString(); createdata.UpDataWmsPlcData(plcId); createdata.UpDataWmsseqIdData(seqId, plcId); Log.WriteLog2(DateTime.Now + " 发送落袋信息成功。 条码:" + code + " 实际格口:" + plcId); tb_message.AppendText(DateTime.Now.ToString() + code + " 落袋发送消息成功\r\n"); } } } } } catch (Exception ex) { Log.WriteLog2("发送WMS落袋信息 ex" + ex.Message); } }
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; } }
/// <summary> /// End asynchronous receive of command from the network, /// and continues to read the rest of the packet. /// </summary> private void InternalReceive(IAsyncResult ar) { try { if (socket == null) { return; } if (socket.Connected) { int received = socket.EndReceive(ar); if (received > 0) { byte[] buffer = (byte[])ar.AsyncState; Metatype = Encoding.ASCII.GetString(buffer, 0, received); byte[] result = new byte[1024]; int receiveNumber = socket.Receive(result); Metatype = Metatype + Encoding.ASCII.GetString(result, 0, receiveNumber); //receivedBytes += (ulong)received; //byte[] result = new byte[1024]; //result = (byte[])ar.AsyncState; //int bufferVal = BitConverter.ToInt32(buffer, 0); //Encoding.ASCII.GetString(result, 0, receiveNumber); // check if it is a know command command = (ChatCommand)7; //command = (ChatCommand)bufferVal; //Trace.Write(string.Format("ChatSocket RECEIVE from {1} -> {0} {2}", // socket.LocalEndPoint.ToString(), socket.RemoteEndPoint.ToString(), command.ToString())); //// InternalReceiveCommandTarget(); // InternalReceiveMetadataType(); // InternalReceiveMetaBuffer(); // fire the Received event only if all the Receive routines // completed succesfully if (Received != null) { Received(this, EventArgs.Empty); } } //else //{ // // it is an unknown command so close the socket // Disconnect(); //} } else // received == 0 { Disconnect(); } } catch (Exception exc) { WmsCommon.Instance().CloseSocket(this); if (SockUtils.HandleSocketError(exc)) { if (Disconnected != null) { Disconnected(this, EventArgs.Empty); } } else { // throw exc; } } }