Esempio n. 1
0
        /// <summary>
        /// Метод по считыванию слов
        /// </summary>
        /// <param name="slot">Слот, хранящий данные</param>
        /// <returns></returns>
        public static ushort[] ReadWords(Slot slot)
        {
            lock (typeof(DataTransfer))
            {
                bool            res     = false;
                List <byte[]>   querys  = new List <byte[]>();
                List <byte[]>   answers = new List <byte[]>();
                List <ushort[]> values  = new List <ushort[]>();

                try
                {
                    if (_connector.Connect())
                    {
                        slot.Loaded = false;

                        if (slot.Size >= 120)
                        {
                            PrepareListReadWords(ref querys, ref answers, slot);
                        }
                        else
                        {
                            byte[] query = Modbus.CreateReadWordsArray(_connector.KNNumber, _connector.DeviceNumber, slot.Start, slot.Size);
                            querys.Add(query);
                            byte[] answer = _connector.ReadWrite(query);
                            if (answer[2] != /*5 +*/ slot.Size * 2)
                            {
                                throw new Exception("Считано неверное количество байт");
                            }
                            answers.Add(answer);
                        }

                        for (int i = 0; i < answers.Count; i++)
                        {
                            List <byte> mbVals = new List <byte>(answers[i][2]);
                            mbVals = answers[i].ToList().Skip(3).Take(answers[i][2]).ToList();
                            ushort[] value = new ushort[(mbVals.Count) / 2];
                            for (int j = 0; j < mbVals.Count; j += 2)
                            {
                                value[(j) / 2] = Common.TOWORD(mbVals[j], mbVals[j + 1]);
                            }
                            values.Add(value);
                        }

                        ushort[] result        = new ushort[slot.Size];
                        int      lastByteIndex = 0;
                        for (int i = 0; i < values.Count; i++)
                        {
                            Array.ConstrainedCopy(values[i], 0, result, lastByteIndex, values[i].Length);
                            lastByteIndex += values[i].Length;
                        }

                        slot.Value  = result;
                        slot.Loaded = true;
                        res         = true;
                    }
                }
                catch (Exception ex)
                {
                    res = false;
                    if (slot != null)
                    {
                        slot.Loaded = false;
                    }
                }
                finally
                {
                    //if (_connector.Disconnect() || !res)
                    //{
                    //    res = false;
                    //}
                }
                return(res ? slot.Value : null);
            }
        }
Esempio n. 2
0
        private static void PrepareListReadWords(ref List <byte[]> querys, ref List <byte[]> answers, Slot slot)
        {
            lock (typeof(DataTransfer))
            {
                int    temp        = 0;
                ushort oneSlotSize = 0x64;

                int    queryCount    = slot.Size / oneSlotSize;
                ushort lastQuerySize = (ushort)(slot.Size % oneSlotSize);
                ushort addressTmp    = slot.Start;

                if (lastQuerySize != 0)
                {
                    temp = 1;
                }

                for (int i = 0; i < queryCount + temp; i++)
                {
                    if (temp == 0)
                    {
                        byte[] query = Modbus.CreateReadWordsArray(_connector.KNNumber, _connector.DeviceNumber,
                                                                   addressTmp, oneSlotSize);
                        querys.Add(query);
                        addressTmp += oneSlotSize;
                    }
                    else
                    {
                        if (i == queryCount + temp - 2)
                        {
                            byte[] query = Modbus.CreateReadWordsArray(_connector.KNNumber, _connector.DeviceNumber,
                                                                       addressTmp, oneSlotSize);
                            querys.Add(query);
                            addressTmp += oneSlotSize;
                        }
                        else if (i == queryCount + temp - 1)
                        {
                            byte[] query = Modbus.CreateReadWordsArray(_connector.KNNumber, _connector.DeviceNumber,
                                                                       addressTmp, lastQuerySize);
                            querys.Add(query);
                            addressTmp += oneSlotSize;
                        }
                        else
                        {
                            byte[] query = Modbus.CreateReadWordsArray(_connector.KNNumber, _connector.DeviceNumber,
                                                                       addressTmp, oneSlotSize);
                            querys.Add(query);
                            addressTmp += oneSlotSize;
                        }
                    }
                }
                for (int i = 0; i < querys.Count; i++)
                {
                    if (temp == 0)
                    {
                        byte[] answer = _connector.ReadWrite(querys[i]);
                        if (answer[2] != 5 + oneSlotSize * 2)
                        {
                            throw new Exception("Считано неверное количество байт");
                        }
                        answers.Add(answer);
                    }
                    else
                    {
                        if (i != queryCount + temp - 1)
                        {
                            byte[] answer = _connector.ReadWrite(querys[i]);
                            if (answer[2] != oneSlotSize * 2)
                            {
                                throw new Exception("Считано неверное количество байт");
                            }
                            answers.Add(answer);
                        }
                        else
                        {
                            byte[] answer = _connector.ReadWrite(querys[i]);
                            if (answer[2] != lastQuerySize * 2)
                            {
                                throw new Exception("Считано неверное количество байт");
                            }
                            answers.Add(answer);
                        }
                    }
                }
            }
        }