2

我正在将此RFID模块用于 Arduino 以太网 R3,我需要从软件序列中检索写在标签外部的卡 ( TAG ) ID。该模块的数据表显示 14 个字节被发送到 Arduino。第一个是标头,最后一个是页脚,页脚前的 2 个字节是校验和,其他 10 个字节是包含标签 ID 的 ASCII 数据。

如何重新创建卡的 ID,并控制校验和?例如,对于具有此 ID:0013530444 的标签,Arduino 响应为:

I received: 2
I received: 51
I received: 67
I received: 48
I received: 48
I received: 67
I received: 69
I received: 55
I received: 53
I received: 52
I received: 67
I received: 67
I received: 66
I received: 3

但我不知道如何在屏幕上打印 Arduino 读取的 ID。如何计算校验和

http://www.seeedstudio.com/wiki/index.php?title=125Khz_RFID_module_-_UART

谁能帮我?

4

2 回答 2

3

这是如何计算校验和的演练。

拿你的卡号(这只是直接从你的文字中引用的)

I received: 2
I received: 51
I received: 67
I received: 48
I received: 48
I received: 67
I received: 69
I received: 55
I received: 53
I received: 52
I received: 67
I received: 67
I received: 66
I received: 3

这将为您提供一个等于以下内容的数字:

2 51 67 48 48 67 69 55 53 52 67 67 66 3

第一个数字 (2) 表示这是请求的开始。

最后一个数字 (3) 表示这是请求的结束。

2 51 67 48 48 67 69 55 53 52 67 67 66 3

为了计算校验和,我们将删除这两个数字。所以你的新号码现在是:

51 67 48 48 67 69 55 53 52 67 67 66

您拥有的最后两个数字是您的校验和。其余数字是您的卡号。所以:

您的卡号是:

51 67 48 48 67 69 55 53 52 67

你的校验和是:

67 66

接下来,您需要将您的卡号和校验和转换为 ASCII 值:

您的卡号是:

3 C 0 0 CE 7 5 4 C

你的校验和是:

CB

接下来,将每个数字成对:

您的卡号是:

3C 00 CE 75 4C

你的校验和是:

CB

然后,您需要将每一对视为 HEXIDECIMAL 值并对它们进行 XOR。所以基本上你需要证明以下几点:

3C ^ 00 ^ CE ^ 75 ^ 4C == CB

(3C ^ 00) = 3C

3C ^ CE ^ 75 ^ 4C == CB

(3C ^ CE) = F2

F2 ^ 75 ^ 4C == CB

(3C ^ CE) = 87

87 ^ 4C == CB

(87 ^ 4C) = CB

CB == CB

因为 CB == CB,所以这是一个有效的交易。

毫无疑问,其他人可以提出比这更好的方法,但是这里应该有足够的伪代码供您自己编写。

于 2012-10-04T17:21:29.477 回答
0

我发现这个博客在 Arduino 中有一个实现,我已经对其进行了调整Java,结果很好。由于有很多按位运算 - 我使用http://www.miniwebtool.com/bitwise-calculator/试图了解发生了什么。我理解所有这一切,除了(val | (tempbyte << 4)),我的意思是我理解这句话的作用,我只是很难看到它是如何产生我想要的结果的。

void loop () {
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(Serial.available() > 0) {
    if((val = Serial.read()) == 2) {
      // check for header 
      bytesread = 0; 
      while (bytesread < 12) {
        // read 10 digit code + 2 digit checksum
        if( Serial.available() > 0) {
          val = Serial.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) {
            // if header or stop bytes before the 10 digit reading
            break;
            // stop reading
          }
          // Do Ascii/Hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
          } else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
          }
          // Every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));
            if (bytesread >> 1 != 5) {
              // If we're at the checksum byte,
              checksum ^= code[bytesread >> 1];
              // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;
            // Store the first hex digit first...
          };
          bytesread++;
          // ready to read next digit
        }
      }
      // Output to Serial:
      if (bytesread == 12) {
        // removed code for clarity
        LCD.print("Check:");
        LCD.print(code[5], HEX);
        LCD.print(code[5] == checksum ? "-passed" : "-error");
      }
      bytesread = 0;
    }
  }
}

我的 Java/Android 端口正在侦听BluetoothSocket. 我使用BlueTerm的代码作为基础,此代码位于ConnectedThread. 为所有愚蠢的评论道歉,但我仍在学习 Java)。

//assume you have checksum as int and code as int array. it will overflow if bytes are used like above example
public void run() {
  Log.d(TAG, "BEGIN mConnectedThread");
  byte[] buffer = new byte[1024];
  int bytes;
  // Keep listening to the InputStream while connected
  while (true) {
    Log.d(TAG, "Running");
    try {
      // Read from the InputStream
      bytes = mmInStream.read(buffer);
      for (int i = 0; i < bytes; i++) {
        Log.d(TAG, "Reading: " + i + " of " + bytes + " from input stream");
        byte b = buffer[i];
        try {
          if(bytesread >= 0) {
            //just printing ASCII
            char printableB = (char) b;
            if (b < 32 || b > 126) printableB = ' ';
            Log.d(TAG, "'" + Character.toString(printableB) + "' (" + Integer.toString(b) + ")");
            if((b == 0x0D)||(b == 0x0A)||(b == 0x03)||(b == 0x02)) {
              // if header or stop bytes before the 10 digit reading
              Log.e(TAG, i + " Unexpected header while processing character " + Integer.toString(b));
            } else {
              // Do ASCII/Hex conversion
              if ((b >= '0') && (b <= '9')) {
                b = (byte) (b - '0');
              } else if ((b >= 'A') && (b <= 'F')) {
                b = (byte) (10 + b - 'A');
              }
              if ((bytesread & 1) == 1) {
                //if isOdd(bytesread)
              // make some space for this hex-digit by shifting the previous hex-digit with 4 bits to the left:
              code[bytesread >> 1] = (b | (tempbyte << 4));
              if (bytesread >> 1 != 5) {
                // If we're not at the checksum byte,
                checksum ^= code[bytesread >> 1];
                // Calculate the checksum... (XOR)
              }
            } else {
              // Store the first hex digit first
              tempbyte = b; 
            }
          }
          bytesread++;
        } else if(b == 2) {
          bytesread = 0;
          Log.d(TAG, "Header found!");
        }
        if(bytesread == 12) {
          String check = (code[5] == checksum ? "-passed" : "-error");
          String r = "";
          for(int j = 0; j < 5; j++){
            r += Integer.toString(code[i]);
          }
          Log.d(TAG, "Check:" + Integer.toString(code[5]) + check);
          init();
        } else if(bytesread > 12){
          Log.e(TAG, "Too many bytes!");
        }
      } catch (Exception e) {
        Log.e(TAG, i + " Exception while processing character " + Integer.toString(b), e);
      }
    }
    String a = buffer.toString();
    a = "";
  } catch (IOException e) {
    Log.e(TAG, "disconnected", e);
    connectionLost();
    break;
  }
}
Log.i(TAG, "END mConnectedThread");
}
于 2013-08-31T16:01:29.110 回答