使用字节数组,我得到了“从char *到字节的无效转换”

问题描述 投票:0回答:1

和我在一起。我不太擅长用Arduino / C编写。

我正在使用MFRC522.h读写RFID标签我目前可以读取卡的UID并将其转储到“ UIDChar”卡的UID通常为8个字符。

UID Example:  467EE9A9

我可以使用mfrc522.MIFARE_SetUid函数将此UID写入新卡。为此,我必须将newUID设置为:

0x46,0x7E,0xE9,0xA9f

我已将此代码写入我的代码。我想要做的是将UID字符串转换为字节数组,以便可以代替手动编写的0x46,0x7E,0xE9,0xA9。

我使用convert函数将UID转换为该格式。可以用“ buf”显示。

Serial.println(buf);

现在是我的问题。如果我替换]

byte newUid[] = {0x46,0x7E,0xE9,0xA9f};

with

byte newUid[] = {buf};

我收到错误从'char *'到'byte {aka unsigned char}'的无效转换'

如何将我的“ newUid”设置为“ buf”?

#define SS_PIN 0  //D2
#define RST_PIN 2 //D1

#include <SPI.h>
#include <MFRC522.h>


/* For RFID */
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
char buf[40]; // For string to byte array convertor

void convert(char *s)
{
 int i, j, k;
 buf[0] = 0x0;

 for (j = 0, i = 0, k = 0; j < strlen(s); j++)
 {

   if (i++ == 0) {
     buf[k++] = '0';
     buf[k++] = 'x';
   }

   buf[k++] = s[j];

   if (i == 2) {
     if(j != strlen(s) -1)  buf[k++] = ',';
     i = 0;
   }
 }

buf[k] = 0x0;

}

void clone() {

 /* RFID Read */
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
  {
    return;
  }
  //Show UID on serial monitor
  Serial.println();
  Serial.print(" UID tag :");

  //  Very basic UID dump
  unsigned int hex_num;
  hex_num =  mfrc522.uid.uidByte[0] << 24;
  hex_num += mfrc522.uid.uidByte[1] << 16;
  hex_num += mfrc522.uid.uidByte[2] <<  8;
  hex_num += mfrc522.uid.uidByte[3];

  // Get UID
  int  NFC_id = (int)hex_num;
  Serial.print(NFC_id, HEX);

  // Convert UID to string using an int and a base (hexadecimal)
  String stringUID =  String(NFC_id, HEX);
  char UIDChar[10];
  stringUID.toCharArray(UIDChar,10);

  delay(1000);
  Serial.println();


  // Convert to uppercase
  for (int i = 0; i < strlen(UIDChar); i++ )
  {
    if ( UIDChar[i] == NULL ) break;
    UIDChar[i] = toupper(UIDChar[i]);
  }

  //Serial.print( &UIDChar[0] );
  Serial.println();
  convert(UIDChar);
  Serial.println(buf);

   /* RFID Write */

   // Set new UID
   //  Change your UID hex string to 4 byte array

   // I get error if I use byte newUid[] = {buf};
 /*  ERROR HERE */
  byte newUid[] = {0x46,0x7E,0xE9,0xA9};
  if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
    Serial.println( "Wrote new UID to card." );
  }

  // Halt PICC and re-select it so DumpToSerial doesn't get confused
  mfrc522.PICC_HaltA();
  if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
    return;
  }

  // Dump the new memory contents
  Serial.println( "New UID and contents:" );
  mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

}

void setup() {

  Serial.begin ( 115200 );

  /* RFID */
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522

  clone();

}

void loop() {

} 
c arduino arduino-esp8266
1个回答
0
投票

写作时

byte newUid[] = {buf};

您正在尝试使用单个元素(newUid中只有一个项目)来初始化{},并且该元素是buf,即char*(或char[])。这就是为什么会出现错误的原因-您试图将一个具有char*的数组分配给一个元素为byte的变量。]​​>

没有详细阅读完整的代码,我不知道您为什么要执行此分配,而不是直接使用buf数组。但是要解决此问题,您可能只想使用

byte* newUid = buf;
© www.soinside.com 2019 - 2024. All rights reserved.