RFID名稱:YHY502CTG 13.56MHz的RFID的Mifare讀/寫模塊試圖將命令寫入到它後讀取RFID讀者的反應,而是得到一個空的響應
我試圖寫該命令「AA BB 02 20 22 「到RFID,作爲迴應我會得到
1) if no card is swiped == "AA BB 02 DF DD" .
Where "AA BB" == Head of this DATA ,
02 == Length of this DATA,
"DF" == One's complement of COMMAND,
02⊕DF(XOR) == "DD"
2) if there is card swipe == "AA BB 06 20 A0 8C 92 54 CC"
where "AA BB" == Head of this DATA ,
06 == Length of this DATA,
20 == COMMAND,
"A0 8C 92 54" == Card Serial Number,
06⊕20⊕A0⊕8C⊕92⊕54(XOR) == "CC".
CODE:-
byte[] bytesToSend = new byte[10] { 0x41, 0x41, 0x42, 0x42, 0x30, 0x32, 0x32, 0x30, 0x32, 0x32 };
_comport.Write(bytesToSend,0,10);// dont know whether ----correct way to write , but working properly//
System.Threading.Thread.Sleep(1000);
var data1 = _comport.ReadExisting();//getting an empty response
MessageBox.Show(data1);
您可能沒有足夠長時間回覆。您可以訂閱「SerialPort.DataReceived」事件以確保您獲取數據並等待響應時間足夠長。 – KDecker
嗨@KDecker,我試過寫System.Threading.Thread.Sleep(5000);在閱讀使用ReadExisting()之前,我也嘗試過var data1 =「」; (data1.Length <4) { data1 = _comport.ReadExisting(); } --- //但它進入無限循環,因爲響應是空的。 –
您需要掛鉤到DataReceived事件本身並編寫適當的處理程序。就像'RFIDDataReceivded + = MySerialPortObj.DataReceived'。然後執行'RFIDDataReceived'並讀取緩衝區中現有的字節/字符串。 //如果這個處理程序沒有被調用,那麼最有可能與您的設備連接有問題。 – KDecker