public UInt8T this[int index] { get { #if DEBUG if (index < 0 || index >= DataLength) { throw new ArgumentOutOfRangeException(nameof(index), index, $"Value \"index\" should be greater equal than 0 and less than {DataLength}"); } #endif UInt8T retVal; lock (atomicityLock) { retVal = rawFrame[HeaderLength + index]; } return(retVal); } set { #if DEBUG if (index < 0 || index >= DataLength) { throw new ArgumentOutOfRangeException(nameof(index), index, $"Value \"index\" should be greater equal than 0 and less than {DataLength}"); } #endif lock (atomicityLock) { byte oldValue = rawFrame[HeaderLength + index]; if (oldValue == value) { goto DoNothing; } UInt16T oldChecksum = 0; oldChecksum[1] = rawFrame[rawFrame[2] - 2]; oldChecksum[0] = rawFrame[rawFrame[2] - 1]; rawFrame[HeaderLength + index] = value; unchecked { if (value > oldValue) { oldChecksum = (ushort)(oldChecksum + (value - oldValue)); } else { oldChecksum = (ushort)(oldChecksum - (oldValue - value)); } } rawFrame[rawFrame[2] - 2] = oldChecksum[1]; rawFrame[rawFrame[2] - 1] = oldChecksum[0]; } DoNothing: return; } }
public ProtoFrame(byte length, byte messageId) { DataLength = (byte)(length - HeaderLength - ChecksumLength); rawFrame[0] = SyncCode0; rawFrame[1] = SyncCode1; rawFrame[2] = length; rawFrame[3] = messageId; UInt16T checksum = 0; for (int i = 0; i < (HeaderLength - 1); i++) { checksum += rawFrame[i]; } rawFrame[4] = checksum[0]; checksum += rawFrame[4]; rawFrame[rawFrame[2] - 2] = checksum[1]; rawFrame[rawFrame[2] - 1] = checksum[0]; }