MIFARE Communication with c#

Unknown | 2:17 AM |

byte []autoscan=new byte[]{0x01, 0x00, 0x00, 0x00, 0xc0, 0x71,0x01, 0x00, 0x01, 0x00, 0x50, 0x70,0x01, 0x00, 0x3f, 0x00, 0x30, 0x60 };
byte[] crctable = new byte[512];
byte a, b,aa,ba;
string j=null, r=null, n=null,s;
byte[] recv3 = new byte[50];
int i,r1=0,k = 0,v=0,q=0,ka=0,vy=0,bu=0,bankupdate=0,flag=0,z=0,l;
int m = 0,w=0;
string p, t;
int result = 0;
byte[] crc = new byte[8];
string user;
MySqlCommand cmd;
MySqlConnection cn;
MySqlDataReader rd;
byte str1, str2;
string c, d, e, f,nn;
public Form3(string username )
{
user = username.ToString();
InitializeComponent();
crctable[0] = 0x0;
crctable[1] = 0xC1;
crctable[2] = 0x81;
crctable[3] = 0x40;
crctable[4] = 0x1;
crctable[5] = 0xC0;
crctable[6] = 0x80;
crctable[7] = 0x41;
crctable[8] = 0x1;
crctable[9] = 0xC0;
crctable[10] = 0x80;
crctable[11] = 0x41;
crctable[12] = 0x0;
crctable[13] = 0xC1;
crctable[14] = 0x81;
crctable[15] = 0x40;
crctable[16] = 0x1;
crctable[17] = 0xC0;
crctable[18] = 0x80;
crctable[19] = 0x41;
crctable[20] = 0x0;
crctable[21] = 0xC1;
crctable[22] = 0x81;
crctable[23] = 0x40;
crctable[24] = 0x0;
crctable[25] = 0xC1;
crctable[26] = 0x81;
crctable[27] = 0x40;
crctable[28] = 0x1;
crctable[29] = 0xC0;
crctable[30] = 0x80;
crctable[31] = 0x41;
crctable[32] = 0x1;
crctable[33] = 0xC0;
crctable[34] = 0x80;
crctable[35] = 0x41;
crctable[36] = 0x0;
crctable[37] = 0xC1;
crctable[38] = 0x81;
crctable[39] = 0x40;
crctable[40] = 0x0;
crctable[41] = 0xC1;
crctable[42] = 0x81;
crctable[43] = 0x40;
crctable[44] = 0x1;
crctable[45] = 0xC0;
crctable[46] = 0x80;
crctable[47] = 0x41;
crctable[48] = 0x0;
crctable[49] = 0xC1;
crctable[50] = 0x81;
crctable[51] = 0x40;
crctable[52] = 0x1;
crctable[53] = 0xC0;
crctable[54] = 0x80;
crctable[55] = 0x41;
crctable[56] = 0x1;
crctable[57] = 0xC0;
crctable[58] = 0x80;
crctable[59] = 0x41;
crctable[60] = 0x0;
crctable[61] = 0xC1;
crctable[62] = 0x81;
crctable[63] = 0x40;
crctable[64] = 0x1;
crctable[65] = 0xC0;
crctable[66] = 0x80;
crctable[67] = 0x41;
crctable[68] = 0x0;
crctable[69] = 0xC1;
crctable[70] = 0x81;
crctable[71] = 0x40;
crctable[72] = 0x0;
crctable[73] = 0xC1;
crctable[74] = 0x81;
crctable[75] = 0x40;
crctable[76] = 0x1;
crctable[77] = 0xC0;
crctable[78] = 0x80;
crctable[79] = 0x41;
crctable[80] = 0x0;
crctable[81] = 0xC1;
crctable[82] = 0x81;
crctable[83] = 0x40;
crctable[84] = 0x1;
crctable[85] = 0xC0;
crctable[86] = 0x80;
crctable[87] = 0x41;
crctable[88] = 0x1;
crctable[89] = 0xC0;
crctable[90] = 0x80;
crctable[91] = 0x41;
crctable[92] = 0x0;
crctable[93] = 0xC1;
crctable[94] = 0x81;
crctable[95] = 0x40;
crctable[96] = 0x0;
crctable[97] = 0xC1;
crctable[98] = 0x81;
crctable[99] = 0x40;
crctable[100] = 0x1;
crctable[101] = 0xC0;
crctable[102] = 0x80;
crctable[103] = 0x41;
crctable[104] = 0x1;
crctable[105] = 0xC0;
crctable[106] = 0x80;
crctable[107] = 0x41;
crctable[108] = 0x0;
crctable[109] = 0xC1;
crctable[110] = 0x81;
crctable[111] = 0x40;
crctable[112] = 0x1;
crctable[113] = 0xC0;
crctable[114] = 0x80;
crctable[115] = 0x41;
crctable[116] = 0x0;
crctable[117] = 0xC1;
crctable[118] = 0x81;
crctable[119] = 0x40;
crctable[120] = 0x0;
crctable[121] = 0xC1;
crctable[122] = 0x81;
crctable[123] = 0x40;
crctable[124] = 0x1;
crctable[125] = 0xC0;
crctable[126] = 0x80;
crctable[127] = 0x41;
crctable[128] = 0x1;
crctable[129] = 0xC0;
crctable[130] = 0x80;
crctable[131] = 0x41;
crctable[132] = 0x0;
crctable[133] = 0xC1;
crctable[134] = 0x81;
crctable[135] = 0x40;
crctable[136] = 0x0;
crctable[137] = 0xC1;
crctable[138] = 0x81;
crctable[139] = 0x40;
crctable[140] = 0x1;
crctable[141] = 0xC0;
crctable[142] = 0x80;
crctable[143] = 0x41;
crctable[144] = 0x0;
crctable[145] = 0xC1;
crctable[146] = 0x81;
crctable[147] = 0x40;
crctable[148] = 0x1;
crctable[149] = 0xC0;
crctable[150] = 0x80;
crctable[151] = 0x41;
crctable[152] = 0x1;
crctable[153] = 0xC0;
crctable[154] = 0x80;
crctable[155] = 0x41;
crctable[156] = 0x0;
crctable[157] = 0xC1;
crctable[158] = 0x81;
crctable[159] = 0x40;
crctable[160] = 0x0;
crctable[161] = 0xC1;
crctable[162] = 0x81;
crctable[163] = 0x40;
crctable[164] = 0x1;
crctable[165] = 0xC0;
crctable[166] = 0x80;
crctable[167] = 0x41;
crctable[168] = 0x1;
crctable[169] = 0xC0;
crctable[170] = 0x80;
crctable[171] = 0x41;
crctable[172] = 0x0;
crctable[173] = 0xC1;
crctable[174] = 0x81;
crctable[175] = 0x40;
crctable[176] = 0x1;
crctable[177] = 0xC0;
crctable[178] = 0x80;
crctable[179] = 0x41;
crctable[180] = 0x0;
crctable[181] = 0xC1;
crctable[182] = 0x81;
crctable[183] = 0x40;
crctable[184] = 0x0;
crctable[185] = 0xC1;
crctable[186] = 0x81;
crctable[187] = 0x40;
crctable[188] = 0x1;
crctable[189] = 0xC0;
crctable[190] = 0x80;
crctable[191] = 0x41;
crctable[192] = 0x0;
crctable[193] = 0xC1;
crctable[194] = 0x81;
crctable[195] = 0x40;
crctable[196] = 0x1;
crctable[197] = 0xC0;
crctable[198] = 0x80;
crctable[199] = 0x41;
crctable[200] = 0x1;
crctable[201] = 0xC0;
crctable[202] = 0x80;
crctable[203] = 0x41;
crctable[204] = 0x0;
crctable[205] = 0xC1;
crctable[206] = 0x81;
crctable[207] = 0x40;
crctable[208] = 0x1;
crctable[209] = 0xC0;
crctable[210] = 0x80;
crctable[211] = 0x41;
crctable[212] = 0x0;
crctable[213] = 0xC1;
crctable[214] = 0x81;
crctable[215] = 0x40;
crctable[216] = 0x0;
crctable[217] = 0xC1;
crctable[218] = 0x81;
crctable[219] = 0x40;
crctable[220] = 0x1;
crctable[221] = 0xC0;
crctable[222] = 0x80;
crctable[223] = 0x41;
crctable[224] = 0x1;
crctable[225] = 0xC0;
crctable[226] = 0x80;
crctable[227] = 0x41;
crctable[228] = 0x0;
crctable[229] = 0xC1;
crctable[230] = 0x81;
crctable[231] = 0x40;
crctable[232] = 0x0;
crctable[233] = 0xC1;
crctable[234] = 0x81;
crctable[235] = 0x40;
crctable[236] = 0x1;
crctable[237] = 0xC0;
crctable[238] = 0x80;
crctable[239] = 0x41;
crctable[240] = 0x0;
crctable[241] = 0xC1;
crctable[242] = 0x81;
crctable[243] = 0x40;
crctable[244] = 0x1;
crctable[245] = 0xC0;
crctable[246] = 0x80;
crctable[247] = 0x41;
crctable[248] = 0x1;
crctable[249] = 0xC0;
crctable[250] = 0x80;
crctable[251] = 0x41;
crctable[252] = 0x0;
crctable[253] = 0xC1;
crctable[254] = 0x81;
crctable[255] = 0x40;
crctable[256] = 0x0;
crctable[257] = 0xC0;
crctable[258] = 0xC1;
crctable[259] = 0x1;
crctable[260] = 0xC3;
crctable[261] = 0x3;
crctable[262] = 0x2;
crctable[263] = 0xC2;
crctable[264] = 0xC6;
crctable[265] = 0x6;
crctable[266] = 0x7;
crctable[267] = 0xC7;
crctable[268] = 0x5;
crctable[269] = 0xC5;
crctable[270] = 0xC4;
crctable[271] = 0x4;
crctable[272] = 0xCC;
crctable[273] = 0xC;
crctable[274] = 0xD;
crctable[275] = 0xCD;
crctable[276] = 0xF;
crctable[277] = 0xCF;
crctable[278] = 0xCE;
crctable[279] = 0xE;
crctable[280] = 0xA;
crctable[281] = 0xCA;
crctable[282] = 0xCB;
crctable[283] = 0xB;
crctable[284] = 0xC9;
crctable[285] = 0x9;
crctable[286] = 0x8;
crctable[287] = 0xC8;
crctable[288] = 0xD8;
crctable[289] = 0x18;
crctable[290] = 0x19;
crctable[291] = 0xD9;
crctable[292] = 0x1B;
crctable[293] = 0xDB;
crctable[294] = 0xDA;
crctable[295] = 0x1A;
crctable[296] = 0x1E;
crctable[297] = 0xDE;
crctable[298] = 0xDF;
crctable[299] = 0x1F;
crctable[300] = 0xDD;
crctable[301] = 0x1D;
crctable[302] = 0x1C;
crctable[303] = 0xDC;
crctable[304] = 0x14;
crctable[305] = 0xD4;
crctable[306] = 0xD5;
crctable[307] = 0x15;
crctable[308] = 0xD7;
crctable[309] = 0x17;
crctable[310] = 0x16;
crctable[311] = 0xD6;
crctable[312] = 0xD2;
crctable[313] = 0x12;
crctable[314] = 0x13;
crctable[315] = 0xD3;
crctable[316] = 0x11;
crctable[317] = 0xD1;
crctable[318] = 0xD0;
crctable[319] = 0x10;
crctable[320] = 0xF0;
crctable[321] = 0x30;
crctable[322] = 0x31;
crctable[323] = 0xF1;
crctable[324] = 0x33;
crctable[325] = 0xF3;
crctable[326] = 0xF2;
crctable[327] = 0x32;
crctable[328] = 0x36;
crctable[329] = 0xF6;
crctable[330] = 0xF7;
crctable[331] = 0x37;
crctable[332] = 0xF5;
crctable[333] = 0x35;
crctable[334] = 0x34;
crctable[335] = 0xF4;
crctable[336] = 0x3C;
crctable[337] = 0xFC;
crctable[338] = 0xFD;
crctable[339] = 0x3D;
crctable[340] = 0xFF;
crctable[341] = 0x3F;
crctable[342] = 0x3E;
crctable[343] = 0xFE;
crctable[344] = 0xFA;
crctable[345] = 0x3A;
crctable[346] = 0x3B;
crctable[347] = 0xFB;
crctable[348] = 0x39;
crctable[349] = 0xF9;
crctable[350] = 0xF8;
crctable[351] = 0x38;
crctable[352] = 0x28;
crctable[353] = 0xE8;
crctable[354] = 0xE9;
crctable[355] = 0x29;
crctable[356] = 0xEB;
crctable[357] = 0x2B;
crctable[358] = 0x2A;
crctable[359] = 0xEA;
crctable[360] = 0xEE;
crctable[361] = 0x2E;
crctable[362] = 0x2F;
crctable[363] = 0xEF;
crctable[364] = 0x2D;
crctable[365] = 0xED;
crctable[366] = 0xEC;
crctable[367] = 0x2C;
crctable[368] = 0xE4;
crctable[369] = 0x24;
crctable[370] = 0x25;
crctable[371] = 0xE5;
crctable[372] = 0x27;
crctable[373] = 0xE7;
crctable[374] = 0xE6;
crctable[375] = 0x26;
crctable[376] = 0x22;
crctable[377] = 0xE2;
crctable[378] = 0xE3;
crctable[379] = 0x23;
crctable[380] = 0xE1;
crctable[381] = 0x21;
crctable[382] = 0x20;
crctable[383] = 0xE0;
crctable[384] = 0xA0;
crctable[385] = 0x60;
crctable[386] = 0x61;
crctable[387] = 0xA1;
crctable[388] = 0x63;
crctable[389] = 0xA3;
crctable[390] = 0xA2;
crctable[391] = 0x62;
crctable[392] = 0x66;
crctable[393] = 0xA6;
crctable[394] = 0xA7;
crctable[395] = 0x67;
crctable[396] = 0xA5;
crctable[397] = 0x65;
crctable[398] = 0x64;
crctable[399] = 0xA4;
crctable[400] = 0x6C;
crctable[401] = 0xAC;
crctable[402] = 0xAD;
crctable[403] = 0x6D;
crctable[404] = 0xAF;
crctable[405] = 0x6F;
crctable[406] = 0x6E;
crctable[407] = 0xAE;
crctable[408] = 0xAA;
crctable[409] = 0x6A;
crctable[410] = 0x6B;
crctable[411] = 0xAB;
crctable[412] = 0x69;
crctable[413] = 0xA9;
crctable[414] = 0xA8;
crctable[415] = 0x68;
crctable[416] = 0x78;
crctable[417] = 0xB8;
crctable[418] = 0xB9;
crctable[419] = 0x79;
crctable[420] = 0xBB;
crctable[421] = 0x7B;
crctable[422] = 0x7A;
crctable[423] = 0xBA;
crctable[424] = 0xBE;
crctable[425] = 0x7E;
crctable[426] = 0x7F;
crctable[427] = 0xBF;
crctable[428] = 0x7D;
crctable[429] = 0xBD;
crctable[430] = 0xBC;
crctable[431] = 0x7C;
crctable[432] = 0xB4;
crctable[433] = 0x74;
crctable[434] = 0x75;
crctable[435] = 0xB5;
crctable[436] = 0x77;
crctable[437] = 0xB7;
crctable[438] = 0xB6;
crctable[439] = 0x76;
crctable[440] = 0x72;
crctable[441] = 0xB2;
crctable[442] = 0xB3;
crctable[443] = 0x73;
crctable[444] = 0xB1;
crctable[445] = 0x71;
crctable[446] = 0x70;
crctable[447] = 0xB0;
crctable[448] = 0x50;
crctable[449] = 0x90;
crctable[450] = 0x91;
crctable[451] = 0x51;
crctable[452] = 0x93;
crctable[453] = 0x53;
crctable[454] = 0x52;
crctable[455] = 0x92;
crctable[456] = 0x96;
crctable[457] = 0x56;
crctable[458] = 0x57;
crctable[459] = 0x97;
crctable[460] = 0x55;
crctable[461] = 0x95;
crctable[462] = 0x94;
crctable[463] = 0x54;
crctable[464] = 0x9C;
crctable[465] = 0x5C;
crctable[466] = 0x5D;
crctable[467] = 0x9D;
crctable[468] = 0x5F;
crctable[469] = 0x9F;
crctable[470] = 0x9E;
crctable[471] = 0x5E;
crctable[472] = 0x5A;
crctable[473] = 0x9A;
crctable[474] = 0x9B;
crctable[475] = 0x5B;
crctable[476] = 0x99;
crctable[477] = 0x59;
crctable[478] = 0x58;
crctable[479] = 0x98;
crctable[480] = 0x88;
crctable[481] = 0x48;
crctable[482] = 0x49;
crctable[483] = 0x89;
crctable[484] = 0x4B;
crctable[485] = 0x8B;
crctable[486] = 0x8A;
crctable[487] = 0x4A;
crctable[488] = 0x4E;
crctable[489] = 0x8E;
crctable[490] = 0x8F;
crctable[491] = 0x4F;
crctable[492] = 0x8D;
crctable[493] = 0x4D;
crctable[494] = 0x4C;
crctable[495] = 0x8C;
crctable[496] = 0x44;
crctable[497] = 0x84;
crctable[498] = 0x85;
crctable[499] = 0x45;
crctable[500] = 0x87;
crctable[501] = 0x47;
crctable[502] = 0x46;
crctable[503] = 0x86;
crctable[504] = 0x82;
crctable[505] = 0x42;
crctable[506] = 0x43;
crctable[507] = 0x83;
crctable[508] = 0x41;
crctable[509] = 0x81;
crctable[510] = 0x80;
crctable[511] = 0x40;

}
public int crc16(byte[] modbusframe, int Length)
{

int i;
int index;
int crc_Low = 0xFF;
int crc_High = 0xFF;

for (i = 0; i < Length; i++)
{
index = crc_High ^ (char)modbusframe[i];
crc_High = crc_Low ^ crctable[index];
crc_Low = (byte)crctable[index + 256];
}

return crc_High * 256 + crc_Low;
}
private void card(object sender, EventArgs e)
{
MessageBox.Show("place the card");
}
private void cardid(object sender, EventArgs e)
{
textBox2.Text = c + d +nn+ f;
}
private void balance(object sender, EventArgs e)
{
label1.Visible = true;
label1.Text = "Your Current Balance in Card is:" + result.ToString();

}
private void Form3_Load(object sender, EventArgs e)
{
serialPort1.Open();


serialPort1.Write(autoscan, 0, 18);



}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{

switch (z)
{
case 0:
{
Thread.Sleep(10);
byte[] recv = new byte[25];
serialPort1.Read(recv, 0, 25);
Thread.Sleep(1000);
k = Convert.ToInt32(recv[2]);
if (k == 6)
{
byte[] ok = new byte[] { 0x01, 0x00, 0x3f, 0x01, 0x00, 0xb8, 0x31 };
serialPort1.DiscardInBuffer();
serialPort1.Write(ok, 0, 7);
Thread.Sleep(1000);
z++;
}
else
{

byte[] autoscan = new byte[] { 0x01, 0x00, 0x00, 0x00, 0xc0, 0x71, 0x01, 0x00, 0x01, 0x00, 0x50, 0x70, 0x01, 0x00, 0x3f, 0x00, 0x30, 0x60 };
serialPort1.Write(autoscan, 0, 18);
z = 0;
}
break;
}

case 1:
{
byte[] resp_ok = new byte[50];
Thread.Sleep(1000);
serialPort1.Read(resp_ok, 0, 10);
Thread.Sleep(1000);
k = Convert.ToInt32(resp_ok[2]);
if (k == 6)
{
MessageBox.Show("Place your card");
byte[] request = new byte[] { 0x01, 0x00, 0x20, 0x00, 0x00, 0x68 };
serialPort1.DiscardInBuffer();
serialPort1.Write(request, 0, 6);
Thread.Sleep(1000);
z++;
}
else
{

byte[] autoscan = new byte[] { 0x01, 0x00, 0x00, 0x00, 0xc0, 0x71, 0x01, 0x00, 0x01, 0x00, 0x50, 0x70, 0x01, 0x00, 0x3f, 0x00, 0x30, 0x60 };
serialPort1.Write(autoscan, 0, 18);
z = 0;
}
break;
}
case 2:
{
byte[] recv2 = new byte[8];
serialPort1.Read(recv2, 0, 8);
Thread.Sleep(1000);
k = Convert.ToInt32(recv2[2]);

if (k == 6)
{
MessageBox.Show("card ok");
byte[] cardid = new byte[] { 0x01, 0x00, 0x21, 0x00, 0x90, 0x69 };
serialPort1.Write(cardid, 0, 6);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}
case 3:
{
byte[] recv3 = new byte[10];
serialPort1.Read(recv3, 0, 10);
k = Convert.ToInt32(recv3[2]);
if (k == 6)
{
int x = Convert.ToInt32(recv3[4]);
int y = Convert.ToInt32(recv3[5]);
int l = Convert.ToInt32(recv3[6]);
int n = Convert.ToInt32(recv3[7]);
c = x.ToString("X");
d = y.ToString("X");
nn = l.ToString("X");
f = n.ToString("X");
this.Invoke(new EventHandler(cardid));
Thread.Sleep(100);
byte[] crc = new byte[] { 0x00, 0x22, 0x04, recv3[4], recv3[5], recv3[6], recv3[7] };
byte[] crc_result = BitConverter.GetBytes(crc16(crc, 7));
byte[] send_cardid = new byte[] { 0x01, 0x00, 0x22, 0x04, recv3[4], recv3[5], recv3[6], recv3[7], crc_result[0], crc_result[1] };
serialPort1.DiscardInBuffer();
serialPort1.Write(send_cardid, 0, 10);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}

case 4:
{
byte[] recv4 = new byte[8];
serialPort1.Read(recv4, 0, 8);
k = Convert.ToInt32(recv4[2]);
if (k == 6)
{
byte[] authent = new byte[] { 0x01, 0x00, 0x23, 0x02, 0x60, 0x00, 0x44, 0xa6 };
serialPort1.DiscardInBuffer();
serialPort1.Write(authent, 0, 8);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}
case 5:
{
byte[] recv5 = new byte[8];
serialPort1.Read(recv5, 0, 8);
k = Convert.ToInt32(recv5[2]);

if (k == 6)
{
MessageBox.Show("card checked");
byte[] valueread = new byte[] { 0x01, 0x00, 0x27, 0x01, 0x01, 0x7f, 0x70 };
serialPort1.DiscardInBuffer();
serialPort1.Write(valueread, 0, 7);
Thread.Sleep(100);
z++;
}
else
{
z = 1;
}
break;
}
case 6:
{
byte[] recv6 = new byte[20];
serialPort1.Read(recv6, 0, 20);
k = Convert.ToInt32(recv6[2]);

if (k == 6)
{
m = Convert.ToInt32(recv6[6]);
w = Convert.ToInt32(recv6[7]);
result = w + (m * 255);
this.Invoke(new EventHandler(balance));
z++;
}
break;
}
case 7:
{
if (v == 1)
{
byte[] recv7 = new byte[29];
Thread.Sleep(100);
serialPort1.Read(recv7, 0, 29);
k = Convert.ToInt32(recv7[2]);

if (k == 6)
{

z++;
}
else
{


}

byte[] halt = new byte[] { 0x01, 0x00, 0x2a, 0x00, 0xa0, 0x6e };
serialPort1.Write(halt, 0, 6);
}
break;

}
case 8:
{
byte[] halt1 = new byte[20];
serialPort1.Read(halt1, 0, 20);
k = Convert.ToInt32(halt1[2]);

if (k == 6)
{


}
break;

}

}
}

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....