public RUSEMCModuleDevice(WordSerializator serializator) : base(serializator) { _serial = 100; _status16 = 0; _calibrationFile = deserialize(Properties.Resources.V01_RUS40_Cal, FileType.CALIBRATION); _factorySettingsFile = deserialize(Properties.Resources.V01_RUS40_FSet, FileType.FACTORY_SETTINGS); _dataPacketFormatFile = generateDataPacketFormatFile(); byte[] generateDataPacketFormatFile() { var body = new Enumerable <byte>() { "EMC_Mdl_0101DDMMYYF_".GetASCIIBytes(), serializator.Serialize(_serial), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Reserved }; var i = new SmartInt().Add(1); addEntity("STAT", i, i.Add(2).DValue, 0); addEntity("STAT", i, i.Add(2).DValue, 0); addEntity("TST1", i, i.Add(2).DValue, 0); addEntity("TST2", i, i.Add(2).DValue, 0); return(body.ToArray()); void addEntity(string mnemonicName, int position, int length, byte numOfBits) { body.Add(mnemonicName.GetASCIIBytes()); body.Add(_serializator.Serialize((ushort)position)); body.Add((byte)length); body.Add(numOfBits); } } }
public InclinometrVirtualDevice(WordSerializator serializator) : base(serializator) { _serial = 999; _calibrationFile = deserialize(Properties.Resources.V01_RUS03_Cal, FileType.CALIBRATION); _factorySettingsFile = deserialize(Properties.Resources.V01_RUS03_FSet, FileType.FACTORY_SETTINGS); _dataPacketFormatFile = generateDataPacketFormatFile(); _flashDataPacketFormatFile = _dataPacketFormatFile; _temperatureCalibrationFile = deserialize(Properties.Resources.V01_RUS03_TCal, FileType.TEMPERATURE_CALIBRATION); byte[] generateDataPacketFormatFile() { var body = new Enumerable <byte>() { "INCL____0101DDMMYYF_".GetASCIIBytes(), serializator.Serialize(_serial), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Reserve }; var i = new SmartInt().Add(1); addEntity("STAT", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("INC_", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("AZI_", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("GTF_", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("XGpr", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("YGpr", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("ZGpr", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("XMpr", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("YMpr", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("ZMpr", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("TGad", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("TMad", (ushort)i, (byte)i.Add(2).DValue, 0, true); return(body.ToArray()); void addEntity(string mnemonicName, ushort position, byte length, byte numOfBits, bool isSigned) { body.Add(mnemonicName.GetASCIIBytes()); body.Add(_serializator.Serialize(position)); body.Add(length); body.Add((isSigned ? (1 << 7) : 0).ToByte().BitOR(numOfBits)); } } }
MultiProtocolRequest(byte deviceId, RequestAddress address, bool isWriteRequest, ResponseInfo responseInfo, IEnumerable <IDataEntity> dataEntities) { DeviceId = deviceId; Address = address; IsWriteRequest = isWriteRequest; var addressInfo = Address.GetInfo(); _responseInfo = responseInfo; _hasLengthField = _responseInfo.ResponseFullLength.IsUnknown; if (address != RequestAddress.NONE) { _commandWords = new ushort[] { (ushort)((DeviceId << 9) | ((~0x80) & Address.GetInfo().Address) | (IsWriteRequest ? 1 << 7 : 0)), 0x0000 }; _header = wordsToBytes(_commandWords).ToArray(); } Serialized = serialize(dataEntities); IEnumerable <byte> serialize(IEnumerable <IDataEntity> entities) { if (address == RequestAddress.NONE) { return(new byte[0]); } else { var dataBytes = entities .Select(e => e.RawValue) .Flatten() .ToArray(); var zeroTrailShouldBeAdded = dataBytes.Length % 2 == 1; var actualLength = zeroTrailShouldBeAdded ? dataBytes.Length + 1 : dataBytes.Length; var requestBytes = new Enumerable <byte>() { _header, IsWriteRequest // If there is data - we need to add length of the data ? wordsToBytes(((ushort)actualLength).ToSequence()) : new byte[0], dataBytes, zeroTrailShouldBeAdded ? new byte[1] : new byte[0] }; requestBytes.Add(wordsToBytes(_checksum.ComputeChecksum(requestBytes).ToSequence())); return(requestBytes); } } }
public RotationSensorVirtualDevice(WordSerializator serializator) : base(serializator) { _calibrationFile = deserialize(Properties.Resources.V01_RUS07_Cal, FileType.CALIBRATION); _factorySettingsFile = deserialize(Properties.Resources.V01_RUS07_FSet, FileType.FACTORY_SETTINGS); _dataPacketFormatFile = generateDataPacketFormatFile(); _flashDataPacketFormatFile = generateDataPacketFormatFile(); _temperatureCalibrationFile = deserialize(Properties.Resources.V01_RUS07_TCal, FileType.TEMPERATURE_CALIBRATION); byte[] generateDataPacketFormatFile() { var body = new Enumerable <byte>() { "Bl_Gyro_0101DDMMYYF_".GetASCIIBytes(), serializator.Serialize(_serial), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Reserve }; var i = new SmartInt().Add(1); addEntity("STAT", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("SGY1", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("SRO1", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("ANG1", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("TEM1", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("SGY2", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("SRO2", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("ANG2", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("TEM2", (ushort)i, (byte)i.Add(2).DValue, 0, true); addEntity("ADSG", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("ADH1", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("ADH2", (ushort)i, (byte)i.Add(2).DValue, 0, false); addEntity("ADTM", (ushort)i, (byte)i.Add(2).DValue, 0, true); return(body.ToArray()); void addEntity(string mnemonicName, ushort position, byte length, byte numOfBits, bool isSigned) { body.Add(mnemonicName.GetASCIIBytes()); body.Add(_serializator.Serialize(position)); body.Add(length); body.Add((isSigned ? 1 : 0).ToByte().BitLShift(7).BitOR(numOfBits)); } } }
public RUSTechnologicalModuleDevice(WordSerializator serializator) : base(serializator) { _serial = 100; _status32 = Global.Random.NextUInt(); _calibrationFile = deserialize(Properties.Resources.V01_RUS24_Cal, FileType.CALIBRATION); _factorySettingsFile = deserialize(Properties.Resources.V01_RUS24_FSet, FileType.FACTORY_SETTINGS); _dataPacketFormatFile = generateDataPacketFormatFile(); _flashDataPacketFormatFile = generateDataPacketFormatFile(); _temperatureCalibrationFile = deserialize(Properties.Resources.V01_RUS24_TCal, FileType.TEMPERATURE_CALIBRATION); _workModeFile = deserialize(Properties.Resources.V01_RUS24_WMode, FileType.WORK_MODE); byte[] generateDataPacketFormatFile() { var body = new Enumerable <byte>() { "TEXMODUL0101DDMMYYF_".GetASCIIBytes(), serializator.Serialize(_serial), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Reserved }; var i = new SmartInt().Add(1); addEntity("STAT", i, i.Add(2).DValue, 0); addEntity("STAT", i, i.Add(2).DValue, 0); addEntity("TST1", i, i.Add(2).DValue, 0); addEntity("TST2", i, i.Add(2).DValue, 0); return(body.ToArray()); void addEntity(string mnemonicName, int position, int length, byte numOfBits) { body.Add(mnemonicName.GetASCIIBytes()); body.Add(_serializator.Serialize((ushort)position)); body.Add((byte)length); body.Add(numOfBits); } } }
public override async Task <byte[]> WaitAsync(int count, WaitMode waitMode, AsyncOperationInfo operationInfo) { var result = await base.WaitAsync(count, waitMode, operationInfo); ReadCount += result.Length; var bytesToStore = Math.Min(Capacity - (ReadCount - result.Length), result.Length).NegativeToZero().ToInt32(); if (bytesToStore != 0) { _storage.Add(result.Take(bytesToStore)); StorageCount += bytesToStore; } return(result); }
byte[] buildRequest(byte id, byte command, bool isReadCommand) { var bytes = new Enumerable <byte>() { id.BitLShift(1), (isReadCommand ? 0 : (1 << 7)).ToByte().BitOR(command), 0, 0 }; if (isReadCommand) { bytes.Add(_serializator.Serialize(_crc16Calculator.ComputeChecksum(bytes))); } return(bytes.ToArray()); }
protected override IEnumerable <byte> serialize(DataEntityFormat format, object entity) { switch (format) { case DataEntityFormat.CALIBRATION_PACKET_ENTITIES_ARRAY: { var descriptors = (CalibrationFileEntity[])entity; return(descriptors.Select(serialize).Flatten()); IEnumerable <byte> serialize(CalibrationFileEntity descriptor) { var descriptorData = new Enumerable <byte> { CP1251_ENCODING.GetBytes(descriptor.Name), 0, (byte)descriptor.ItemType, (byte)descriptor.DataType, EntitySerializersFactory .GetSerializer(DataEntityFormat.UINT16) .Serialize(DataEntityFormat.UINT16, (ushort)descriptor.DataLength) }; switch (descriptor.DataType) { case DataTypes.INT8: { var bytes = ((Array)descriptor.Data) .ToEnumerable() .Select(o => (byte)o) .ToArray(); descriptorData.Add(bytes); } break; case DataTypes.INT16: { var bytes = ((Array)descriptor.Data) .ToEnumerable() .Select(o => (short)o) .Select(v => EntitySerializersFactory.GetSerializer(DataEntityFormat.INT16).Serialize(DataEntityFormat.INT16, (short)v)) .Flatten() .ToArray(); descriptorData.Add(bytes); } break; case DataTypes.UINT16: { var bytes = ((Array)descriptor.Data) .ToEnumerable() .Select(o => (ushort)o) .Select(v => EntitySerializersFactory.GetSerializer(DataEntityFormat.UINT16).Serialize(DataEntityFormat.UINT16, (ushort)v)) .Flatten() .ToArray(); descriptorData.Add(bytes); } break; case DataTypes.FLOAT: { var bytes = ((Array)descriptor.Data) .ToEnumerable() .Select(o => (float)o) .Select(v => BitConverter.ToUInt32(BitConverter.GetBytes(v), 0)) .Select(v => EntitySerializersFactory.GetSerializer(DataEntityFormat.UINT32).Serialize(DataEntityFormat.UINT32, (uint)v)) .Flatten() .ToArray(); descriptorData.Add(bytes); } break; case DataTypes.STRING_CP1251: { var bytes = CP1251_ENCODING.GetBytes((string)descriptor.Data); descriptorData.Add(bytes); } break; default: throw new NotSupportedException(); } return(descriptorData); } } default: throw new NotSupportedException(); } }