private void GenerateImages()
        {
            if (PortData == null)
            {
                DataCollection = new SerialPortDataViewModel[0];
                return;
            }

            var dataCollection = new List <SerialPortDataViewModel>();

            foreach (var data in PortData.Take(CountOfNumbers))
            {
                var imageData = _imageDataCollection.SingleOrDefault(x => x.Number == data);

                if (imageData == null)
                {
                    continue;
                }

                var item = new SerialPortDataViewModel();

                if (File.Exists(imageData.Filepath))
                {
                    item.Init(imageData.Number, imageData.Filepath);
                }
                else
                {
                    item.Init(imageData.Number);
                }

                dataCollection.Add(item);
            }

            DataCollection = dataCollection.ToArray();
        }
        private void ClickToItemCommandHandler(object parameter)
        {
            Task.Run(() =>
            {
                if (answer_sended == true)
                {
                    return;
                }

                var item      = (SerialPortDataViewModel)parameter;
                var itemArray = new SerialPortDataViewModel[1];
                itemArray[0]  = item;

                DataCollection = itemArray;


                //SerialPort port = null;

                try
                {
                    byte[] a = new byte[2];
                    a[0]     = Convert.ToByte(item.Number / 128);
                    a[1]     = Convert.ToByte(item.Number - 128 * a[0]);
                    port.Write(a, 0, 2);
                    answer_sended = true;
                }
                catch { }
                finally
                {
                    //port?.Close();
                }
            });
        }
        private void ProcessPortData(string data)
        {
            generationCounter++;


            var values = data.Replace("\\r", "").Replace("\\n", "")
                         .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                         .Select(byte.Parse)
                         .ToArray();

            if (values.Length == 0)
            {
                PortData = null;
                return;
            }

            if (values.Length == 0 && values[0] == 0)
            {
                PortData = null;
                return;
            }

            message_type = values[0];
            List <int> temp_answers = new List <int>();

            if (message_type == 1 || message_type == 2)
            {
                answer_sended     = false;
                SelectedImageData = null;

                Weight = values[1] * 128 + values[2];

                var portData = new List <int>();
                for (int i = 3, j = 0; i < values.Length - 1; i += 2, j++)
                {
                    var item1 = values[i];
                    var item2 = values[i + 1];

                    portData.Add(item1 * 128 + item2);
                    temp_answers.Add(item1 * 128 + item2);
                }

                answers = temp_answers.ToArray();

                PortData = portData.ToArray();
            }
            else if (message_type == 3)
            {
                if (answers.Length > 0)
                {
                    var imageData = _imageDataCollection.SingleOrDefault(x => x.Number == answers[0]);

                    var item = new SerialPortDataViewModel();

                    if (File.Exists(imageData.Filepath))
                    {
                        item.Init(imageData.Number, imageData.Filepath);
                    }
                    else
                    {
                        item.Init(imageData.Number);
                    }

                    var itemArray = new SerialPortDataViewModel[1];
                    itemArray[0] = item;

                    DataCollection = itemArray;
                }
            }
        }