Exemplo n.º 1
0
 public void SetDataRecv(DataRecv dr)
 {
     m_datarecv_send = dr;
 }
Exemplo n.º 2
0
        public List <byte> DataAnalyze_Rtu(List <byte> DataRecv)
        {
            /*-------Slave ID first-------*/
            if (DataRecv[0] != SlaveID)
            {
                DataRecv = new List <byte> {
                    0x00
                };
                return(DataRecv);
            }
            /*-------Crc Check Second------*/
            //byte[] DataRecv_copy = DataRecv.ToArray();
            int  count = DataRecv.Count;
            byte crc1  = DataRecv[count - 2];
            byte crc2  = DataRecv[count - 1];

            DataRecv.RemoveRange(count - 2, 2);
            CrcCheck_Rtu(DataRecv);
            /*---------------------  CRC Right? ----------------------*/
            if (crc1 != DataRecv[count - 2] && crc2 != DataRecv[count - 1])
            {
                DataRecv = new List <byte> {
                    0x01
                };                                   //Crc Error
                return(DataRecv);
            }
            /*------------------  DataLength Right? ---------------------*/
            int Addr = DataRecv[2] * 256 + DataRecv[3];
            int Amt  = DataRecv[4] * 256 + DataRecv[5];

            DataRecv.RemoveRange(count - 2, 2);
            int DataLength = DataRecv.Count;

            switch (DataRecv[1])//Func
            {
            case 0x01:
                if (DataLength != 6)
                {
                    DataRecv = new List <byte> {
                        0x02
                    };                                      //"DataLength Error"
                    return(DataRecv);
                }
                if (Addr >= 10000)
                {
                    Error_Code("0x02", DataRecv);
                }
                else if (Amt == 0 | Amt > 2000)
                {
                    Error_Code("0x03", DataRecv);
                }
                else if (Addr + Amt > 10000)
                {
                    Error_Code("0x03", DataRecv);
                }
                else if (Amt != 0)
                {
                    _0x01_Rtu(DataRecv);
                }
                CrcCheck_Rtu(DataRecv);
                return(DataRecv);

            case 0x03:
                if (DataLength != 6)
                {
                    DataRecv = new List <byte> {
                        0x02
                    };                                      //"DataLength Error"
                    return(DataRecv);
                }
                if (Addr + Amt > 10000)
                {
                    Error_Code("0x02", DataRecv);
                }
                else if (Amt == 0 | Amt > 125)
                {
                    Error_Code("0x03", DataRecv);
                }
                else if (Amt != 0)
                {
                    _0x03_Rtu(DataRecv);
                }
                CrcCheck_Rtu(DataRecv);
                return(DataRecv);

            case 0x0F:
                try
                {
                    if (DataLength != 6 + DataRecv[6] + 1)
                    {
                        DataRecv = new List <byte> {
                            0x02
                        };                                      //"DataLength Error"
                        return(DataRecv);
                    }
                }
                catch
                {
                    DataRecv = new List <byte> {
                        0x02
                    };                                      //"DataLength Error"
                    return(DataRecv);
                }
                if (Amt == 0 | Amt > 1968)
                {
                    Error_Code("0x03", DataRecv);
                }
                else if (Addr + Amt > 10000)
                {
                    Error_Code("0x02", DataRecv);
                }
                else if (Amt != 0)
                {
                    _0x0F_Rtu(DataRecv);
                }
                CrcCheck_Rtu(DataRecv);
                return(DataRecv);

            case 0x10:
                try
                {
                    if (DataLength != 6 + DataRecv[6] + 1)
                    {
                        DataRecv = new List <byte> {
                            0x02
                        };                                      //"DataLength Error"
                        return(DataRecv);
                    }
                }
                catch
                {
                    DataRecv = new List <byte> {
                        0x02
                    };                                      //"DataLength Error"
                    return(DataRecv);
                }
                if (Amt == 0 | Amt > 123)
                {
                    Error_Code("0x03", DataRecv);
                }
                else if (Addr + Amt >= 10000)
                {
                    Error_Code("0x02", DataRecv);
                }
                else if (Amt != 0)
                {
                    _0x10_Rtu(DataRecv);
                }
                CrcCheck_Rtu(DataRecv);
                return(DataRecv);

            default:
            {
                Error_Code("0x01", DataRecv);
                CrcCheck_Rtu(DataRecv);
                return(DataRecv);
            }
            }
        }