public static void requestID(ref Task Data) { int m_antenna_sel = 1; int status = -1; status = Net_SetAntenna(m_hScanner, m_antenna_sel); Sleep(20); int nCounter = 0, ID_len = 0, ID_len_temp = 0; string str = "", strtemp = ""; byte[] DB = new byte[128]; byte[] IDBuffer = new byte[7680]; byte[] mask = new byte[96]; byte[] AccessPassWord = new byte[4]; for (int n = 0; n < 150; n++) { //读取G2卡ID Array.Clear(Data.TagBuffer, 0, Data.TagBuffer.Length); status = Net_EPC1G2_ReadLabelID(m_hScanner, 1, 0, 0, mask, IDBuffer, ref nCounter); if (status != OK) { Sleep(20); continue; } // 如果ID Buffer中的ID比设定的长度(目前是6)大,则说明读取错误?放弃本次读取,再读一次 if (IDBuffer[ID_len] > 6) { nCounter = 0; Sleep(2); continue; }// //转换ID Buffer到 String //取ID Buffer中第一个ID,若有多个ID,则只保留第一个,忽略其余 ID_len_temp = IDBuffer[ID_len] * 2 + 1; for (int j = 0; j < ID_len_temp; j++) { Data.TagBuffer[j] = IDBuffer[ID_len + j]; } ID_len += ID_len_temp; str = ""; strtemp = ""; ID_len = Data.TagBuffer[0] * 2; for (int j = 0; j < ID_len; j++) { strtemp = Data.TagBuffer[j + 1].ToString("X2"); str += strtemp; } Data.BOXID = str; if (nCounter < 1) { Data.status = false; Data.log = "区域中暂时没有卡片"; } if (nCounter == 1) Data.status = true; if (nCounter > 1) { Data.status = false; Data.log = "检测到多张卡"; } //MessageBox.Show(CardID); /************************************* ************************************/ //返回卡ID,取EPC数据块的前6位字符 // if (CardID.Length > 6) // CardID = CardID.Substring(0, 6); //************************************/ break;//*/ } }
public static int PutDataIntoCard(int block, int n_ptr, int n_len, string PutString, Task Data) { int status = -1; //string str = "", strtemp = ""; string str = ""; byte EPC_BYTE = 0x00; byte[] DB = new byte[128]; byte[] IDBuffer = new byte[7680]; byte[] mask = new byte[96]; mask = System.Text.Encoding.Default.GetBytes(PutString); byte[] AccessPassword = new byte[4]; string str_temp = "00000000"; // 读取密码 for (int i = 0; i < 4; i++) { AccessPassword[i] = Convert.ToByte(str_temp[i * 2] + str_temp[i * 2 + 1]); AccessPassword[i] = 0; } //MessageBox.Show(AccessPassword[1].ToString()); EPC_BYTE = Convert.ToByte("6"); //目前认为ID是6位 byte ptr = Convert.ToByte(n_ptr); //准备读的首地址是n_ptr byte len = Convert.ToByte(n_len); //准备读的长度是n_len //设置天线 status = Net_SetAntenna(m_hScanner, 1); ; if (status != OK) { MessageBox.Show("读卡器天线出现问题,请重试!\n若仍然有问题,请联系生产厂家!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return -1; } for (int n = 0; n < 150; n++) { //MessageBox.Show("天线成功!"); //写入卡片指定区域 if (block == 3) { //目前写入User区的 byte[] IDTemp = new byte[12]; //目前认为ID是6位 for (int i = 0; i < Data.TagBuffer[0] * 2; i++) { IDTemp[i] = Data.TagBuffer[i + 1]; } str = PutString; Sleep(5); status = Net_EPC1G2_WriteWordBlock(m_hScanner, EPC_BYTE, IDTemp, 3, ptr, len, mask, AccessPassword); } else if (block == 1) { } else return 7; if (status == OK) break; Sleep(5); } if (status != OK) { switch (status) { case 1: MessageBox.Show("出问题啦!读卡器天线连接失败!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 2: MessageBox.Show("未检测到有效的数据卡!请扫描卡片!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 3: MessageBox.Show("出问题啦!检测到非法的数据卡!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 4: MessageBox.Show("出问题啦!读写功率不够!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 5: MessageBox.Show("出问题啦!数据卡ID区读写保护!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 6: MessageBox.Show("出问题啦!校验和错误!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 7: MessageBox.Show("出问题啦!参数错误!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 8: MessageBox.Show("出问题啦!数据区不存在!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; case 9: MessageBox.Show("出问题啦!数据卡的密码不正确!", "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; default: MessageBox.Show("出现了位置问题!\n错误代码:" + status.ToString(), "出问题啦!", MessageBoxButtons.OK, MessageBoxIcon.Error); break; } return 5; } return 0; }
public static void requestData(ref Task Data) { byte[] rawData = new byte[24]; if (!getrawdata(Data, ref rawData)) { Data.status = false; Data.log = "读取数据失败"; return; } Data.finished = System.Text.Encoding.Default.GetString(rawData, 20, 1); //MessageBox.Show(Data.finished); try { Data.CarNum = System.Text.Encoding.Default.GetString(rawData, 2, 8).Trim(); Data.Type = int.Parse(System.Text.Encoding.Default.GetString(rawData, 0, 2).Trim()); Data.StartTime = decodetime(ToHexString(rawData, 12, 5)).Trim(); Data.StartSpot = int.Parse(System.Text.Encoding.Default.GetString(rawData, 18, 2).Trim()); } catch { Data.status = false; Data.log = "数据转换时出错"; return; } if (Data.finished == MISSION_FINISH) { Data.status = false; Data.log = "任务已完成"; return; } // MessageBox.Show(Data.StartSpot.ToString()); string sEndTime = System.DateTime.Now.ToString("yy-MM-dd,HH:mm"); string sEndTime2 = System.DateTime.Now.ToString("yyMMddHHmm"); sEndTime2 = sEndTime2.Substring(0, 10); Data.EndTime = sEndTime; if (PutDataIntoCard(3, 10, 1, MISSION_FINISH, Data) != 0) { Data.status = false; Data.log = "写卡失败"; return; } Data.status = true; Data.log = "写卡成功!"; }
public static bool getrawdata(Task Data, ref byte[] raw_Data) { byte[] rawData = new byte[24]; int status = -1; byte EPC_BYTE = 0x00; // byte ptr = 0x00; //读取起始地址 //byte len = 0x00; //读取的长度 byte[] DB = new byte[128]; byte[] IDBuffer = new byte[7680]; byte[] mask = new byte[96]; byte[] AccessPassword = new byte[4]; string str_temp = "00000000"; for (int i = 0; i < 4; i++) { AccessPassword[i] = Convert.ToByte(str_temp[i * 2] + str_temp[i * 2 + 1]); } EPC_BYTE = Convert.ToByte("6"); //目前认为ID是6位 for (int n = 0; n < 30; n++) { byte[] IDTemp = new byte[12]; //目前认为ID是6位 for (int i = 0; i < Data.TagBuffer[0] * 2; i++) { IDTemp[i] = Data.TagBuffer[i + 1]; } status = Net_EPC1G2_ReadWordBlock(m_hScanner, EPC_BYTE, IDTemp, Convert.ToByte(3), Convert.ToByte(0), Convert.ToByte(8), DB, AccessPassword); if (status == OK) break; Sleep(1); } if (status != OK) { return false; } for (int i = 0; i < 16; i++) { rawData[i] = DB[i]; DB[i] = 0; } for (int n = 0; n < 30; n++) { byte[] IDTemp = new byte[12]; //目前认为ID是6位 for (int i = 0; i < Data.TagBuffer[0] * 2; i++) { IDTemp[i] = Data.TagBuffer[i + 1]; } status = Net_EPC1G2_ReadWordBlock(m_hScanner, EPC_BYTE, IDTemp, Convert.ToByte(3), Convert.ToByte(8), Convert.ToByte(4), DB, AccessPassword); if (status == OK) break; Sleep(1); } if (status != OK) return false; for (int i = 16; i < 23; i++) { rawData[i] = DB[i - 16]; DB[i - 16] = 0; } raw_Data = rawData; return true; }