/** * * Método faz a gravação de uma mensagem em um bloco específico * no cartão. * * A mensagem que será gravada não deve ser superior a 16 bits. * * A leitura sempre será retornada em Bytes. * * @param bloco = número que bloco que deve ser lido * @param mensagem = número do setor existente dentro do bloco que será lido * * @throws IOException * * @return true = caso haja um erro na gravação, será tratada no throw * * */ public bool GravaSetorCartao(int bloco, byte[] mensagem) { try { // Valida a conexão com o cartão ValidConexaoCartao(); // Valida a quantidade de bytes que serão gravados if (mensagem.Length != 16) { throw new IllegalArgumentException("A mensagem não contem 16 bits"); } // Grava no cartão mifareClassic.WriteBlock(bloco, mensagem); } catch (IOException e) { throw new System.Exception(e.Message); } finally { DesconectaCartao(); this.GravaTempoFinal(); } return(true); }
public static void MifareTest() { using (var context = new NfcContext()) using (var device = context.OpenDevice()) // Try to open the NFC reader { MifareClassic mfc = new MifareClassic(device); mfc.InitialDevice(); mfc.SelectCard(); mfc.Authentication(0, KeyType.KeyA, 0xFFFFFFFFFFFFu); var block0 = mfc.ReadBlock(0); Program.PrintHex(block0, 16); mfc.Authentication(1, KeyType.KeyA, 0xFFFFFFFFFFFFu); mfc.WriteBlock(4, new byte[16] { 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }); var block4 = mfc.ReadBlock(4); Program.PrintHex(block4, 16); Console.ReadLine(); } }
public bool WriteByteBlock(int block, byte[] key, byte[] content, NfcKeyType nfcKeyType) { //TODO 4. MIFARE OPERATIONS 3 try { if (content?.Length != 16) { return(false); } //No card found if (!LoadCurrentMifareClassic()) { return(false); } //Different card ids if (!CheckCardId()) { return(false); } //No authenticated if (!AuthenticateSector(block, nfcKeyType, key)) { return(false); } currentMifareClassic.WriteBlock(block, content); return(true); } catch { return(false); } }
public void MifareClassic_WriteBlock(MifareClassic mfc, int Block, byte[] Data) { var tmp = new byte[16]; if (Data.Length >= 16) { for (int i = 0; i < 16; i++) { tmp[i] = Data[i]; } } else { for (int i = 0; i < Data.Length; i++) { tmp[i] = Data[i]; } for (int i = Data.Length; i < 16; i++) { tmp[i] = (byte)' '; } } mfc.WriteBlock(Block, tmp); }
/// <summary> /// 卸货写卡函数 /// </summary> private void UnloadFunction(Intent intent) { //业务类型置"1" //计量类型置"1" //计重方式置"1" //退货标识置"1" //写入卸货时间 bool result = true; if (ic.icserial != ic.FindIC(intent)) { result = false; CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true); return; } //取出封装在intent中的TAG var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; if (CommonFunction.mode == "NFC") { MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { try { #region 点击按钮的写卡方法 try { string pkidd = Convert.ToString(pkid); isOK = 1; string writedata = ""; if (pkidd.Length == 1) { writedata = Convert.ToString(isOK) + pkidd + "000000000000000000000000000000"; } else if (pkidd.Length == 2) { writedata = Convert.ToString(isOK) + pkidd + "00000000000000000000000000000"; } else if (pkidd.Length == 3) { writedata = Convert.ToString(isOK) + pkidd + "0000000000000000000000000000"; } bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(10, ic.ToDigitsBytes(writedata)); } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 点击按钮的写卡方法 //0业务类型 1计量类型 2计重方式 3 退货标识 // if (btGet.Checked) //收货 // { // #region 1-写入收发货标识 位于2区10块 [1,2] 第一位 // try // { // //string tmp = ic.IcDatas[0, 1].Remove(0, 1); // //string writedata = tmp.Insert(0, "1"); // // string writedata = ic.IcDatas[1, 2].Substring(0, 1) ; // string pkidd = Convert.ToString(pkid); // string writedata = ""; // if (pkidd.Length == 1) // { // writedata = Convert.ToString(isOK) + pkidd + "000000000000000000000000000000"; // } // else if (pkidd.Length == 2) // { // writedata = Convert.ToString(isOK) + pkidd + "00000000000000000000000000000"; // } // else if (pkidd.Length == 3) // { // writedata = Convert.ToString(isOK) + pkidd + "0000000000000000000000000000"; // } // bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); // if (auth) // { // mfc.WriteBlock(10, ic.ToDigitsBytes(writedata)); // } // } // catch (Java.IO.IOException ex) // { // result = false; // CommonFunction.ShowMessage(ex.Message, this, true); // } // #endregion //} #endregion } finally { mfc.Close(); } } } catch (Java.IO.IOException ex) { CommonFunction.ShowMessage("请放卡!", this, true); return; } } else { #region 判断卡类型 CommonFunction.ShowMessage("不支持该类型的卡", this, true); return; #endregion } } else if (CommonFunction.mode == "SerialPort") { // } WriteDataOK(result); }
/// <summary> /// 卸货写卡函数 /// </summary> private void UnloadFunction(Intent intent) { //业务类型置"1" //计量类型置"1" //计重方式置"1" //退货标识置"1" //写入卸货时间 bool result = true; if (ic.icserial != ic.FindIC(intent)) { result = false; CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true); return; } //取出封装在intent中的TAG //var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; if (CommonFunction.mode == "NFC") { MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { try { if (btSamp.Checked) //取样 { #region 写入取样编号 IcDatas[0,0] - 1扇区 4块 try { //当前时间 string xhdt = string.Format("{0:yyyyMMddhhmmss}", DateTime.Now); //写入取样编号 YYYYMMDD+0001 //sampCode = xhdt + serialNumber; // string writedata = xhdt + serialNumber + ic.IcDatas[0, 2].Substring(18); //写入取样卡标识 string writedata = "12018041100100000000000000000000"; //serialNumber = Convert.ToString( Convert.ToInt32(serialNumber)+1); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(4, ic.ToDigitsBytes(writedata)); } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion } } finally { mfc.Close(); } } } catch (Java.IO.IOException ex) { CommonFunction.ShowMessage("请放卡!", this, true); return; } } else { #region 判断卡类型 CommonFunction.ShowMessage("不支持该类型的卡", this, true); return; #endregion } } else if (CommonFunction.mode == "SerialPort") { // } WriteDataOK(result); }
/// <summary> /// 卸货写卡函数 /// </summary> private void UnloadFunction(Intent intent) { //业务类型置"1" //计量类型置"1" //计重方式置"1" //退货标识置"1" //写入卸货时间 bool result = true; if (ic.icserial != ic.FindIC(intent)) { result = false; CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true); return; } //取出封装在intent中的TAG var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; if (CommonFunction.mode == "NFC") { MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { try { //0业务类型 1计量类型 2计重方式 3 退货标识 if (bunload.Checked) //确认卸货 { #region 1-更改计量类型为1,位于1扇区5块第2位 try { //string tmp = ic.IcDatas[0, 1].Remove(0, 1); //string writedata = tmp.Insert(0, "1"); string writedata = ic.IcDatas[0, 1].Substring(0, 1) + "1" + ic.IcDatas[0, 1].Substring(2); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(5, ic.ToDigitsBytes(writedata)); } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位 try { string xhdt = string.Format("{0:yyMMddhhmmss}", DateTime.Now); EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight); string kd = buckleweight.Text.Trim().PadLeft(6, '0'); string writedata = xhdt + kd + ic.IcDatas[0, 2].Substring(18); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 3-写入卸货人名字,位于2扇区9块后16位 try { string tmp = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位 string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 4-取样类型位于3扇区12块前两位,如果取样类型为13,写入取样时间和取样人员,3扇区12块,2位取样代码,12位取样时间,8位取样人员名字 try { string quyangbaozhi = ic.IcDatas[2, 0].ToString().Substring(0, 2); if (quyangbaozhi == "13") { //判断取样代码。为"13"时,写入IC卡取样时间和取样人员(即货场操作人员);不为"13"时,不对IC卡操作。 string qydt = string.Format("{0:yyyyMMddhhmmss}", DateTime.Now); string writedata = quyangbaozhi + qydt + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(3, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(12, ic.ToDigitsBytes(writedata));//12位取样时间和16位卸货人员名字写入IC卡3扇区12块 } } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion } else if (bhalfreturn.Checked) //半车退货 { #region 1-更改计量类型为1,位于1扇区5块第2位;业务类型置"1",位于1扇区5块第1位;计重方式置"1",位于1扇区5块第3位;退货标识置"1",位于1扇区5块第4位; try { bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { string writedata = "1111" + ic.IcDatas[0, 1].Substring(4); mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));//将计量类型标识置为1,写入IC卡1扇区5块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位 try { string xhdt = string.Format("{0:yyMMddhhmmss}", DateTime.Now); EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight); string kd = buckleweight.Text.Trim().PadLeft(6, '0'); string writedata = xhdt + kd + ic.IcDatas[0, 2].Substring(18); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 3-写入卸货人名字,位于2扇区9块后16位 try { string tmp = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位 string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion } else if (ballreturn.Checked) //全车退货 { #region 1-更改计量类型为1,位于1扇区5块第2位;业务类型置"1",位于1扇区5块第1位;计重方式置"0",位于1扇区5块第3位;退货标识置"1",位于1扇区5块第4位; try { string writedata = "1101" + ic.IcDatas[0, 1].Substring(4); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));//将计量类型标识置为1,写入IC卡1扇区5块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位 try { string xhdt = string.Format("{0:yyMMddhhmmss}", DateTime.Now); EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight); string kd = buckleweight.Text.Trim().PadLeft(6, '0'); string writedata = xhdt + kd + ic.IcDatas[0, 2].Substring(18); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 3-写入卸货人名字,位于2扇区9块后16位 try { string tmp = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位 string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion } else if (bunloadandload.Checked) //卸后退装 { #region 1-更改计量类型为1,位于1扇区5块第2位;业务类型置"0",位于1扇区5块第1位;计重方式置"0",位于1扇区5块第3位;退货标识置"1",位于1扇区5块第4位; try { string writedata = "0101" + ic.IcDatas[0, 1].Substring(4); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(5, ic.ToDigitsBytes(writedata));//将计量类型标识置为1,写入IC卡1扇区5块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 2-写入12位卸货时间和6位扣吨,位于1扇区6块前16位 try { string xhdt = string.Format("{0:yyMMddhhmmss}", DateTime.Now); EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight); string kd = buckleweight.Text.Trim().PadLeft(6, '0'); string writedata = xhdt + kd + ic.IcDatas[0, 2].Substring(18); bool auth = mfc.AuthenticateSectorWithKeyB(1, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(6, ic.ToDigitsBytes(writedata));//12位卸货时间为当前时间,扣重如果有数据,按实际值;如果无数据,置0,写入IC卡1扇区6块。 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion #region 3-写入卸货人名字,位于2扇区9块后16位 try { string tmp = ic.IcDatas[1, 1].Remove(16); //获取2扇区9块前16位 string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块 } } catch (Java.IO.IOException ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } #endregion } } finally { mfc.Close(); } } } catch (Java.IO.IOException ex) { CommonFunction.ShowMessage("请放卡!", this, true); return; } } else { #region 判断卡类型 CommonFunction.ShowMessage("不支持该类型的卡", this, true); return; #endregion } } else if (CommonFunction.mode == "SerialPort") { // } WriteDataOK(result); }
/// <summary> /// 取样写入函数 /// </summary> private void QualityFunction(Intent intent) { bool result = true; if (ic.icserial != ic.FindIC(intent)) { result = false; CommonFunction.ShowMessage("要写入的卡和读取的卡不符,请检查是否被更换", this, true); return; } //取出封装在intent中的TAG var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; if (CommonFunction.mode == "NFC") { //写卡 MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(MifareClassic))) { try { if (afterquality.Checked) { #region 1-先写入操作人员名字,位于2扇区9块后16位 try { string tmp = ic.IcDatas[1, 1].Substring(0, 16); //获取2扇区9块前16位 string writedata = tmp + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(2, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(9, ic.ToDigitsBytes(writedata));//16位卸货人员名字写入IC卡2扇区9块 } else { result = false; } } catch (Exception ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); return; } #endregion #region 2-取样类型位于3扇区12块前两位,如果取样类型为12,写入取样时间和取样人员 3扇区12块 2位取样代码,12位取样时间,16位取样人员名字 try { //string quyangbiaozhi = ic.IcDatas[2, 0].ToString().Substring(0, 2); string quyangbiaozhi = ic.IcDatas[0, 1].ToString().Substring(4, 1); if (quyangbiaozhi == "2") { //判断取样代码。为"12"时,磅后取样,写入IC卡取样时间和取样人员(即货场操作人员);不为"12"时,不对IC卡操作,并且提示。 string qydt = string.Format("{0:yyyyMMddhhmmss}", DateTime.Now); string writedata = quyangbiaozhi + qydt + CommonFunction.Add20ToUserName(ic.GetHexFromChs(UserName), 16); bool auth = mfc.AuthenticateSectorWithKeyB(3, ic.ToDigitsBytes(ic.mb)); if (auth) { mfc.WriteBlock(12, ic.ToDigitsBytes(writedata));//12位取样时间和16位卸货人员名字写入IC卡3扇区12块 } else { result = false; } } else { result = false; CommonFunction.ShowMessage("取样类型非'磅后取样'", this, true); } } catch (Exception ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); return; } #endregion } else if (beforequality.Checked) { } } finally { mfc.Close(); } } } catch (Exception ex) { result = false; CommonFunction.ShowMessage(ex.Message, this, true); } finally { mfc.Close(); } } else { #region 判断卡类型 result = false; CommonFunction.ShowMessage("不支持该类型的卡", this, true); return; #endregion } } else if (CommonFunction.mode == "SerialPort") { CommonFunction.ShowMessage("磅后取样", this, true); } if (result) { beforequality.Checked = false; beforequality.Enabled = false; afterquality.Checked = false; afterquality.Enabled = false; CommonFunction.ShowMessage("操作成功!", this, true); } }