Пример #1
0
        public static bool Check(byte[] buffer)
        {
            bool success = false;
            byte lrsSet  = LRC.Lrc(buffer, buffer.Length - 1);

            if (lrsSet == buffer[buffer.Length - 1])
            {
                success = true;
            }

            return(success);
        }
Пример #2
0
        public byte[] MakeBaseRequest0X48(UInt16 u16AddressForReading, UInt16 u16CountOfRegistersForReading, UInt16 u16AddressForWriting, UInt16 u16CountOfRegistersForWrit, UInt16 u16CountOfBytesForWrit, UInt16 u16RequestNumber, byte[] registersForWrit = null)
        {
            List <byte> bytes = new List <byte>();

            bytes.Add(NetworkAddress);

            bytes.Add(0x48);

            bytes.AddRange(BitConverter.GetBytes(u16AddressForReading).Reverse());                    //Нач-ый адрес для чтения

            bytes.AddRange(BitConverter.GetBytes(u16CountOfRegistersForReading).Reverse());           //Количество регистров для чтения

            bytes.AddRange(BitConverter.GetBytes(u16AddressForWriting).Reverse());                    //Нач-ый адрес для записи

            bytes.AddRange(BitConverter.GetBytes(u16CountOfRegistersForWrit).Reverse());              //Количество регистров для записи

            bytes.AddRange(BitConverter.GetBytes(u16CountOfBytesForWrit).Reverse());                  //Кол-во байт для записи

            bytes.AddRange(BitConverter.GetBytes(u16RequestNumber).Reverse());                        //Номер запроса

            if (u16CountOfRegistersForWrit != 0)
            {
                bytes.AddRange(registersForWrit);                   //1,2,..,N-ый регистр для записи
            }

            byte lrc = LRC.Lrc(bytes.ToArray(), bytes.Count);

            bytes.Add(lrc);

            List <byte> sendBytes = new List <byte>();

            sendBytes.AddRange(Encoding.ASCII.GetBytes(string.Format(":{0}", string.Join("", bytes.Select(b => b.ToString("X2"))))));

            sendBytes.Add(0x0D); sendBytes.Add(0x0A); //перевод строки

            return(sendBytes.ToArray());
        }
Пример #3
0
        public byte[] MakeBaseRequest0X03(UInt16 u16AddressForReading, UInt16 u16CountOfRegistersForReading)
        {
            List <byte> bytes = new List <byte>();

            bytes.Add(NetworkAddress);

            bytes.Add(0x03);

            bytes.AddRange(BitConverter.GetBytes(u16AddressForReading).Reverse());                    //Нач-ый адрес для чтения

            bytes.AddRange(BitConverter.GetBytes(u16CountOfRegistersForReading).Reverse());           //Количество регистров для чтения

            byte lrc = LRC.Lrc(bytes.ToArray(), bytes.Count);

            bytes.Add(lrc);

            List <byte> sendBytes = new List <byte>();

            sendBytes.AddRange(Encoding.ASCII.GetBytes(string.Format(":{0}", string.Join("", bytes.Select(b => b.ToString("X2"))))));

            sendBytes.Add(0x0D); sendBytes.Add(0x0A); //перевод строки

            return(sendBytes.ToArray());
        }
Пример #4
0
        private dynamic Send(byte[] data, int attempts_total = 4)
        {
            dynamic answer = new ExpandoObject();

            answer.success = false;
            answer.error   = string.Empty;
            try
            {
                answer.errorcode = DeviceError.NO_ERROR;

                byte[] bufferASCII = null;
                byte[] buffer      = null;

                for (var attempts = 0; attempts < attempts_total && answer.success == false; attempts++)
                {
                    /*buffer = new byte[] {0x01,0x48,00,0xDA,00,00,0x0B,0x0C,04,0x12,0x88,06,0x42,0xA4,0xA1,0x2D,0x3F,0x16,0x9F,0xBE,0x40,04,0xC0,0x6F,0x40,00,0x2F,0xB1,0x42,0x60,0xD6,0xE6,0x3E,0xC8,0xDF,0x3B,0x40,03,0xF2,0xE1,0x40,01,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,
                     *      00,00,00,00,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,0xB3,0x70,0x41,0xD9,00,00,0x7F,0xF0,0xB1,06,0x3E,0x6E,00,00,0x7F,
                     *      0xF0,00,00,0x7F,0xF0,00,01,00,00,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,00,00,00,00,0x7F,0xF0,00,00,0x7F,0xF0,00,00,0x7F, 0xF0,00,00,0x7F,0xF0,00,01,00,00,00,00,0x7F,0xF0,00,00,00,00,00,00,00,00,00,00,00,00,00,0x80,00,00,00,00,
                     *      00,00,00,00,06,00,00,00,0x0C,00,0x0B,00,00,00,0xEE,0x38,00,00,00,00,00,00,00,00,0xCD};
                     *
                     * if (buffer[0] != NetworkAddress)
                     * {
                     *  answer.error = "Несовпадение сетевого адреса";
                     *  answer.errorcode = DeviceError.ADDRESS_ERROR;
                     * }
                     * else
                     * {
                     *  answer.success = true;
                     *  answer.error = string.Empty;
                     *  answer.errorcode = DeviceError.NO_ERROR;
                     * }*/
                    bufferASCII = SendSimple(data);

                    if (bufferASCII.Length == 0)
                    {
                        answer.error     = "Нет ответа";
                        answer.errorcode = DeviceError.NO_ANSWER;
                    }
                    else
                    {
                        if (bufferASCII.Length < 4)
                        {
                            answer.error     = "В кадре ответа не может содежаться менее 4 байт";
                            answer.errorcode = DeviceError.TOO_SHORT_ANSWER;
                        }
                        else if (bufferASCII[0] != 0x3A || (bufferASCII[bufferASCII.Length - 2] != 0x0d && bufferASCII[bufferASCII.Length - 1] != 0x0a))
                        {
                            answer.error     = "Отсутствуют начало и конец пакета";
                            answer.errorcode = DeviceError.PACKET_ERROR;
                        }
                        buffer = Helper.ASCIItoBytes(bufferASCII.Skip(1).Take(bufferASCII.Count() - 3).ToArray());
                        if (!LRC.Check(buffer))
                        {
                            answer.error     = "Контрольная сумма пакета не сошлась";
                            answer.errorcode = DeviceError.CRC_ERROR;
                        }
                        else if (buffer[0] != NetworkAddress)
                        {
                            answer.error     = "Несовпадение сетевого адреса";
                            answer.errorcode = DeviceError.ADDRESS_ERROR;
                        }
                        else
                        {
                            answer.success   = true;
                            answer.error     = string.Empty;
                            answer.errorcode = DeviceError.NO_ERROR;
                        }
                    }
                }

                if (answer.success)
                {
                    if (buffer[1] == 0x48)
                    {
                        answer.Body = buffer.Skip(6).ToArray();
                    }
                    else if (buffer[1] == 0x03)
                    {
                        answer.Body = buffer.Skip(3).ToArray();
                    }
                    else
                    {
                        answer.success = false;
                        answer.error   = "Нет такой команды";
                    }
                    answer.NetworkAddress = buffer[0];
                }
                return(answer);
            }
            catch
            {
                answer.error     = "Send exception";
                answer.errorcode = DeviceError.DEVICE_EXCEPTION;
                return(answer);
            }
        }