public byte[] ToBytes() { var res = new List <byte>(); var ascii = new ASCIIEncoding(); byte[] add = ascii.GetBytes(_header); res.AddRange(add); res.Add(_discretOffset); res.Add(_relayOffset); res.Add(_diodeOffset); res.Add(_systemDiodeOffset); res.Add(_discretCount); res.AddRange(Common.TOBYTES(_discretConfig.Select(o => o.Value).ToArray(), false)); res.Add(_relayCount); res.AddRange(Common.TOBYTES(_relayConfig.Select(o => o.Value).ToArray(), false)); res.Add(_diodeCount); res.AddRange(Common.TOBYTES(_diodeConfig.Select(o => o.Value).ToArray(), false)); res.Add(_systemDiodeCount); res.AddRange(Common.TOBYTES(_systemDiodeConfig.Select(o => o.Value).ToArray(), false)); var array = new byte[256]; Array.Copy(res.ToArray(), array, res.Count); var crc = Crc16.CalcCrcFast(array, array.Length - 2); array[array.Length - 2] = Common.HIBYTE(crc); array[array.Length - 1] = Common.LOBYTE(crc); return(array); }
private async Task WriteCRC() { _crc = Common.SwapByte(Crc16.CalcCrcFast(this.Data, Data.Length)); ushort[] data = new ushort[] { (ushort)_pageCount, 0x8000, _crc }; try { if (_isMR) { await ConnectionManager.Connection.ModbusMasterController?.WriteMultipleRegistersAsyncFunction12(_deviceNumber, (ushort)this._moduleInformation.FlashSize, _moduleInformation.ModulePosition, data, "запрос CRC"); } else { await ConnectionManager.Connection.ModbusMasterController?.WriteMultipleRegistersAsync(_deviceNumber, (ushort)this._moduleInformation.FlashSize, data, "запрос CRC"); } } catch (Exception e) { throw new Exception("Ошибка отправки CRC"); } }
public AllConfig(byte[] array) { _discretOffset = 20; _relayOffset = (byte)(_discretOffset + DISCRET_MAX * 2 + 1); _diodeOffset = (byte)(_relayOffset + DIODE_MAX * 2 + 1); _systemDiodeOffset = (byte)(_diodeOffset + DIODE_MAX * 2 + 1); if (array.Length != 256) { throw new ArgumentException(); } var crc = Crc16.CalcCrcFast(array, 256 - 2); if (Common.TOWORD(array[254], array[255]) != crc) { MessageBox.Show("Ошибка CRC"); throw new ArgumentException(); } var ascii = new ASCIIEncoding(); if (_header != new string(ascii.GetChars(array, 0, _header.Length))) { throw new ArgumentException(); } var discretOffset = array[16]; var relayOffset = array[17]; var diodeOffset = array[18]; var systemDiodeOffset = array[19]; _discretCount = array[discretOffset]; _discretConfig = new OneStruct[DISCRET_MAX]; for (int i = 0; i < DISCRET_MAX; i++) { if (i < _discretCount) { _discretConfig[i] = new OneStruct(Common.TOWORD(array[discretOffset + i * 2 + 2], array[discretOffset + i * 2 + 1])); } else { _discretConfig[i] = new OneStruct(); } } _relayCount = array[relayOffset]; _relayConfig = new OneStruct[RELAY_MAX]; for (int i = 0; i < RELAY_MAX; i++) { if (i < _relayCount) { _relayConfig[i] = new OneStruct(Common.TOWORD(array[relayOffset + i * 2 + 2], array[relayOffset + i * 2 + 1])); } else { _relayConfig[i] = new OneStruct(); } } _diodeCount = array[diodeOffset]; _diodeConfig = new OneStruct[DIODE_MAX]; for (int i = 0; i < DIODE_MAX; i++) { if (i < _diodeCount) { _diodeConfig[i] = new OneStruct(Common.TOWORD(array[diodeOffset + i * 2 + 2], array[diodeOffset + i * 2 + 1])); } else { _diodeConfig[i] = new OneStruct(); } } _systemDiodeCount = array[systemDiodeOffset]; _systemDiodeConfig = new OneStruct[SYSTEM_DIODE_MAX]; for (int i = 0; i < _systemDiodeCount; i++) { if (i < _systemDiodeCount) { _systemDiodeConfig[i] = new OneStruct(Common.TOWORD(array[systemDiodeOffset + i * 2 + 2], array[systemDiodeOffset + i * 2 + 1])); } else { _systemDiodeConfig[i] = new OneStruct(); } } }