gpt4 book ai didi

c - RFID RC522读卡接线Pi

转载 作者:行者123 更新时间:2023-11-30 16:52:26 25 4
gpt4 key购买 nike

我正在使用 raspberyy pi 3,RFID RC522。我想使用 WiringPi 来读取卡片。我正在尝试这段代码;

#include<stdio.h>
#include<conio.h>
#include<wiringPi.h>
#include<wiringPiSPI.h>

int main()
{
int chan = 1;
int speed = 1000000;

if (wiringPiSPISetup(chan, speed) == -1)
{
printf("Could not initialise SPI\n");
return;
}
printf("When ready hit enter.\n");
(void) getchar(); // remove the CR
unsigned char buff[100];

while (1)
{
int ret = wiringPiSPIDataRW(chan, buff, 4);
printf("%d %s \n", ret, buff);

}
}

当我尝试这个时,它总是变成“4”。怎么读都看不懂。

最佳答案

您正在向从属 SPI 设备发送未初始化的数据。

unsigned char buff[100];

while (1)
{
int ret = wiringPiSPIDataRW(chan, buff, 4);
printf("%d %s \n", ret, buff);

}

缓冲区内容不确定。

查看library doc

int wiringPiSPIDataRW (int channel, unsigned char *data, int len);

This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

这意味着您应该使用要发送的消息来初始化缓冲区。该数据将丢失,因为从机回复将返回到同一缓冲区中。

查看this example你应该做一些类似的事情:

 unsigned char buff[100] = {0};

// Following bytes must be set according to your slave SPI device docs.
buffer[0] = ??;
buffer[1] = ??;
buffer[2] = ??;
buffer[3] = ??;
wiringPiSPIDataRW(chan, buffer, 4);

关于c - RFID RC522读卡接线Pi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41218617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com