Ejemplo n.º 1
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            Socket svrSocket;

            if (buttonStart.Text == "Start")
            {
                m_com          = new AdamCom(2);
                m_com.Checksum = false;                                                               // set the checksum disbale, if the checksum must be applied, set it to true
                m_com.SetComPortState(Baudrate.Baud_9600, Databits.Eight, Parity.None, Stopbits.One); // 9600, N, 8, 1
                m_com.SetComPortTimeout(m_SerialTimeout, m_SerialTimeout, 0, m_SerialTimeout, 0);
                if (!m_com.OpenComPort())
                {
                    MessageBox.Show("Failed to open COM!", "Error");
                    return;
                }
                m_SvrSocket = new UDPSocketServer();
                m_SvrSocket.Create(m_ListenPort);
                svrSocket              = m_SvrSocket.ServerSocket();
                m_SvrStream            = new SocketStream(ref svrSocket, m_UDPSendTimeout, m_UDPRecvTimeout);
                buttonStart.Text       = "Stop";
                m_UDPRecv              = 0;
                m_UDPSent              = 0;
                m_SerialSent           = 0;
                m_SerialRecv           = 0;
                textBoxUDPRecv.Text    = m_UDPRecv.ToString();
                textBoxUDPSent.Text    = m_UDPSent.ToString();
                textBoxSerialSent.Text = m_SerialSent.ToString();
                textBoxSerialRecv.Text = m_SerialRecv.ToString();
                timer1.Enabled         = true;
            }
            else
            {
                timer1.Enabled = false;
                m_SvrSocket.Terminate();
                m_com.CloseComPort();
                buttonStart.Text = "Start";
            }
        }
Ejemplo n.º 2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            string[] szIP;
            int      iPort = 5168; // data streaming UDP listen port

            System.Net.Sockets.Socket sk;

            if (btnStart.Text == "Start")
            {
                try
                {
                    udpServer = new UDPSocketServer();
                    udpServer.Create(iPort);
                    sk        = udpServer.ServerSocket();
                    udpStream = new SocketStream(ref sk, 2000, 2000);
                    Advantech.Adam.AdamSocket.GetLocalNetwork(out szIP);
                    btnStart.Text = "Stop";
                    AddListBoxItem("Local IP = " + szIP[0]);
                    AddListBoxItem("Start listening... ");

                    this.m_bStreamThreadStop      = false;
                    this.m_bStreamThreadTerminate = false;
                    m_StreamThread = new System.Threading.Thread(new ThreadStart(this.ThreadProc));
                    m_StreamThread.Start();
                }
                catch
                {
                    if (m_StreamThread != null)
                    {
                        this.m_bStreamThreadStop = true;
                        int iCnt = 0;
                        while (!m_bStreamThreadTerminate)
                        {
                            Thread.Sleep(10);
                            ++iCnt;
                            if (iCnt > 50)
                            {
                                break;
                            }
                        }
                        m_StreamThread = null;
                    }

                    udpServer.Terminate();
                    udpServer     = null;
                    udpStream     = null;
                    btnStart.Text = "Start";
                    MessageBox.Show("Failed to start data streaming process!", "Error");
                }
            }
            else
            {
                try
                {
                    if (m_StreamThread != null)
                    {
                        this.m_bStreamThreadStop = true;
                        int iCnt = 0;
                        while (!m_bStreamThreadTerminate)
                        {
                            Thread.Sleep(10);
                            ++iCnt;
                            if (iCnt > 50)
                            {
                                break;
                            }
                        }
                        m_StreamThread = null;
                    }

                    udpServer.Terminate();
                    udpServer     = null;
                    udpStream     = null;
                    btnStart.Text = "Start";
                }
                catch (ObjectDisposedException)
                {
                }
            }
        }