下面的代码是http://blog.csdn.net/ex_net/article/details/7250033 的改进版本, 用多线程实现读卡器的读卡。
#include <pthread.h>
#include"reader.h"
void *create(void *arg)
{
while(1)
{
printf("thread is Running ..... ");
sleep(5);
}
}
void *ReaderThread(void *arg)
{
int reader_fd;
char buf[32];
if((reader_fd=OpenReader("/dev/tty0"))==-1)
{
puts("Open Dev Error!\r\n");
}
while(1)
{
if(ReadId(reader_fd,buf,32)==1)
{
printf("Read ID=%s\r\n",buf);
}
}
CloseReader(reader_fd);
}
int main(int argc, char *argv[])
{
pthread_t tidp;
int rc1,rc2;
rc1=pthread_create(&tidp,NULL,create,NULL);
if(rc1!=0)
{
printf("pthread_create is not created ... \r\n");
return -1;
}
printf("prthread_create is created... \r\n");
rc2=pthread_create(&tidp,NULL,ReaderThread,NULL);
if(rc2!=0)
{
printf("ReaderThread is not created ... \r\n");
return -1;
}
printf("ReaderThread is created... \r\n");
while(1)
{
printf("System is Runing...\r\n");
sleep(1);
}
return 0;
}
程序运行效果如下图: