Exemple #1
0
 protected virtual void OnReceived(TCPEventArgs e)
 {
     if (MessageReceived != null)
     {
         MessageReceived(this, e);
     }
 }
Exemple #2
0
        private void Polling()
        {
            while (mark)
            {
                try
                {
                    string data = Recieve();
                    if (data != "")
                    {
                        TCPEventArgs args = new TCPEventArgs(data, client);

                        if (MessageReceived != null)
                        {
                            MessageReceived(this, args);
                        }
                        //Messages.WriteLine(data);
                    }
                    Thread.Sleep(20);
                    if (isconnect != null)
                    {
                        isconnect(IsConnected());
                    }
                }
                catch (Exception ex)
                {
                    //Log.WriteLog("网络循环出现错误,信息为" + ex.Message);
                }
            }
        }
Exemple #3
0
        public void AnalyzePLCMessage(Object sender, TCPEventArgs e)
        {
            Debug.WriteLine("receivedMessage:   " + e.receivedmessage);
            string recStr = e.receivedmessage.Replace(" ", "");

            string[] recStrArr = recStr.Split(' ');
            Messages.WriteLine("接收到PLC传送的图片文件名:" + recStr);
            if (recStr.Contains("."))
            {
                Messages.WriteLine("准备从文件服务器读取文件");
                GetImage(recStr);
                Messages.WriteLine("图片读取完成,启动打印机");
                PrintImage();
                Messages.WriteLine("反馈信号给PLC");
                Thread.Sleep(2000);
                PrinterTcp.Send("READY");
                Messages.WriteLine("打印任务发送完成");
                Debug.WriteLine("Print Complete");
            }
            Debug.WriteLine(recStr);
        }
Exemple #4
0
        /// <summary>
        /// 数据接收委托函数
        /// </summary>
        /// <param name="result">传入参数</param>
        protected void DataRec(IAsyncResult result)
        {
            if (IsOnline())
            {
                int         length = networkstream.EndRead(result); //获取接收数据的长度
                List <byte> data   = new List <byte>();             //新建byte数组
                data.AddRange((byte[])result.AsyncState);           //获取数据
                data.RemoveRange(length, data.Count - length);      //根据长度移除无效的数据
                byte[] data2 = new byte[1000];                      //重新定义接收缓冲

                bool c = tcpclient.Connected;
                networkstream.BeginRead(data2, 0, 1000, new AsyncCallback(DataRec), data2);  //重新挂起数据的接收等待
                //自定义代码区域,处理数据data
                //for (int i = 0; i < data.Count;i++ )
                //{
                //    Debug.WriteLine(data[i]);
                //}
                Encoding receiveEncoding_ = Encoding.UTF8;
                string   receiveStr       = receiveEncoding_.GetString((byte[])result.AsyncState, 0, length);
                receivedMessage = receiveStr;
                receivedMessageCount++;
                //  Debug.WriteLine(receiveStr);

                TCPEventArgs e = new TCPEventArgs(receiveStr);
                OnReceived(e);

                if (length == 0)
                {
                    //连接已经关闭
                    Debug.WriteLine("connection closed by server----------------");
                }
            }
            else
            {
                ReConnect();
            }
        }