- 论坛徽章:
- 0
|
小弟近日接到老板的一个新活,是关于ic卡的读写操作,参阅了网上的文章后,发现在主机读取ic卡读写器返回的数据时挂起,ic卡读写器的灯灭,程序僵死,恳请各位大哥帮忙看看。
卡片采用的是mifare s50结构
这个是所采用的协议:
1。命令序列:主机发送0x02给读写器,读写器发回0x10表示可以接受数据,主机接受到0x10后,发送数据块,发送完数据块后,再发送0x10,0x03表示此次通讯结束,读写器再发回0x10表示确认结束。
2。响应序列:读写器发送0x02给主机,主机收到后发回0x10表示就绪,读写器发送数据块,发送完数据块后,再发送0x10,0x03,表示发送结束,主机接受后,再发回0x10表示确认结束。
数据块的格式:
1。主机到读写器:
命令(1字节)+ length(low)(1字节) + length(high)(1字节) + 数据(length字节) + BBC(校验和,从命令到BBC的异或和为0,1字节)
2。读写器到主机:
状态(1字节)+ length(low)(1字节) + length(high)(1字节) + 数据(length字节) + BBC(校验和,从状态到BBC的异或和为0,1字节)
其中termios结构设置如下:
newtio.c_cflag = B115200 | CS8 | CREAD | CLOCAL;
newtio.c_iflag = IGNPAR | ICRNL ;
newtio.c_oflag = OPOST;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
处理函数如下:
WriteChar('\x02'); //发送命令字符
ch = ReadChar(); //接受读卡器发回的状态字符
if (ch == '\x10')
{
printf("the device is OK!\n");
Write(sz); //发送命令序列
WriteChar('\x10');
WriteChar('\x03');
ch = ReadChar();
if(ch == '\x10')
{
cout << "end of host to client!" << endl;
}
else if(ch == '\x15')
{
cout << "the machine not answer!" << endl;
exit(-1);
}
cout << "start to recv from client!" << endl;
ch = ReadChar();
printf("the read ch should be x02 : %02x\n",(unsigned char)ch); //?程序死到这里了,接收的不是0x02,而是0x10
if(ch == '\x02')
{
cout << "the device start to send data!" << endl;
s_in = 0; //接收字符个数
WriteChar('\x10');
readnum = Read(2); //开始接收数据,先接受两个长度,判断是否有数据要发回,如果有,继续接收,否则停止接收结束
printf("the readnum is :%d\n",readnum);
if(readnum)
{
usleep(500);
num = (int)buf_in[1];
readnum = num + 2;
if(num)
Read(readnum);
usleep(500);
ch = ReadChar();
if( ch == '\x10')
{
cout << "end of data buf!" << endl;
}
ch = ReadChar();
if( ch == '\x03')
{
cout << "end of recv!" << endl;
}
WriteChar('\x10');
}
else
WriteChar('\x10');
}
}
else if(ch == '\x15')
{
printf("the device is not work!\n");
exit(-1);
}
return 0;
现在的问题是发送的命令字符:0x02,0x10,0x03,读写器都能响应,但在读写器返回数据时死机,也就是说读写器不发回响应序列,它只发回0x10这个状态字符,
如果哪位大哥做过读写器的程序,请指点一下,小弟不胜感激!!! 先在此谢过 |
|