public ResponseText(MessageStoryItem msi = default) { if (msi == default) { return; } this.DateTime = msi.DateTime.ToString(); LastCmd = msi.Command.ToString(); if (msi.DataLength > 12) { UpFreq = msi.Data[0].ToString(); DnFreq = msi.Data[1].ToString(); StokeLength = msi.Data[2].ToString(); StokeRate = msi.Data[3].ToString(); StatusByte = msi.Data[3].ToString("X2"); var sb = new StatusByte { Byte = msi.Data[4] }; StatusStart = sb.Start ? "Started" : "Stopped"; StatusShortCircuit = sb.ShortCircuit ? "ShortCircuit" : "ok"; StatusOverTemp = sb.OverTemperature ? "OverHeat" : "ok"; StatusOverLoad = sb.OverLoad ? "OverLoad" : "ok"; } }
byte[] Heading(long size, StatusByte statusByte) { var arr = new byte[9]; var s = BitConverter.GetBytes(size); Array.Copy(s, arr, s.Length); arr[8] = (byte)statusByte; return(arr); }
/// <summary> /// Отправить объект, помеченный аттрибутом [Serializable] /// </summary> /// <param name="obj"></param> public void Send(object obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } Stream s; try { s = BinarySerializer.Serialize(obj); } catch (NotSupportedException) { throw; } StatusByte statusByte = obj is ConnectionOpitions ? StatusByte.connectionOpitions : (obj is StreamInfo ? StatusByte.streamInfo : obj is StatusByte ? StatusByte.timeoutCheck : StatusByte.data); toSend.Enqueue(new Pair <Stream, StatusByte>(s, statusByte)); StartSend(); }
void Heading(out long size, out StatusByte statusByte, byte[] heading) { size = BitConverter.ToInt64(heading, 0); statusByte = (StatusByte)heading[8]; }
/// <summary> /// Toggle the OST bit causing the sensor to immediately take another reading /// </summary> void TakeMeassure(StatusByte waitForStatus) { byte ctrlReg1 = I2C.ReadByte(Register.CTRL_REG1); ctrlReg1 = ctrlReg1.SetBit(1, false);//Clear OST bit ctrlReg1 = I2C.ReadByte(Register.CTRL_REG1); ctrlReg1 = ctrlReg1.SetBit(1, true); //Set OST byte I2C.WriteByte(Register.CTRL_REG1, ctrlReg1); int counter = 0; while ((I2C.ReadByte(Register.STATUS) & ((byte)waitForStatus)) == 0) { if (++counter > 100) throw new TimeoutException($"Operation timeouted while waiting for {waitForStatus} status"); Task.WaitAll(Task.Delay(1)); } }
/// <summary> /// Reads the status registry and waiting 100ms to the given value. /// </summary> /// <param name="status">Status to wait for</param> /// <exception cref="System.TimeoutException">If the status registry doesn't contains the status after 100ms</exception> void WaitForStatus(StatusByte status) { int counter = 0; while ((I2C.ReadByte(Register.STATUS) & (1 << 1)) == 0) { if (++counter > 100) throw new TimeoutException($"Operation timeouted while waiting for {status} status"); Task.WaitAll(Task.Delay(1)); } }