Ejemplo n.º 1
0
        public string cmd_generator(string cmd)
        {
            CRC crc = new CRC();
            string length_data = string.Empty;

            HEADER = string.Empty;
            DATA_BYTE_LENGTH = 0;
            USER_DATA = string.Empty;
            CMD = string.Empty;
            ADDRESS = string.Empty;
            CMD_CODE = string.Empty;
            RTU_SYS_TIME = string.Empty;
            WORK_STATE = string.Empty;
            SIGNAL_S = string.Empty;
            CRC_DATA = string.Empty;

            string result=string.Empty;
            switch (cmd)
            {
                case "QUERY":

                    CMD = "00";
                    ADDRESS = rtu_id;
                    CMD_CODE = "21";
                    USER_DATA = CMD + ADDRESS + CMD_CODE;
                    CRC_DATA = Convert.ToString(crc.CRC8(USER_DATA, 0), 16);

                    DATA_BYTE_LENGTH = USER_DATA.Length / 2;
                    length_data = swapByte(DATA_BYTE_LENGTH.ToString("x8"));
                    HEADER = "68" + length_data + length_data + "68";
                    result = HEADER + USER_DATA + CRC_DATA + "16";
                    break;
                case "LOGOUT":

                    break;
                default:

                    break;
            }
            return result;
        }
Ejemplo n.º 2
0
        public string ParseData(string rtu_data)
        {
            string result = string.Empty;
            bool IsVaild = false;
            string S_DATA_LENGTH;

            //1. Vertify input data
            if (rtu_data.Substring(0, 2) == "68" && rtu_data.Substring(rtu_data.Length-2, 2) == "16")
            {
                IsVaild = true;
            }
            else
            {
                IsVaild = false;
            }

            if (IsVaild == true)
            {
                //2. split header to get length
                S_DATA_LENGTH = rtu_data.Substring(2, 8);
                DATA_BYTE_LENGTH = Convert.ToInt32(swapByte(S_DATA_LENGTH.Substring(0, 4)),16);

                if (S_DATA_LENGTH.Substring(0, 4) == S_DATA_LENGTH.Substring(4, 4))
                {

                    //DATA_BYTE_LENGTH*2 + 4 --- 2 means 1 bytes to 2 chars, 4 means "CRC" 2 chars + "end" 2 chars
                    if (rtu_data.Substring(12).Length == (DATA_BYTE_LENGTH*2 + 4))
                    {
                        Console.WriteLine("{0}", "正常:数据长度正确");
                        //3. GET CMD ADD CMD_CODE
                        CMD = rtu_data.Substring(12, 2);
                        ADDRESS = rtu_data.Substring(14, 8);
                        CMD_CODE = rtu_data.Substring(22, 2);
                        USER_DATA = rtu_data.Substring(12, DATA_BYTE_LENGTH * 2);
                        CRC ccc = new CRC();
                        CRC_DATA = Convert.ToString(ccc.CRC8(USER_DATA, 0), 16);
                         Console.WriteLine("USER_dATA: {0}, {1}", USER_DATA, CRC_DATA);

                        //4. judge CMD_TYPE
                        if (CMD == "80" && CMD_CODE == "11")
                        {
                            CMD_TYPE = "RTU_LOGIN";
                        }
                        else if ((CMD == "A0" || CMD == "B0") && CMD_CODE == "21")
                        {
                            CMD_TYPE = "RTU_DATA";
                        }else if((CMD == "80" && CMD_CODE == "12"))
                        {
                            CMD_TYPE = "RTU_REQUIRE_LOGOUT";
                        }
                        else if(CMD == "E0")
                        {
                            CMD_TYPE = "CMD_ERROR--";
                            switch(rtu_data.Substring(24, 2))
                            {
                                case "01":
                                    break;
                                case "02":
                                    break;
                                case "03":
                                    break;
                                case "04":
                                    CMD_TYPE += "RTU_DATA_EMPTY";
                                    break;
                                default:
                                    CMD_TYPE += "ERROR_CODE";
                                    break;
                            }

                        }else if (CMD == "A0" && CMD_CODE == "12")
                        {
                            CMD_TYPE = "RTU_LOGOUT";
                        }

                        //5.
                        switch (CMD_TYPE)
                        {
                            case "RTU_LOGIN":
                                longCmdParse(rtu_data);
                                break;
                            case "RTU_DATA":

                                FD_DATA = FD_DataParse(USER_DATA);
                                break;
                            case "RTU_REQUIRE_LOGOUT":

                                break;
                            case "RTU_DATA_EMPTY":
                                break;
                            case "ERROR_READ_ERROR":
                                break;

                        }

                        Console.WriteLine("Length:{0},Cmd:{1},Address:{2},Cmd_code:{3}", DATA_BYTE_LENGTH, CMD, ADDRESS, CMD_CODE);

                    }
                    else
                    {
                        Console.WriteLine("{0}", "错误:数据长度错误");
                        return "ERROR_LENGTH";
                    }

                }
                else
                {
                    Console.WriteLine("{0}", "错误:L1 L2不相等");
                    return "ERROR_L1_L2LENGTH";
                }

            }

               return result;
        }