I used raspberry pi 3 and Windows IoT build an RFID reader. When I copy this lib RFID RC522 Raspberry PI 2 Windows IOT and make new class for this lib. I have some problem.
In original namespace, I add a new class 'startmfrc522'.
public class startmfrc522 {
Mfrc522 mfrc = new Mfrc522();
public async Task<bool> start() {
System.Diagnostics.Debug.WriteLine("start RFID");
await mfrc.InitIO();
System.Diagnostics.Debug.WriteLine("finish Init");
return true;
}
public async Task<string> readtag() {
for (int i = 0; i < 5; i++) {
System.Diagnostics.Debug.WriteLine("start read tag");
if (mfrc.IsTagPresent()) {
System.Diagnostics.Debug.WriteLine("success");
string uid = mfrc.ReadUid().ToString();
mfrc.HaltTag();
return uid;
}
else {
mfrc.HaltTag();
return "fail";
}
await Task.Delay(1000);
System.Diagnostics.Debug.WriteLine("Delay for 1 s");
}
return "fail";
}
}
In the mainpage, I use this code to start the rfid reader and read tag
startmfrc522 rfid = new startmfrc522();
while (!rfid.start().Result){
System.Diagnostics.Debug.Write(".");
}
System.Diagnostics.Debug.WriteLine(rfid.readtag());
First problem is VS 2015 told me use 'bool x = await start()', I change it to 'bool result = await start()', still not working.
Second problem is I test read the tag, looks like mfrc.InitIO not finish and immediately read the tag. But I add the 'async' and 'await', shouldn't it will wait the InitIO finish the work and read the tag?
Third problem is 'Task.Sleep' not working, I think it should be wait for five times and total wait for 5s.
I think I'm not really understand the 'await' and 'async'. Hope someone could tell me what is it and how to change my code to work!