Example #1
0
        private void Button_connect_Click(object sender, EventArgs e)
        {
            if (comboBox_portname1.SelectedIndex == 0)
            {
                try
                {
                    _clientSocket = new TcpClient();
                    _clientSocket.Connect(_ipAddress, _ipPort);
                    _clientSocket.ReceiveTimeout        = 1000;
                    _clientSocket.SendTimeout           = 1000;
                    _clientSocket.Client.ReceiveTimeout = 1000;
                    _clientSocket.Client.SendTimeout    = 1000;
                    _serverStream = _clientSocket.GetStream();
                }
                catch (Exception ex)
                {
                    _logger.AddText("Connect error to: " + _ipAddress + ":" + _ipPort + ex + Environment.NewLine,
                                    (byte)DataDirection.Error, DateTime.Now);
                    if (_clientSocket.Client.Connected)
                    {
                        _clientSocket.Client.Disconnect(false);
                    }

                    //serverStream.Close();
                    if (_clientSocket.Connected)
                    {
                        _clientSocket.Close();
                    }

                    _clientSocket = new TcpClient();
                    return;
                }

                _logger.AddText("Device connected to: " + _ipAddress + ":" + _ipPort + Environment.NewLine,
                                (byte)DataDirection.Note, DateTime.Now);
                button_receive.Enabled = true;
                timer1.Enabled         = true;
            }
            else
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }

                serialPort1.PortName       = comboBox_portname1.Text;
                serialPort1.BaudRate       = _comSpeed;
                serialPort1.DataBits       = 8;
                serialPort1.Handshake      = Handshake.None;
                serialPort1.Parity         = Parity.None;
                serialPort1.StopBits       = StopBits.One;
                serialPort1.ReadTimeout    = 500;
                serialPort1.WriteTimeout   = 500;
                serialPort1.ReadBufferSize = 8192;
                try
                {
                    serialPort1.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error opening netPort " + serialPort1.PortName + ": " + ex.Message);
                    return;
                }

                _logger.AddText("Port opened\r\n", (byte)DataDirection.Note, DateTime.Now);
            }

            if (!serialPort1.IsOpen && !IsClientConnected())
            {
                Button_disconnect_Click(this, EventArgs.Empty);
                return;
            }

            button_connect.Enabled      = false;
            button_disconnect.Enabled   = true;
            button_send.Enabled         = true;
            button_sendAll.Enabled      = true;
            button_sendCommand.Enabled  = true;
            button_getConfig.Enabled    = true;
            button_getStatus.Enabled    = true;
            button_getSensor.Enabled    = true;
            button_setTime.Enabled      = true;
            comboBox_portname1.Enabled  = false;
            comboBox_portspeed1.Enabled = false;
        }