private void i2cWrite(I2cMaster master, I2cSendMode sendMode) { if (master == null) { return; } if (master.device != null) { string addressUsed = HexConverter.ByteToString(master.device.ConnectionSettings.SlaveAddress); try { string result = ""; switch (sendMode) { case I2cSendMode.Write: byte[] SendBuf = HexConverter.StringToByteArray(master.write); if (SendBuf.Count() > 0) { result = master.device.WritePartial(SendBuf).Status.ToString(); status.Log(string.Format(LocalizableStrings.IOT_I2C_WRITE, master.name, HexConverter.ByteToString(SendBuf), addressUsed)); } break; case I2cSendMode.WriteRead: byte[] WriteBuf = HexConverter.StringToByteArray(master.writeRead); if (WriteBuf.Count() > 0) { byte[] ReadBuf = new byte[master.readSize]; result = master.device.WriteReadPartial(WriteBuf, ReadBuf).Status.ToString(); status.Log(string.Format(LocalizableStrings.IOT_I2C_WRITE, master.name, HexConverter.ByteToString(WriteBuf), addressUsed)); status.Log(string.Format(LocalizableStrings.IOT_I2C_READ, master.name, HexConverter.ByteToString(ReadBuf), addressUsed)); } break; case I2cSendMode.Read: byte[] RecBuf = new byte[master.readSize]; result = master.device.ReadPartial(RecBuf).Status.ToString(); status.Log(string.Format(LocalizableStrings.IOT_I2C_READ, master.name, HexConverter.ByteToString(RecBuf), addressUsed)); break; } status.Log(result); } catch { status.Log(string.Format(LocalizableStrings.IOT_I2C_NORESPONSE, master.name, addressUsed)); } } else { status.Log(string.Format(LocalizableStrings.IOT_I2C_NOT_INITIALIZED, master.name)); } }
private async Task i2cActivate(I2cMaster master) { if (master == null) { return; } if (master.device != null) { master.device.Dispose(); master.device = null; } var address = HexConverter.StringToByteArray(master.slaveAddress); if (address.Count() < 1) { status.Log(string.Format(LocalizableStrings.IOT_I2C_ADDRESS_INVALID, master.name)); return; } var settings = new I2cConnectionSettings(address.First()); settings.BusSpeed = master.busSpeed; try { master.device = await I2cDevice.FromIdAsync(master.info.Id, settings); string addressUsed = HexConverter.ByteToString(master.device.ConnectionSettings.SlaveAddress); if (master.device == null) { status.Log(string.Format(LocalizableStrings.IOT_I2C_IN_USE, addressUsed, master.name)); } status.Log(string.Format(LocalizableStrings.IOT_I2C_INITIALIZED, addressUsed, master.name)); } catch (Exception ex) { status.Log(string.Format(LocalizableStrings.IOT_I2C_INITIALIZE_FAILED, ex.Message)); } }
private void spiSend(SpiController controller, SpiSendMode sendMode) { if (controller == null) { return; } if (controller.device != null) { switch (sendMode) { case SpiSendMode.Write: var WriteBuf = HexConverter.StringToByteArray(controller.write); if (WriteBuf.Count() > 1) { controller.device.Write(WriteBuf); status.Log(string.Format(LocalizableStrings.IOT_SPI_WROTE, controller.name, HexConverter.ByteToString(WriteBuf))); } break; case SpiSendMode.Transfer: var SendBuf = HexConverter.StringToByteArray(controller.transfer); if (SendBuf.Count() > 1) { var ReadBuf = new byte[SendBuf.Count()]; controller.device.TransferFullDuplex(SendBuf, ReadBuf); status.Log(string.Format(LocalizableStrings.IOT_SPI_OUT, controller.name, HexConverter.ByteToString(SendBuf))); status.Log(string.Format(LocalizableStrings.IOT_SPI_IN, controller.name, HexConverter.ByteToString(ReadBuf))); } break; } } else { status.Log(string.Format(LocalizableStrings.IOT_SPI_NOT_INITIALIZED, controller.name)); } }