Exemplo n.º 1
0
        private void Setup()
        {
            try
            {
                _client = new TcpClient(_server, _port);
                _stream = _client.GetStream();

                _reader = new BinaryReader(_stream);
                _writer = new BinaryWriter(_stream);

                int result = _reader.ReadByte();
                if (result == MessageDictionary.Hello)
                {
                    _writer.Write(MessageDictionary.Hello);
                    _writer.Write(MessageDictionary.ImUploadClient);
                    _writer.Write(_userId);

                    if (_reader.Read() == MessageDictionary.OK)
                    {
                        _isConnected = true;

                        ConnectionSuccess?.Invoke(this, EventArgs.Empty);
                    }
                }
                else
                {
                    _isConnected = false;
                    ConnectionFailed?.Invoke(this, EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                ConnectionFailed?.Invoke(this, EventArgs.Empty);
            }
        }
        /// <summary>
        /// Connect to Arduino Leonardo Serial Port
        /// </summary>
        /// <returns></returns>
        public async Task ConnectAsync()
        {
            //
            if (!IsConnected)
            {
                // Get all com serial names
                var portNames = SerialPort.GetPortNames().ToList();

                SerialPort serialPort;

                // Loop all com port names
                foreach (var name in portNames.OrderByDescending(i => portNames.IndexOf(i)))
                {
                    // Target new comport by comport name
                    serialPort = new SerialPort(name, portRange, Parity.None);

                    try
                    {
                        // Set the RTS to enable
                        serialPort.RtsEnable = true;
                        // Set the data resive event
                        serialPort.DataReceived += ResiceData_Event;
                        // Open the serialport
                        serialPort.Open();
                        // Send '1' to arduino to get a response for to check for is the rigth arduino devcice
                        SendData(ref serialPort, "1");
                        // Wait 2sek for response data
                        await Task.Delay(2000);

                        // If data has '1' the connection istablish
                        if (data.Equals("1"))
                        {
                            // Set the connection is connectet to true
                            IsConnected = true;
                            // Set the current serial port
                            currentSerialPort = serialPort;
                            // Start sender to to send stream of '1' if in the rigth state
                            StartSender();
                            // Call connection success event if et connectet
                            ConnectionSuccess?.Invoke(this, new EventArgs());
                        }
                        else
                        {
                            // Call connection fail event if et not connectet
                            ConnectionFail?.Invoke(this, new EventArgs());
                        }
                    }

                    finally
                    {
                        // If port not are connected to rigth Arduino close serila connection if is has one
                        if (currentSerialPort != null && currentSerialPort.IsOpen && !IsConnected)
                        {
                            currentSerialPort.Close();
                        }
                    }
                    // If its connected break out of the loop
                    if (IsConnected)
                    {
                        break;
                    }
                }
            }
        }