一个FDX-B ISO11784/85协议动物标签内包括了以下信息:
本示例使用的发卡器: EM4305 EM4469 ISO11784/85协议125K低频FXD-B动物标签读写发卡器-淘宝网 (taobao.com)
国家编码:900
国家编码转二进制:1110000100 (高位在前低位在后)
SN序列号:202300000001
SN序列号转二次制:10111100011010000001010000011100000001 (高位在前低位在后)
1、首先发送11位前导码:10000000000
2、加入0到7位SN码 再1位‘1’:10000000110000000000
3、加入8到15位SN码 再加1位‘1’:10000011110000000110000000000
4、加入16到23位SN码 再加1位‘1’:10000010110000011110000000110000000000
5、加入24到31位SN码 再加1位‘1’:10001101010000010110000011110000000110000000000
6、加入32到37位SN码、0、1两位国家编码、再加1位‘1’:10010111110001101010000010110000011110000000110000000000
7、加入2到9位国家编码再加1位‘1’:11110000110010111110001101010000010110000011110000000110000000000
8、加入1位data blocks标识位、7位保留位再加1位‘1’:
9、加入7位保留位、1位Animal标识位再加1位‘1’:
10、计算以上由38位SN码、10位国家代码、14位保留码、1位data block标识位、1位Animal标识位 总计64位8字节的 crc16ccitt 校验码。
11、加入8位crc16ccitt 低字节校验码再加1位‘1’
12、加入8位crc16ccitt 高字节校验码再加1位‘1’
13、加入8位扩展信息再加1位‘1’
14、加入8位扩展信息再加1位‘1’
15、加入8位扩展信息再加1位‘1’
16,以上总计128位数据,写入EM4305卡片的5、6、7、8四个块,即为FDX-B协议标签。
private void button1_Click(object sender, EventArgs e)
{
byte status; //存放返回值
int j;
byte myctrlword = 0x00; //控制字
byte[] oldpicckey = new byte[4]; //密码
byte[] mypiccserial = new byte[4]; //卡序列号
byte[] mypiccdata = new byte[70]; //读卡数据缓冲:卡无线转输分频比、卡内容长度(字节数),及最多返回12个块的数据
byte[] mypiccblockflag = new byte[2]; //指定读哪一块
string bitstr;
textBox1.Text = "0000";
textBox2.Text = "000000000000";
textBox3.Text = "00000";
textBox4.Text = "00000000";
mypiccblockflag[0] = 224;
mypiccblockflag[1] = 1;
status = em4305_read(myctrlword, mypiccserial, oldpicckey, mypiccblockflag, mypiccdata);
if (status == 0)
{
idr_beep(30);
string blockdata = "";
for (int i = 0; i < mypiccdata[1]; i++)
{
bitstr ="00000000"+ Convert.ToString(mypiccdata[2 + i], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
blockdata =bitstr+ blockdata;
}
byte[] info = new byte[14];
if (checkgetbitinf(blockdata, info))
{
string Serialnumberbit = "";
bitstr = "00000000" + Convert.ToString(info[1], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Serialnumberbit = bitstr + Serialnumberbit;
bitstr = "00000000" + Convert.ToString(info[2], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Serialnumberbit = bitstr + Serialnumberbit;
bitstr = "00000000" + Convert.ToString(info[3], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Serialnumberbit = bitstr + Serialnumberbit;
bitstr = "00000000" + Convert.ToString(info[4], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Serialnumberbit = bitstr + Serialnumberbit;
bitstr = "00000000" + Convert.ToString(info[5], 2);
string conutrybit = "";
conutrybit = bitstr.Substring(0, 2);
bitstr = bitstr.Substring(bitstr.Length - 6);
Serialnumberbit = bitstr + Serialnumberbit;
Int64 SerialnumberId = Convert.ToInt64(Serialnumberbit, 2);
textBox2.Text = SerialnumberId.ToString("D12");
bitstr = "00000000" + Convert.ToString(info[6], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
conutrybit = bitstr + conutrybit;
Int16 conutryid = Convert.ToInt16(conutrybit, 2);
textBox1.Text = conutryid.ToString("D4");
bitstr = "00000000" + Convert.ToString(info[7], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
string Reservedbit = bitstr.Substring(0, 7);
if (bitstr.Substring(7, 1) == "1") { checkBox3.Checked = true; } else { checkBox3.Checked = false; }
bitstr = "00000000" + Convert.ToString(info[8], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
if (bitstr.Substring(0, 1) == "1") { checkBox4.Checked = true; } else { checkBox4.Checked = false; }
Reservedbit = Reservedbit + bitstr.Substring(1, 7);
Int16 Reserved = Convert.ToInt16(Reservedbit, 2);
textBox3.Text = Reserved.ToString("D5");
string Extensionbit = "";
bitstr = "00000000" + Convert.ToString(info[11], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Extensionbit = bitstr + Extensionbit;
bitstr = "00000000" + Convert.ToString(info[12], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Extensionbit = bitstr + Extensionbit;
bitstr = "00000000" + Convert.ToString(info[13], 2);
bitstr = bitstr.Substring(bitstr.Length - 8);
Extensionbit = bitstr + Extensionbit;
Int32 Extension = Convert.ToInt32(Extensionbit, 2);
textBox4.Text = Reserved.ToString("D8");
}
else
{
MessageBox.Show("不是有效的FDX-B协议标签!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else { MessageErrInf(status); }
}
private void button2_Click(object sender, EventArgs e)
{
Int16 conutryid = Convert.ToInt16(textBox1.Text.Trim());
Int64 SerialnumberId = Convert.ToInt64(textBox2.Text.Trim());
Int16 Reserved = Convert.ToInt16(textBox3.Text.Trim());
Int32 Extension = Convert.ToInt32(textBox4.Text.Trim());
if (conutryid > 1023){
MessageBox.Show("Country国家代码的取值范围是:0~1023!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Trim().Length;
textBox1.Select();
return;
}
if (SerialnumberId > 274877906943)
{
MessageBox.Show("SerialNumber序列号的取值范围是:0~274877906943!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox2.SelectionStart = 0;
textBox2.SelectionLength = textBox2.Text.Trim().Length;
textBox2.Select();
return;
}
if (Reserved > 16383)
{
MessageBox.Show("Reserved保留信息的取值范围是:0~16383!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox3.SelectionStart = 0;
textBox3.SelectionLength = textBox3.Text.Trim().Length;
textBox3.Select();
return;
}
if (Extension > 16777215)
{
MessageBox.Show("Extension扩展信息的取值范围是:0~16777215!", "示例提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox4.SelectionStart = 0;
textBox4.SelectionLength = textBox4.Text.Trim().Length;
textBox4.Select();
return;
}
string Extensionbit = "000000000000000000000000"+Convert.ToString(Extension, 2);
Extensionbit = Extensionbit.Substring(Extensionbit.Length - 24);
string Reservedbit = "00000000000000" + Convert.ToString(Reserved, 2);
Reservedbit = Reservedbit.Substring(Reservedbit.Length - 14);
if (checkBox4.Checked) { Reservedbit = "1" + Reservedbit; } else { Reservedbit = "0" + Reservedbit; }
if (checkBox3.Checked) { Reservedbit = Reservedbit+"1"; } else { Reservedbit = Reservedbit+"0"; }
string conutrybit = "0000000000" + Convert.ToString(conutryid, 2);
conutrybit = conutrybit.Substring(conutrybit.Length - 10);
string Serialnumberbit = "00000000000000000000000000000000000000" + Convert.ToString(SerialnumberId, 2);
Serialnumberbit = Serialnumberbit.Substring(Serialnumberbit.Length - 38);
string fdxbitstr = "10000000000"; //前导码
fdxbitstr = "1" + Serialnumberbit.Substring(30, 8) + fdxbitstr;
fdxbitstr = "1" + Serialnumberbit.Substring(22, 8) + fdxbitstr;
fdxbitstr = "1" + Serialnumberbit.Substring(14, 8) + fdxbitstr;
fdxbitstr = "1" + Serialnumberbit.Substring(6, 8) + fdxbitstr;
fdxbitstr = "1" + conutrybit.Substring(8, 2) + Serialnumberbit.Substring(0, 6) + fdxbitstr;
fdxbitstr = "1" + conutrybit.Substring(0, 8) + fdxbitstr;
fdxbitstr = "1" + Reservedbit.Substring(8, 8) + fdxbitstr;
fdxbitstr = "1" + Reservedbit.Substring(0, 8) + fdxbitstr;
byte[] info = new byte[8];
for (int i = 0; i < 8; i++)
{
string thisbit = fdxbitstr.Substring((i + 1) * 9 - 9, 9);
if (thisbit.Substring(0, 1) == "1")
{
info[7 - i] = (byte)Convert.ToInt16(thisbit.Substring(1, 8), 2);
}
}
ushort crc16ccitt = GetCrc16Ccitt(info);
string crcbit = "0000000000000000" + Convert.ToString(crc16ccitt, 2);
crcbit = crcbit.Substring(crcbit.Length - 16);
fdxbitstr = "1" + crcbit.Substring(8, 8) + fdxbitstr;
fdxbitstr = "1" + crcbit.Substring(0, 8) + fdxbitstr;
fdxbitstr = "1" + Extensionbit.Substring(16, 8) + fdxbitstr;
fdxbitstr = "1" + Extensionbit.Substring(8, 8) + fdxbitstr;
fdxbitstr = "1" + Extensionbit.Substring(0, 8) + fdxbitstr;
byte[] writebuf = new byte[16];
for (int i = 0; i < 16; i++)
{
string thisbit = fdxbitstr.Substring((i + 1) * 8 - 8, 8);
writebuf[15 - i] = (byte)Convert.ToInt16(thisbit, 2);
}
byte status; //存放返回值
byte myctrlword = 0x00; //控制字
byte[] oldpicckey = new byte[4]; //密码
byte[] mypiccserial = new byte[4]; //卡序列号
byte[] mypiccblockflag = new byte[2]; //指定读哪一块
mypiccblockflag[0] = 224;
mypiccblockflag[1] = 1;
status = em4305_write(myctrlword, mypiccserial, oldpicckey, mypiccblockflag, writebuf);
if (status == 0)
{
if (checkBox2.Checked){
em4305_lock(myctrlword, mypiccserial, oldpicckey, mypiccblockflag);
}
string seriaStr = "";
for (int i = 0; i < 4; i++)
{
seriaStr = seriaStr + mypiccserial[i].ToString("X2");
}
idr_beep(30);
MessageBox.Show("卡号:" + seriaStr + "写FDX-B协议标签成功!", "提示:", MessageBoxButtons.OK , MessageBoxIcon.Information );
if (checkBox1.Checked)
{
SerialnumberId = SerialnumberId + 1;
textBox2.Text = SerialnumberId.ToString("D12");
}
}
else { MessageErrInf(status); }
}