//} /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void okButton_Click(object sender, EventArgs e) { IComparable value; bool r = ParseParameterValue(out value); if (!r) { return; } // TODO: 2010-07-29 convert param value to byte[] // // 1. get converter name // 2. get bytes converter // 3. bc.ConvertToBytes( paramvalue ) save to data byte array // ParameterDefine pd = this.GetSelectedParameterDefine(); string converterName = pd.ConverterName; IBytesConverter ibc = GetBytesConverter(converterName); if (ibc == null) { string exmsg = string.Format("cannot get bytesconverter '{0}'", converterName); throw new InvalidOperationException(exmsg); } byte[] bs = ibc.ConvertToBytes(value); this._datas = bs; this.DialogResult = DialogResult.OK; Close(); }
/// <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()); }