由於我使用的讀卡器,檢索MIFARE卡的序列號的協議如下:獲取MIFARE卡的序列號不正確
的Mifare防撞,
0x0202
:
功能卡的防衝突
格式aa bb 05 00 00 00 02 02 00
響應aa bb 0a 00 52 51 02 02 00 46 ff a6 b8 a4
其中46 ff a6 b8
是上述響應中的卡序列號。
我實現在C#這個協議如下:
private SerialPort _serialPort = new SerialPort();
private string _receivedData = null;
public MifareCardReader(string comPort, int baudRate)
{
_serialPort = new SerialPort();
_serialPort.PortName = comPort;
_serialPort.BaudRate = baudRate;
_serialPort.DataBits = 8;
_serialPort.Parity = Parity.None;
_serialPort.StopBits = StopBits.One;
_serialPort.Handshake = Handshake.None;
_serialPort.Open();
// Add event
_serialPort.DataReceived += SerialPort_DataReceived;
}
public string MifareAnticollision()
{
if (_serialPort != null && _serialPort.IsOpen)
{
string message = "AABB050000000202000D";
byte[] data = StringToByteArray(message);
_serialPort.Write(data, 0, data.Length);
}
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
_receivedData += _serialPort.ReadExisting();
byte[] data = Encoding.ASCII.GetBytes(receivedData);
if (data.Length >= 9)
{
if (data[8] == 0) // OK
{
// Response data is complete
if (data.Length == 14)
{
StringBuilder hex = new StringBuilder(8);
hex.AppendFormat("{0:X2}", data[9]);
hex.AppendFormat("{0:X2}", data[10]);
hex.AppendFormat("{0:X2}", data[11]);
hex.AppendFormat("{0:X2}", data[12]);
string cardID = hex.ToString();
_receivedData = string.Empty;
}
}
else // fail
{
_receivedData = string.Empty;
}
}
}
我用3個不同MIFARE卡測試這一點,但是,輸出是不符合期望:
- 卡1:接收到:
3F463F3F
,預計:974682D6
- 卡2:收到:
3F450B3F
,預計:EA450B91
- Card 3:received:
070D3F3F
,expected:070DEBD6
需要更改哪些內容才能獲得正確的輸出?
但你有什麼問題嗎? –
我沒有完全按照協議,但沒有得到正確的卡片序列號。我不知道爲什麼。我的算法錯誤或協議丟失? –
確定,所以你有不良的輸出,給我你的輸入十六進制值和ü想,我會解決它的ü:) Card3輸出的3個例子:070D3F3F是壞的輸出?或者它是輸入? –