public static byte[] PrepareWriteBytes(int salveId, SmsSlaveModel model = null) { List <byte> byteArray = new List <byte>(); if (salveId == 8) { var security = _database.GetModel <Security>()[0]; var rfids = _database.GetModel <Models.Database.Rfid>(); byteArray.Add((byte)security.BanQuota); byteArray.Add((byte)security.BanTime); byteArray.Add((byte)security.KeyPadPassword.Length); foreach (var c in security.KeyPadPassword) { byteArray.Add((byte)(c)); } //byteArray.Add((byte)rfids.Count); //foreach (var r in rfids) //{ // foreach(var rf in r.Text) // { // byteArray.Add((byte)Int32.Parse(rf.ToString())); // } //} } else { byteArray.Add((byte)model.Sflag); byteArray.Add((byte)model.Switch1); byteArray.Add((byte)model.Switch2); byteArray.Add((byte)model.SmsSend); //byteArray.Add((byte)11); //var nb = "96171193064"; //foreach(var n in nb) // byteArray.Add((byte)n); } return(byteArray.ToArray()); }
/// <summary> /// Sends control signal to the specific Arduino and retrieves response bytes. /// </summary> /// <param name="slave">Slave to which data to be send</param> /// <param name="ControlMode">Select specific control mode.</param> /// <param name="Pin">Pin to be set. ONLY VALID FOR MODE2</param> /// <param name="PinValue">Value to be set. ONLY VALID FOR MODE2</param> /// <returns>Returns fourteen response byte.</returns> public static async System.Threading.Tasks.Task WriteRead(Slave slave, Mode ControlMode, byte Pin = 0, byte PinValue = 0, SmsSlaveModel model = null) { while (Lock != false) { } Lock = true; // Create response byte array of fourteen byte[] Response2 = new byte[8]; byte[] Response1 = new byte[3]; try { // Initialize I2C var Settings = new I2cConnectionSettings(slave.I2cAddress); Settings.BusSpeed = I2cBusSpeed.FastMode; if (AQS == null || DIS == null) { AQS = I2cDevice.GetDeviceSelector(); DIS = await DeviceInformation.FindAllAsync(AQS); } using (I2cDevice Device = await I2cDevice.FromIdAsync(DIS[0].Id, Settings)) { byte[] writeValues = PrepareWriteBytes(slave.I2cAddress, model); if (slave.I2cAddress == 8) { //foreach(var w in writeValues) Device.Write(writeValues); Device.Read(Response1); DecodeRespose(slave.I2cAddress, Response1); } if (slave.I2cAddress == 9) { Device.Write(writeValues); Device.Read(Response2); DecodeRespose(slave.I2cAddress, Response2); } if (slave.I2cAddress == 8) { AppKeys.Slave1Reachable = new SolidColorBrush(Colors.Green); AppKeys.Slave1State = "Connected"; } if (slave.I2cAddress == 9) { AppKeys.Slave2Reachable = new SolidColorBrush(Colors.Green); AppKeys.Slave2State = "Connected"; } } } catch (Exception ex) { if (slave.I2cAddress == 8) { AppKeys.Slave1Reachable = new SolidColorBrush(Colors.Red); AppKeys.Slave1State = "Checking ..."; } if (slave.I2cAddress == 9) { AppKeys.Slave2Reachable = new SolidColorBrush(Colors.Red); AppKeys.Slave2State = "Checking ..."; } // SUPPRESS ERROR AND RETURN EMPTY RESPONSE ARRAY } Lock = false; //return Response; }