Example #1
0
        private void button6_Click(object sender, EventArgs e)
        {
            index += 1;
            string data   = "101|";
            string temp   = "";
            int    length = 0;

            for (var i = 0; i < tblist.Count; i++)
            {
                if (tblist[i].Text != "")
                {
                    temp += SortingPub.formatData((i + 1).ToString(), 2) + SortingPub.formatData(tblist[i].Text, 3);
                }
            }
            length = temp.Length + 4 + 4 + 8 + 4 + 1 + 1;
            data  += SortingPub.formatData(length.ToString(), 4) + index + textBox61.Text + "1000" + temp + ".";
            sendData(data);
        }
Example #2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="clientSocket"></param>
        private void ReceiveMessage(object clientSocket)
        {
            Socket myClientSocket = (Socket)clientSocket;

            while (true)
            {
                try
                {
                    checkSocket();
                    //通过clientSocket接收数据
                    int receiveNumber = myClientSocket.Receive(result);
                    Console.WriteLine("接收plc{0}消息{1}", myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
                    String data       = Encoding.ASCII.GetString(result, 0, receiveNumber);
                    String dataHeader = "";
                    String dataEnd    = "";

                    String datatemp = "";
                    if (data != null && data.Length > 3)
                    {
                        dataHeader = data.Substring(0, 3);
                        dataEnd    = data.Substring(4);
                    }
                    if (dataHeader.Equals("103"))//检查plc是否准备就绪
                    {
                        plcConnIsOk = false;
                        if (dataEnd.Equals("1"))
                        {
                            plcConnIsOk = true;
                        }
                    }
                    if (dataHeader.Equals("001"))
                    {
                        startMachine = false;
                        if (dataEnd.Equals("1"))//成功接收拨烟数据
                        {
                            startMachine = true;
                        }
                    }
                    if (dataHeader.Equals("101"))
                    {
                        plcRetSorting = "0";
                        if (dataEnd.Equals("1"))//成功接收拨烟数据
                        {
                            plcRetSorting = "1";
                        }
                    }
                    if (dataHeader.Equals("102"))//任务完成信号
                    {
                        string[] dataarr = data.Split('|');
                        for (int i = 1; i < dataarr.Length; i++)
                        {
                            //若非重复数据
                            if (data != datatemp)
                            {
                                dataEnd = dataarr[i].Substring(3);
                                taskNum = taskNum + 1;
                                string tnum = SortingPub.unformatData(dataEnd);
                                Console.WriteLine("任务号:" + tnum);
                                //updateListBox("任务:" + tnum + "  分拣完成");
                                sFun.updateTaskState(tnum);//更新该任务对应的各表标志
                                // sFun.getAllocateCigarDataFromDB();
                                string taskinfo = sFun.getTaskinfo(tnum);

                                Console.WriteLine("taskinfo:" + taskinfo);

                                this.BeginInvoke(new UpdateDataGridView(updateTaskInfo), new Object[] { taskinfo });
                                // updateTaskInfo(taskinfo);
                            }
                            updateLabel("正在分拣,请耐心等候...", label3);
                            datatemp = data;
                        }
                        // sendCompletedFeed();
                        myClientSocket.Send(Encoding.ASCII.GetBytes("1021"));
                    }
                    if (dataHeader.Equals("300"))//分拣故障
                    {
                        // myClientSocket.Send(Encoding.ASCII.GetBytes("2"));
                        plcHasError = true;
                        Console.WriteLine("分拣故障,故障编号:" + dataEnd);
                        SortingFun.WriteErrorLog(dataEnd);
                        sendErrorFeed(dataHeader);
                    }

                    if (dataHeader.Equals("302"))//分拣故障恢复
                    {
                        // myClientSocket.Send(Encoding.ASCII.GetBytes("2"));
                        plcHasError = false;
                        Console.WriteLine("故障恢复,故障编号:" + dataEnd);
                        //myClientSocket.Send(Encoding.ASCII.GetBytes("1"));
                        sendErrorFeed(dataHeader);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    updateListBox(ex.Message);
                    myClientSocket.Shutdown(SocketShutdown.Both);
                    myClientSocket.Close();
                    break;
                }
            }
        }