private void ConnectCurrentMifareClassic() { if (!currentMifareClassic.IsConnected) { currentMifareClassic.Connect(); } }
public NFCMessage MifareClassic_AuthenticateSectorWithKeyB(MifareClassic mfc, int Sector, List <byte[]> Keys) { if (mfc == null) { return(NFCMessage.NFC_NULL_MIFARECLASSIC); } if (!mfc.IsConnected) { mfc.Connect(); } bool auth = false; int i = 0; if (Keys == null || Keys.Count == 0) { auth = mfc.AuthenticateSectorWithKeyB(Sector, MifareClassic.KeyNfcForum.ToArray()); if (!auth) { auth = mfc.AuthenticateSectorWithKeyB(Sector, MifareClassic.KeyDefault.ToArray()); } } else { while (!auth || i != Keys.Count) { auth = mfc.AuthenticateSectorWithKeyB(Sector, Keys[i]); i++; } } return(auth ? NFCMessage.NFC_AUTH_OK : NFCMessage.NFC_AUTH_FAIELD); }
private bool TryAndReadFromTag(Tag tag) { MifareClassic mfc = MifareClassic.Get(tag); byte[] data; bool auth = false; try { mfc.Connect(); String UID = byte2hex(mfc.Tag.GetId()); String cardData = null; int secCount = 1; int bCount = 3; //Just read 2 block for now int bIndex = 0; for (int j = 1; j <= secCount; j++) { auth = mfc.AuthenticateSectorWithKeyB(j, KEY_DEFAULT); if (auth) { //bCount = mfc.GetBlockCountInSector(j); bIndex = mfc.SectorToBlock(j); for (int i = 0; i < bCount; i++) { data = mfc.ReadBlock(bIndex); cardData += Encoding.ASCII.GetString(data); bIndex++; } DisplayMessage(cardData); mfc.Close(); return(true); } else { Log.Error(Tag, "Sector read authentication failed"); mfc.Close(); return(false); } } } catch (Exception e) { Log.Error(Tag, e.Message); } return(false); }
/// <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); }
private void ReadIcData(Intent intent) { m_intent = intent; ic.icserial = ic.FindIC(m_intent); Car = new CarInfo(); //初始化一个CarInfo类 //取出封装在intent中的TAG var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; #region 清空文本 carNumber = FindViewById <EditText>(Resource.Id.carNumber); carNumber.Text = ""; #endregion MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { #region 获取到tag try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { #region 循环读取5个扇区数据 for (int i = 0; i < ic.sectors.Length; i++) { for (int k = 0; k < 3; k++) { int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k); if (sec < mfc.SectorCount) { if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb))) { int c = mfc.SectorCount; c = mfc.BlockCount; byte[] data = new byte[16]; data = mfc.ReadBlock(ic.sectors[i] * 4 + k); if (data != null) { ic.IcDatas[i, k] = ic.ToHexString(data); ic.isReadIcOK = true; } else { CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true); ic.isReadIcOK = false; return; } } else { CommonFunction.ShowMessage("密码验证失败!", this, true); ic.isReadIcOK = false; return; } } } } #endregion } } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } finally { mfc.Close(); } if (ic.isReadIcOK) { try { //解析扇区数据 Car = ic.GetCarInfo(ic.IcDatas); if (Car != null) { CommonFunction.ShowMessage("", this, false); #region 读卡正确,显示在窗体文本内,按钮复活 carNumber.Text = Car.CarPlate; // carNumber.Text = "GT030108";//暂用 //根据车号查询 相关收发货路线信息 goodsInfo(); #endregion } else { #region 读卡异常,按钮不可用 btSender.Enabled = false; btGet.Enabled = false; CommonFunction.ShowMessage("卡内信息错误!", this, true); #endregion } } catch (System.ArgumentOutOfRangeException ax) { CommonFunction.ShowMessage("读卡信息错误!", this, true); } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } } #endregion } else { #region 判断卡类型 btGet.Enabled = false; btSender.Enabled = false; btGet.Checked = false; btSender.Checked = false; CommonFunction.ShowMessage("不支持该类型的卡", this, true); ic.isReadIcOK = false; return; #endregion } }
public byte[] LerUmSetoresDoCartao(int bloco, int setor) { byte[] byteRead = null; // Irá armazenar os byte lidos do cartão bool auth = false; // Valida se existe ou não permissão para ler o bloco int blocoIndex = 0; // Irá armazenar o indice que esta sendo lido do cartão try { // Faz a conexão com o cartão mifareClassic.Connect(); // Faz a validação de permissão para a leitura do bloco A auth = ValidPermissaoBlocoA(bloco); if (!auth) { // Faz a validação de permissão para a leitura do bloco B auth = ValidPermissaoBlocoB(bloco); if (!auth) { throw new System.Exception("Falha na validação de senha."); } } if (auth) { // É necessário fazer novamente a validação de permissão Bloco a bloco // Faz a validação de permissão para a leitura do bloco A auth = ValidPermissaoBlocoA(setor); if (!auth) { // Faz a validação de permissão para a leitura do bloco B auth = ValidPermissaoBlocoB(setor); } if (auth) { // seta o indice do setor dentro do bloco blocoIndex = mifareClassic.SectorToBlock(setor); // Faz a leitura de um BLOCO no setor byteRead = mifareClassic.ReadBlock(blocoIndex); } } } catch (IOException e) { throw new System.Exception(e.Message); } finally { DesconectaCartao(); this.GravaTempoFinal(); } return(byteRead); }
private void ReadIcData(Intent intent) { m_intent = intent; ic.icserial = ic.FindIC(m_intent); Car = new CarInfo(); //初始化一个CarInfo类 //取出封装在intent中的TAG tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; //清空文本 extra_bags.Text = ""; samp_bags.Text = ""; samp_factory.Text = ""; samp_goodsname.Text = ""; samp_carNumber.Text = ""; cmeasure_Id.Text = ""; MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { #region 获取到tag try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { #region 循环读取5个扇区数据 for (int i = 0; i < ic.sectors.Length; i++) { for (int k = 0; k < 3; k++) { int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k); if (sec < mfc.SectorCount) { if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb))) { int c = mfc.SectorCount; c = mfc.BlockCount; byte[] data = new byte[16]; data = mfc.ReadBlock(ic.sectors[i] * 4 + k); if (data != null) { ic.IcDatas[i, k] = ic.ToHexString(data); ic.isReadIcOK = true; } else { CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true); ic.isReadIcOK = false; return; } } else { CommonFunction.ShowMessage("密码验证失败!", this, true); ic.isReadIcOK = false; return; } } } } #endregion } } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } finally { mfc.Close(); } if (ic.isReadIcOK) { try { //解析扇区数据 Car = ic.GetCarInfo(ic.IcDatas); if (Car != null) { // CommonFunction.ShowMessage("", this, false); //将摇号信息写入本地数据表 // isEqualFactory(); #region 读卡正确,显示在窗体文本内,按钮复活 //是否为同一张卡 重复卡不得重复刷 if (!isEqualFactory()) { samp_carNumber.Text = Car.CarPlate; // bussnise.Text = Car.BusinessType; // measuremode.Text = Car.MeasureMode; cmeasure_Id.Text = Car.MeasureID; //根据车号和计量单号 从计量系统中查询 采购订单号 供应商编号 供应商名称 货物编号 货物名称等信息 bsif.selectCmInfo(Car.CarPlate, Car.MeasureID, out cMeasureID, out purchaseOrderID, out car_factoryCode, out car_factoryName, out car_goodsCode, out car_goodsName); samp_factory.Text = car_factoryName; samp_goodsname.Text = car_goodsName; } #endregion } else { #region 读卡异常,按钮不可用 ToggleButton samp = FindViewById <ToggleButton>(Resource.Id.btExtract); samp.Enabled = false; CommonFunction.ShowMessage("卡内信息错误!", this, true); #endregion } } catch (System.ArgumentOutOfRangeException ax) { CommonFunction.ShowMessage("读卡信息错误!", this, true); } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } } #endregion } else { #region 判断卡类型 btExtra.Enabled = false; btExtra.Checked = false; CommonFunction.ShowMessage("不支持该类型的卡", this, true); ic.isReadIcOK = false; return; #endregion } }
/// <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); }
private void ReadIcData(Intent intent) { m_intent = intent; ic.icserial = ic.FindIC(m_intent); Car = new CarInfo(); //初始化一个CarInfo类 //取出封装在intent中的TAG tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { #region 获取到tag try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { #region 循环读取5个扇区数据 for (int i = 0; i < ic.sectors.Length; i++) { for (int k = 0; k < 3; k++) { int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k); if (sec < mfc.SectorCount) { if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb))) { int c = mfc.SectorCount; c = mfc.BlockCount; byte[] data = new byte[16]; data = mfc.ReadBlock(ic.sectors[i] * 4 + k); if (data != null) { ic.IcDatas[i, k] = ic.ToHexString(data); ic.isReadIcOK = true; } else { CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true); ic.isReadIcOK = false; return; } } else { CommonFunction.ShowMessage("密码验证失败!", this, true); ic.isReadIcOK = false; return; } } } } #endregion } } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } finally { mfc.Close(); } if (ic.isReadIcOK) { ToggleButton samp = FindViewById <ToggleButton>(Resource.Id.btSamp); //Button samp = FindViewById<Button>(Resource.Id.btSamp); samp.Enabled = true; btSamp.SetBackgroundColor(Android.Graphics.Color.ParseColor("#DB3E3E")); } else { #region 读卡异常,按钮不可用 Button samp = FindViewById <Button>(Resource.Id.btSamp); samp.Enabled = false; CommonFunction.ShowMessage("卡内信息错误!", this, true); #endregion } #endregion } else { #region 判断卡类型 btSamp.Enabled = false; btSamp.Checked = false; CommonFunction.ShowMessage("不支持该类型的卡", this, true); ic.isReadIcOK = false; return; #endregion } }
/// <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); }
private void ReadIcData(Intent intent) { m_intent = intent; ic.icserial = ic.FindIC(m_intent); Car = new CarInfo(); //初始化一个CarInfo类 //取出封装在intent中的TAG var tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag; if (tag == null) { return; } m_tag = tag; #region 清空文本 EditText carinfo = FindViewById <EditText>(Resource.Id.tuCarInfo); carinfo.Text = ""; EditText bussnise = FindViewById <EditText>(Resource.Id.tuBussnise); bussnise.Text = ""; EditText measuretype = FindViewById <EditText>(Resource.Id.tuMeasureType); measuretype.Text = ""; EditText measuremode = FindViewById <EditText>(Resource.Id.tuMeasureMode); measuremode.Text = ""; EditText measureid = FindViewById <EditText>(Resource.Id.tuMeasureID); measureid.Text = ""; EditText goodweight = FindViewById <EditText>(Resource.Id.tuGoodWeight); goodweight.Text = ""; EditText measuredt = FindViewById <EditText>(Resource.Id.tuMeasureDT); measuredt.Text = ""; EditText buckleweight = FindViewById <EditText>(Resource.Id.eBuckleWeight); buckleweight.Text = "0"; #endregion MifareClassic mfc = MifareClassic.Get(m_tag); if (mfc != null) { #region 获取到tag try { mfc.Connect(); var type = mfc.GetType(); if (type.Equals(typeof(Android.Nfc.Tech.MifareClassic))) { #region 循环读取5个扇区数据 for (int i = 0; i < ic.sectors.Length; i++) { for (int k = 0; k < 3; k++) { int sec = mfc.BlockToSector(ic.sectors[i] * 4 + k); if (sec < mfc.SectorCount) { if (mfc.AuthenticateSectorWithKeyB(sec, ic.ToDigitsBytes(ic.mb))) { int c = mfc.SectorCount; c = mfc.BlockCount; byte[] data = new byte[16]; data = mfc.ReadBlock(ic.sectors[i] * 4 + k); if (data != null) { ic.IcDatas[i, k] = ic.ToHexString(data); ic.isReadIcOK = true; } else { CommonFunction.ShowMessage("读取" + ic.sectors[i].ToString() + "扇区" + Convert.ToString(ic.sectors[i] * 4 + k) + "块失败!", this, true); ic.isReadIcOK = false; return; } } else { CommonFunction.ShowMessage("密码验证失败!", this, true); ic.isReadIcOK = false; return; } } } } #endregion } } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } finally { mfc.Close(); } if (ic.isReadIcOK) { try { //解析扇区数据 Car = ic.GetCarInfo(ic.IcDatas); if (Car != null) { CommonFunction.ShowMessage("", this, false); #region 读卡正确,显示在窗体文本内,按钮复活 carinfo.Text = Car.CarPlate; bussnise.Text = Car.BusinessType; measuretype.Text = Car.MeasurementType; measuremode.Text = Car.MeasureMode; measureid.Text = Car.CardID; goodweight.Text = Car.Gross.ToString(); measuredt.Text = Car.GrossDateTime; Button unload = FindViewById <Button>(Resource.Id.buUnLoad); unload.Enabled = true; unload.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32")); unload.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff")); Button halfreturn = FindViewById <Button>(Resource.Id.buHalfReturn); halfreturn.Enabled = true; halfreturn.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32")); halfreturn.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff")); Button allreturn = FindViewById <Button>(Resource.Id.buAllReturn); allreturn.Enabled = true; allreturn.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32")); allreturn.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff")); Button unloadandload = FindViewById <Button>(Resource.Id.buUnloadAndLoad); unloadandload.Enabled = true; unloadandload.SetBackgroundColor(Android.Graphics.Color.ParseColor("#BD2B32")); unloadandload.SetTextColor(Android.Graphics.Color.ParseColor("#ffffff")); #endregion } else { #region 读卡异常,按钮不可用 Button unload = FindViewById <Button>(Resource.Id.buUnLoad); unload.Enabled = false; Button halfreturn = FindViewById <Button>(Resource.Id.buHalfReturn); halfreturn.Enabled = false; Button allreturn = FindViewById <Button>(Resource.Id.buAllReturn); allreturn.Enabled = false; Button unloadandload = FindViewById <Button>(Resource.Id.buUnloadAndLoad); unloadandload.Enabled = false; CommonFunction.ShowMessage("卡内信息错误!", this, true); #endregion } } catch (System.ArgumentOutOfRangeException ax) { CommonFunction.ShowMessage("读卡信息错误!", this, true); } catch (Exception ex) { CommonFunction.ShowMessage(ex.Message, this, true); } } #endregion } else { #region 判断卡类型 bunload.Enabled = false; bhalfreturn.Enabled = false; ballreturn.Enabled = false; bunloadandload.Enabled = false; bunload.Checked = false; bhalfreturn.Checked = false; ballreturn.Checked = false; bunloadandload.Checked = false; CommonFunction.ShowMessage("不支持该类型的卡", this, true); ic.isReadIcOK = false; return; #endregion } }
/// <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); } }