private void btnCrc_Click(object sender, EventArgs e) { var value = tbIn.Text; byte[] data = cbIsAscii.Checked ? Encoding.Default.GetBytes(value) : StringByteUtils.StringToBytes(value); tbCrc16.Text = StringByteUtils.BytesToString(CRC.Crc16(data, data.Length)); tbCrcc.Text = StringByteUtils.BytesToString(CRC.CRC16_C(data)); tbUpdateCRC.Text = StringByteUtils.BytesToString(StringByteUtils.GetBytes(CRC.UpdateCRC(data, data.Length), true)); tbGBcrc16.Text = $"##{value.Length.ToString().PadLeft(4, '0')}{value}{StringByteUtils.BytesToString(CRC.GBcrc16(data, data.Length)).Replace(" ", "")}"; tbHBcrc16.Text = $"##{value.Length.ToString().PadLeft(4, '0')}{value}{StringByteUtils.BytesToString(CRC.HBcrc16(data, data.Length)).Replace(" ", "")}"; }
public byte[] ToBytes() { var dataLength = BitConverter.GetBytes(_data.Length / 2); var byteCount = (byte)_data.Length; var startAddr = StringByteUtils.GetBytes(_registerAddress, true); var head = new byte[] { _deviceAddress, 0x10, startAddr[0], startAddr[1], dataLength[1], dataLength[0], byteCount }; var temp = StringByteUtils.ComibeByteArray(head, _data); var crc = CRC.Crc16(temp, temp.Length); return(StringByteUtils.ComibeByteArray(temp, crc)); }