如果我想通过SPI连接第二个rfid阅读器,我应该从主题RFID RC522 Raspberry PI 2 Windows IOT中改变什么?
我将第二个“ss线”连接到SPI_CE1_N引脚,将“复位线”连接到GPIO12引脚。
我在类Mfrc522中添加了一个构造函数,我使用SPI_CONTROLLER_NAME,SPI_CHIP_SELECT_LINE,RESET_PIN作为参数但没有成功。
第一次初始化
public async Task InitIO()
{
...
...
try
{
settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE);
settings.ClockFrequency = 1000000;
settings.Mode = SpiMode.Mode0;
String spiDeviceSelector = SpiDevice.GetDeviceSelector();
IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);
_spi = await SpiDevice.FromIdAsync(devices[0].Id, settings);
}
catch (Exception ex)
{
throw new Exception("SPI Initialization Failed", ex);
}
...
...
}
第二次初始化:
public async Task ReInitSpi()
{
_spi.Dispose(); //Here is important.
try
{
if (settings.ChipSelectLine == 0)
{
settings = new SpiConnectionSettings(1); //CS1
}
else
{
settings = new SpiConnectionSettings(SPI_CHIP_SELECT_LINE); //CS0
}
settings.ClockFrequency = 1000000;
settings.Mode = SpiMode.Mode0;
String spiDeviceSelector = SpiDevice.GetDeviceSelector();
IReadOnlyList<DeviceInformation> devices = await DeviceInformation.FindAllAsync(spiDeviceSelector);
_spi = await SpiDevice.FromIdAsync(devices[0].Id, settings);
}
/* If initialization fails, display the exception and stop running */
catch (Exception ex)
{
throw new Exception("SPI Initialization Failed", ex);
}
}
例如,每次使用它时,请执行以下操作:
await mfrc.ReInitSpi();
var writeBuffer = new byte[] { 0x55, 0xaa };
mfrc._spi.Write(writeBuffer);
这适合我。我希望它对你有所帮助。