Пример #1
0
        //读数据接收回调函数
        public void ReceiveCallback(IAsyncResult iar)
        {
            UdpState udpState = iar.AsyncState as UdpState;

            if (iar.IsCompleted)
            {
                Byte[] receiveBytes = udpState.udpClient.EndReceive(iar, ref udpReceiveState.ipEndPoint);

                if (receiveBytes.Length >= 44) // 读数据返回
                {
                    DataForm mainform = g_em as DataForm;

                    if (bUpdateFirst == true)
                    {
                        mi           = new MyInvoke(UpdateForm);
                        bUpdateFirst = false;
                    }
                    if (mainform.InvokeRequired)
                    {
                        mainform.BeginInvoke(mi, receiveBytes);
                        //mainform.BeginInvoke((MethodInvoker)(() =>
                        //{
                        //    string strData = "";
                        //    DataForm mainForm = g_em as DataForm;
                        //    strData = "";
                        //    for (int n = 0; n < receiveBytes.Length; n++)
                        //        strData = strData + " " + (receiveBytes[n]).ToString("X2");
                        //    mainForm.lstData.Items.Insert(0, strData);
                        //    strData = DateTime.Now.ToString("HH:mm:ss ");
                        //    mainForm.lstData.Items.Insert(0, strData);
                        //    Console.WriteLine(strData);
                        //}));
                    }
                    Console.WriteLine();
                }
                else if (receiveBytes.Length >= 10)             // 写数据返回
                {
                    readWriteType = ReadWriteType.READ_AGV_PLC; ////恢复为读命令
                }
            }
        }