public override string GetMessage(TimerValueFormat timerValueFormat)
        {
            StringBuilder sb           = new StringBuilder();
            StringBuilder stringValues = new StringBuilder();

            IEnumerator enumerator = m_values.GetEnumerator();

            while (enumerator.MoveNext())
            {
                switch (OperandType)
                {
                case Utils.Operands.OP_XDW:
                case Utils.Operands.OP_DW:
                    UInt32 convertedInt32Value = Convert.ToUInt32(enumerator.Current);
                    string hexDW = convertedInt32Value.ToString("X").PadLeft(m_typeLength / 4, '0');
                    stringValues.Append(hexDW);
                    break;

                case Utils.Operands.OP_XL:
                case Utils.Operands.OP_ML:
                    Int32  convertedValue = Convert.ToInt32(enumerator.Current);
                    string hex            = convertedValue.ToString("X").PadLeft(m_typeLength / 4, '0');
                    stringValues.Append(hex);
                    break;

                case Utils.Operands.OP_SI:
                case Utils.Operands.OP_MI:
                case Utils.Operands.OP_COUNTER_PRESET:
                case Utils.Operands.OP_COUNTER_CURRENT:
                case Utils.Operands.OP_XI:
                    Int16  convertedInt16Value = Convert.ToInt16(enumerator.Current);
                    string hexMI = convertedInt16Value.ToString("X").PadLeft(m_typeLength / 4, '0');
                    stringValues.Append(hexMI);
                    break;

                case Utils.Operands.OP_MF:
                    string tmpHex = Utils.HexEncoding.ConvertSingleToHex(Convert.ToSingle(enumerator.Current))
                                    .PadRight(8, '0');
                    //Used for IEEE 754 standard.
                    string hexF = tmpHex.Substring(4, 4) + tmpHex.Substring(0, 4);
                    stringValues.Append(hexF);
                    break;

                case Utils.Operands.OP_TIMER_PRESET:
                case Utils.Operands.OP_TIMER_CURRENT:
                    UInt32 convertedUInt32Value =
                        timerValueFormat.Equals(TimerValueFormat.TimeFormat)
                                ? Utils.z_GetSecondsValue(enumerator.Current as List <ushort>)
                                : Convert.ToUInt32(enumerator.Current);
                    string hexUint32 = convertedUInt32Value.ToString("X").PadLeft(m_typeLength / 4, '0');
                    stringValues.Append(hexUint32);
                    break;
                }
            }

            sb.Append(STX);
            sb.Append(UnitId);
            sb.Append(AsciiCommandCode);
            sb.Append(Address);
            sb.Append(Length);
            sb.Append(stringValues.ToString());
            //sb.Append(CRC);
            sb.Append(CrcOf(stringValues.ToString()));
            sb.Append(ETX);

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        private List <object> GetValuesFromMessage(string message)
        {
            int           index  = 0;
            List <object> values = new List <object>();

            index += Utils.Lengths.LENGTH_STX1;
            int unitId = Convert.ToInt32(message.Substring(index, Utils.Lengths.LENGTH_UNIT_ID), 16);

            index += Utils.Lengths.LENGTH_UNIT_ID;
            string commandCode = message.Substring(index, Utils.Lengths.LENGTH_COMMAND_CODE);

            int valuesLength = m_length.Value * (m_typeLength / 4);

            index += Utils.Lengths.LENGTH_COMMAND_CODE;

            switch (commandCode)
            {
            case CommandCode.ReadIntegers.GX:     //COUNTER_CURRENT
            case CommandCode.ReadIntegers.GY:     //COUNTER_PRESET
            case CommandCode.ReadIntegers.GF:     //SI
            case CommandCode.ReadIntegers.RW:     //MI
                for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                {
                    values.Add(Int16.Parse(message.Substring(index, m_typeLength / 4), NumberStyles.HexNumber));
                    index += m_typeLength / 4;
                }

                break;

            case "RZ":                             //Fast Operands
            {
                switch (m_commandCode)             //sent CommandCode
                {
                case CommandCode.ReadIntegers.RZI: //XI
                {
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(Int16.Parse(message.Substring(index, m_typeLength / 4),
                                               NumberStyles.HexNumber));
                        index += m_typeLength / 4;
                    }

                    break;
                }

                case CommandCode.ReadIntegers.RZL:         //XL
                {
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(Int32.Parse(message.Substring(index, m_typeLength / 4),
                                               NumberStyles.HexNumber));
                        index += m_typeLength / 4;
                    }

                    break;
                }

                case CommandCode.ReadIntegers.RZD:         //XDW
                {
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(UInt32.Parse(message.Substring(index, m_typeLength / 4),
                                                NumberStyles.HexNumber));
                        index += m_typeLength / 4;
                    }

                    break;
                }
                }
            }
            break;

            case "RN":
                switch (m_commandCode)             //sent CommandCode
                {
                case CommandCode.ReadIntegers.RNJ: //SDW
                case CommandCode.ReadIntegers.RND: //DW
                {
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(UInt32.Parse(message.Substring(index, m_typeLength / 4),
                                                NumberStyles.HexNumber));
                        index += m_typeLength / 4;
                    }
                }
                break;

                case CommandCode.ReadIntegers.RNH:         //SL
                case CommandCode.ReadIntegers.RNL:         //ML
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(Int32.Parse(message.Substring(index, m_typeLength / 4),
                                               NumberStyles.HexNumber));
                        index += m_typeLength / 4;
                    }

                    break;

                case CommandCode.ReadIntegers.RNF:         //MF
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        string hexValueIEEE754 = (message.Substring(index, m_typeLength / 4));
                        string actualHexValue  =
                            hexValueIEEE754.Substring(4, 4) + hexValueIEEE754.Substring(0, 4);
                        values.Add(Utils.HexEncoding.ConvertHexToSingle(actualHexValue));
                        index += m_typeLength / 4;
                    }

                    break;
                }

                break;

            case CommandCode.ReadIntegers.GT:     //TIMER_CURRENT
            case CommandCode.ReadIntegers.GP:     //TIMER_PRESET
                if (TimerValueFormat.Equals(TimerValueFormat.TimeFormat))
                {
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(Utils.z_GetTimeUnits(Int32.Parse(message.Substring(index, m_typeLength / 4),
                                                                    NumberStyles.HexNumber)));
                        index += m_typeLength / 4;
                    }
                }
                else
                {
                    //SecondsFormat
                    for (int i = 0; i < valuesLength; i += m_typeLength / 4)
                    {
                        values.Add(UInt32.Parse(message.Substring(index, m_typeLength / 4),
                                                NumberStyles.HexNumber));
                        index += m_typeLength / 4;
                    }
                }

                break;
            }

            return(values);
        }