예제 #1
0
        public Form1()
        {
            InitializeComponent();

            btnConnect.Click += (o, e) =>
            {
                txbData.Text   = string.Format("Connect to: {0}:{1}", txbAddress.Text, nmPort.Value);
                soc            = new CClientSocket(txbAddress.Text, (int)nmPort.Value);
                soc.OnConnect += (_soc) =>
                {
                    Invoke((MethodInvoker) delegate
                    {
                        txbData.AppendText(string.Format("Socket {0} is connected", _soc.RemoteEndPoint.ToString()));
                        //btnConnect.Text = "Disconnect";
                    });
                };
                soc.OnRead += (_soc) =>
                {
                    Invoke((MethodInvoker) delegate
                    {
                        txbData.AppendText(string.Format("Receive {0}:: Data: {1}", _soc.ToString(), soc.ReceivedText));
                    });
                };
                soc.OnDisconnect += (_soc) =>
                {
                    Invoke((MethodInvoker) delegate
                    {
                        txbData.AppendText(string.Format("Disconnect {0}", _soc.ToString()));
                        //btnConnect.Text = "Connect";
                    });
                };
                soc.Connect();
            };

            btnSend.Click += (o, e) =>
            {
                soc.SendText(txbSend.Text);
            };
        }
예제 #2
0
        public Form1()
        {
            InitializeComponent();

            zedview = new ZedGraphView(ref zedGraphControl1); //init
            zedview.Init();

            //Events ----
            btnConnect.Click += (o, e) =>
            {
                txbData.Text   = string.Format("Connect to: {0}:{1}", txbAddress.Text, nmPort.Value);
                soc            = new CClientSocket(txbAddress.Text, (int)nmPort.Value);
                soc.OnConnect += (_soc) =>
                {
                    Invoke((MethodInvoker) delegate
                    {
                        txbData.AppendText(string.Format("Socket {0} is connected", _soc.RemoteEndPoint.ToString()));
                        //btnConnect.Text = "Disconnect";
                        timer1.Enabled = true;
                    });
                };
                //soc.OnRead += (_soc) =>
                //{
                //    Invoke((MethodInvoker)delegate
                //    {
                //        //Format receive: $$,11.1444,6.2323,13.4445,$$  (Hercules - TCP Server)
                //        string strdata = soc.ReceivedText;
                //        //txbData.AppendText(string.Format("Receive {0}:: Data: {1}", _soc.ToString(), strdata));
                //        txbData.AppendText(strdata + "  \r\n");
                //        string[] subdata = strdata.Split(',');
                //        //
                //        if (subdata[0] == "$")
                //        {
                //            if (subdata.Length >= 3)
                //            {
                //                if (subdata[1] != null)
                //                {
                //                    double.TryParse(subdata[1], out volt); //Volt 1
                //                    double.TryParse(subdata[2], out volt1); //Volt 2
                //                    double.TryParse(subdata[3], out volt2); //Volt 3
                //                    zedview.Draw(volt, volt1, volt2);
                //                }
                //            }


                //        }
                //    });
                //};
                soc.OnDisconnect += (_soc) =>
                {
                    Invoke((MethodInvoker) delegate
                    {
                        txbData.AppendText(string.Format("Disconnect {0}", _soc.ToString()));
                        //btnConnect.Text = "Connect";
                    });
                };
                soc.Connect();
            };

            btnDisconnect.Click += (o, e) =>
            {
                soc.Disconnect();
            };
            //
            btnSend.Click += (o, e) =>
            {
                soc.SendText(txbSend.Text);
            };
            //
            btnClear.Click += (o, e) => zedview.Init();
        }