示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            Xdgk.Communi.BytesConverterManager bcMan = XD221ModbusConfigObject.BytesConverterManager;
            // TODO: get ci
            //
            XD221ConfigDefine ci  = null; // this.XD221ConfigItem;
            IBytesConverter   ibc = bcMan.CreateBytesConverter(ci.Converter, null);

            if (ibc == null)
            {
                string s = string.Format("Create converter fail, converter type '{0}'", ci.Converter);
                throw new Exception(s);
            }

            byte[] inDataBs = ibc.ConvertToBytes(this.Value);
            // TODO: convert to modbus bs
            //
            byte[] beginBs     = GetModbusWordBytes(ci.Offset);
            byte[] wordCountBs = GetModbusWordBytes(inDataBs.Length / 2);
            byte[] byteCountBs = new byte[] { (byte)inDataBs.Length };


            MemoryStream ms = new MemoryStream();

            AppendBytes(ms, beginBs, wordCountBs, byteCountBs, inDataBs);
            return(ms.ToArray());
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bs"></param>
        /// <returns></returns>
        static public XD221ModbusConfigObject Parse(byte[] bs)
        {
            if (bs.Length == 7)
            {
                int begin     = GetModbusWord(bs, 0);
                int wordCount = GetModbusWord(bs, 2);
                int byteCount = bs[4];
                // TODO: wordindex converter
                //
                XD221ConfigDefine ci = XD221ConfigDefineCollection.Find(begin + 1);

                // TODO: ci == null
                //
                IBytesConverter         ibc = BytesConverterManager.CreateBytesConverter(ci.Converter, null);
                object                  obj = ibc.ConvertToObject(bs);
                XD221ModbusConfigObject mpo = new XD221ModbusConfigObject(ci.Name, obj);
                return(mpo);
            }
            return(null);
        }