⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 biaokill.cpp

📁 qt下RFID标签的读取
💻 CPP
字号:
#include"biaokill.h"int num=0;unsigned char buf[200];int sockfd;int first_open = 0;//设置波特率为9600static char cmd_open0[6] = {0xAA, 0xFF, 0x03, 0x64, 0x00, 0xF0};static char rsp_open0[5] = {0xBB, 0xEF, 0x02, 0x00, 0x54};//设置波特率为115200static unsigned char cmd_open1[6] = {0xAA, 0xFF, 0x03, 0x64, 0x04, 0xEC};static char rsp_open1[5] = {0xBB, 0xEF, 0x02, 0x00, 0x54};//命令:可能为复位//打开完成static char cmd_open2[5] = {0xAA, 0xFF, 0x02, 0x66, 0xEF};static char rsp_open2[7] = {0xBB, 0xEF, 0x04, 0x00, 0x01, 0x01, 0x50};//更新数据:读写器会叫一声static char cmd_open3[5] = {0xAA, 0xFF, 0x02, 0x65, 0xF0};static char rsp_open3[5] = {0xBB, 0x01, 0x02, 0x00, 0x42};//读取ISO18000标签的IDstatic char cmd_read1[5] = {0xAA, 0xFF, 0x02, 0x54, 0x01};static char rsp_read1[5] = {0xBB, 0x00, 0x02, 0x00, 0x43};static char rsp_read2[6] ={0xBB, 0x00, 0x03, 0x00, 0x04, 0x3E};static char cmd_read2[5] = {0xAA, 0xFF, 0x02, 0x90, 0xC5};static char cmd_read3[6] = {0xAA,0xFF,0x03,0x50,0x08,0xFC};//BB EF 11 00 01 01 01 E0 04 00 00 6E FC 8A 01 F6 F7 00 00 7C//ID号为:E0 04 00 00 6E FC 8A 01//static char rsp_read2[20] = {0xBB, 0xEF, 0x11, 0x00, 0x01, 0x01, 0x01, 0xE0, 0x04, //                     0x00, 0x00, 0x6E, 0xFC, 0x8A, 0x01, 0xF6, 0xF7, 0x00, 0x00, 0x7C};SetDataForm::SetDataForm( QWidget *parent,  const char* name): QWidget( parent, name)    {           resize(640, 480);	setCaption("Storage Manage System -inventory counting");	QLabel *label1 = new QLabel( "card message:", this);	label1->setGeometry( 0,20,100,35 );	label1->setFont(QFont("Times",15,QFont::Bold));	QLineEdit *edit1 = new QLineEdit(this);	edit1->setGeometry( 110,20,260, 35 );		QPushButton *inqbutton=new QPushButton("Inquiry", this);	inqbutton->setGeometry(450,20, 150,35);	table = new QTable( this, "data table" );        table->setCaption( QString::fromUtf8("读取货物标签信息:") );        table->setGeometry(0,70, 400,320);        table->setNumCols( 1 );        table->setNumRows( 100 );        table->setColumnWidth( 0, 350 );        QHeader *th = table->horizontalHeader();        th->setLabel( 0, "Value" );        		QPushButton *colorButton = new QPushButton( this );        colorButton->setGeometry(20,395, 150,30);        colorButton->setText( "&Save..." );        colorButton->setEnabled( false );	        QPushButton *b1 = new QPushButton("Read", this);       	b1->setGeometry(450,100,150,35);        QPushButton *b2 = new QPushButton("Stop ", this);       	b2->setGeometry(450,180,150,35);        QPushButton *b3 = new QPushButton("Return", this);        b3->setGeometry(450,260,150,35);        QPushButton *b4 = new QPushButton("Quit ", this);        b4->setGeometry(450,340,150,35);               connect(b1, SIGNAL(clicked()), this, SLOT(popupDialog()));        }void SetDataForm::popupDialog(){	/*if ( ( g_fd= open_port ( 1 ) ) <0 ) {	 qWarning ( "open_port error \n" );	//return -1;		}   qWarning("openrfid \n");    open_rfid(g_fd); for (int j=0; j<1000; j++){};*/       table->setText(57,0, "inittimer open");	timer=startTimer(1500);	qWarning("sleep open \n");    /* while(1){    	sleep(1);    	}*/for (int i=1; i<=10000;i++){}qWarning("sleep close \n");    //close(g_fd);    qWarning("close g_fd \n");	}void SetDataForm::timerEvent(QTimerEvent *e){               qWarning("QTimer all right! \n");	//int i=2,j=0;	//char *s; 	if(e->timerId()==timer)	{	qWarning("ready to check data");	qWarning("biao");	table->setText(56,0,"multitags start");    	identify_multitags();		qWarning("sleep close \n");       // close(g_fd);	table->setText(46,0,"multitags over");        killTimer(timer);	   table->setText(45,0, "timer over");	qWarning("timer close \n");            }}int SetDataForm::open_port( int comport){	int fd;	if(comport==1)	{fd=open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY );//调用Unix的open	 if ( fd == -1 ) 	      {		qWarning( "Can't open Serial Port 1\n" );		return -1;	       }else	table->setText(60,0,"ttyS1 open success!");	}//恢复串口为阻塞状态	 fcntl( fd, F_SETFL, 0 );	 return fd;   //返回指针供使用} int SetDataForm::set_port( int fd, int nSpeed, int nBits, char nEvent, int nStop ){	struct termios   newtio, oldtio;	table->setText(65,0,"SET-PORT");	/*6B	if ( tcgetattr ( fd, &oldtio ) != 0 ) {		qWarning ( "Setup Serial 1 failed\n" );		return -1;	}	bzero ( &newtio, sizeof ( newtio ) );*/	//6C	 tcgetattr ( fd, &newtio );	 tcflush(fd,TCIOFLUSH);	//设置字符大小	newtio.c_cflag |= (CLOCAL|CREAD);	newtio.c_cflag &= ~CSIZE;	//6C	newtio.c_iflag &= ~(IXON | IXOFF | IXANY);	//设置停止位	switch( nBits )	{		case 7:				newtio.c_cflag |= CS7;				break;		case 8:				newtio.c_cflag |= CS8;				break;	}	//设置奇偶校验位	switch( nEvent )	{		case 'O'://奇数			newtio.c_cflag |= PARENB;			newtio.c_cflag |= PARODD;			newtio.c_iflag |= (INPCK|ISTRIP);			break;		case 'E'://奇数			newtio.c_iflag |= (INPCK|ISTRIP);				newtio.c_cflag |= PARENB;			newtio.c_cflag &= ~PARODD;			break;		case 'N'://奇数			newtio.c_cflag &= ~PARENB;			//6C			newtio.c_iflag &= ~INPCK;			break;	}	//设置波特率	switch( nSpeed )	{		case 9600:			cfsetispeed(&newtio, B9600);			cfsetospeed(&newtio, B9600);			break;		case 19200:			cfsetispeed(&newtio, B19200);			cfsetospeed(&newtio, B19200);			break;		case 39400:			cfsetispeed(&newtio, B38400);			cfsetospeed(&newtio, B38400);			break;		case 57600:			cfsetispeed(&newtio, B57600);			cfsetospeed(&newtio, B57600);			break;					case 115200:			cfsetispeed(&newtio, B115200);			cfsetospeed(&newtio, B115200);			break;		default:			cfsetispeed( &newtio, B9600 );			cfsetospeed( &newtio, B9600 );			break;					}	//设置停止位	if ( nStop == 1 )		newtio.c_cflag &= ~CSTOPB;	else if ( nStop == 2 )		newtio.c_cflag |= ~CSTOPB;	//设置等待时间和最小接收字符	newtio.c_cc[VTIME] = 10;	newtio.c_cc[VMIN] = 5;	//6C	newtio.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG);	newtio.c_oflag &=~OPOST;	//处理未接收字符6B	//tcflush ( fd, TCIFLUSH );	//激活新配置	if ( ( tcsetattr ( fd, TCSANOW, &newtio ) ) != 0 ) {		qWarning( "com set error\n" );		return -1;	}	tcflush(fd,TCIOFLUSH);	table->setText(65,0,"SET-PORT over");	return 0;}int SetDataForm::open_rfid(int fd){  unsigned char rsp_data[30],rsp_data1[30];  char data[100];    int i;//RFID读写器第一次加电源时,波特率被重置为9600,所以首先应运行ioctl(fd,1),设置串口1的波特率为9600;//在write命令中设置RFID的波特率为115200,(因为 读写器接收到此命令帧后,以原来波特率返回命令完成帧后修改读写器参数以新波特率进行通信),//所以,在read中接收到RFID的响应命令后,再重新设置串口1的波特率为115200.此后,串口1与RFID读写器于波特率115200进行通信//在驱动程序中将波特率默认设为115200 table->setText(61,0, "open rfid");      for(i=0;i<30;i++)      rsp_data[i]='\0';    for(i=0;i<30;i++)       rsp_data1[i]='\0';         for(i=0;i<100;i++)    data[i]='\0';			    set_port( fd, 9600, 8, 'N', 1 );    for (i=0;i<6;i++)   {        if(cmd_open1[i]<0x10)	{	sprintf(data+i*2,"%x",0);	sprintf(data+i*2+1,"%x",cmd_open1[i]);	}	else	sprintf(data+i*2,"%x",cmd_open1[i]);   }   printf("data= %s\n", data);    write(fd,cmd_open1,sizeof(cmd_open1));    read(fd,rsp_data,sizeof(rsp_open1));    //显示rsp_data1    for(i=0;i<sizeof(rsp_open1);i++)    {        if(rsp_data[i]<0x10)	{	sprintf(data+i*2,"%x",0);	sprintf(data+i*2+1,"%x",rsp_data[i]);	}	else	sprintf(data+i*2,"%x",rsp_data[i]);   }        table->setText(59,0,data);       for(i=0;i<1000;i++){}	set_port ( fd, 115200, 8, 'N', 1 );    write(fd,cmd_open2,sizeof(cmd_open2));    read(fd,rsp_data1,sizeof(rsp_open2));   //这里读出的响应信息的最后三个字节数据好像是错的,不知道为什么?        for(i=0;i<sizeof(rsp_open2);i++)        {        if(rsp_data1[i]<0x10)	{	sprintf(data+i*2,"%X",0);	sprintf(data+i*2+1,"%X",rsp_data1[i]);	}	else	sprintf(data+i*2,"%X",rsp_data1[i]);   }      table->setText(58,0, data);    return 0;	         }//多标签ID号读取int SetDataForm::identify_multitags(){   //#if 1    qWarning("start identify");    int i,j,k;    int n=0,m=0,n1=0,m1=0,n2=0,m2=0;    char data[400], s[12];    //char tagID[50][8];//标签的ID号为8个字节		   //char rsp_data3[30],rsp_data4[200];   //int tagnum;   // char *s;    int tagnum=0;    for (i=0;i<400;i++)               {	        data[i]='\0';		}   unsigned char rsp_data1[30],rsp_data2[30],rsp_data3[200];   //char rsp_data1[30],rsp_data2[30],rsp_data3[200];      for(i=0;i<200;i++)        buf[i]='\0';		    for(i=0;i<30;i++)       rsp_data1[i]='\0';    for(i=0;i<30;i++)        rsp_data2[i]='\0';    for(i=0;i<200;i++)    	rsp_data3[i]='\0';   table->setText(55,0, " identify first");    write(g_fd,cmd_read1,sizeof(cmd_read1));    n1=read(g_fd,rsp_data1,sizeof(rsp_read1));     buf[0] = n1;        for (i = 1; i < sizeof(rsp_read1) + 1; i++)    {    	buf[i] = rsp_data1[i-1];    }    //return 0;//    table->setText(54,0,buf);   int  count = 0,OVERTIME=10;    //table->setText(7,0,"identify first");    while(n1<sizeof(rsp_read1))    {    	m1=read(g_fd,rsp_data1+n1,sizeof(rsp_read1)-n1);	if (m1 > 0)	{		n1=n1+m1;		count = 0;	}	else	{		for (int i=0; i<=1000;i++){}		count++;		if (count >= OVERTIME)		{			return -1;			 table->setText(53,0, " OVER TIME");		}		}	    }//    table->setText(52,0,rsp_data1);     qWarning("first ok ");    table->setText(51,0,"identify 2");     write(g_fd,cmd_read2,sizeof(cmd_read2));    n2=read(g_fd,rsp_data2,sizeof(rsp_read2));    if(n2<sizeof(rsp_read2))    {         	printf("while1\n");    	m2=read(g_fd,rsp_data2+n2,sizeof(rsp_read2)-n2);    	n2=n2+m2;    	printf("while2\n");    }        for(i=0;i<sizeof(rsp_read2);i++)    sprintf(data+i*2,"%x",rsp_data2[i]);        table->setText(50,0,data);        qWarning("second ok ");   tagnum = rsp_data2[4];    sprintf(data,"%x",rsp_data2[4]);    table->setText(49,0, data);   buf[0] = rsp_data2[4];   qWarning("start tagnum ");	    table->setText(48,0,"start tagnum");//table->setText(42,0,tagnum);  if(tagnum>0)  {    write(g_fd,cmd_read3,sizeof(cmd_read3));    n=read(g_fd,rsp_data3,tagnum*14+6);            while(n<(tagnum*14+6))    {      m=read(g_fd,rsp_data3+n,tagnum*14+6-n);      n=n+m;     }    for(i=0;i<tagnum;i++)     {         for(j=0;j<12;j++)	 {	  buf[i*12+j+1] = rsp_data3[j+7*(2*i+1)];	  qWarning("%x",buf[i*12+j+1]);	 	 }	      }      qWarning("tagnum ok ");	   }   //i=i+1;   //killTimer(timer);  /*for (i=0;i<73;i++)  {  buf[i]=0XAA;     if(i%10==0)buf[++i]=i%100;    qWarning("%x",buf[i]);    }   for(i=73; i<144;i++)  {  buf[i]=0XCC;     if(i%10==0)buf[++i]=i%100;    qWarning("%x",buf[i]);    }   // buf[73]='\0';   int tagnum=12;*/      table->setText(47,0,"data data");  // qWarning("buf over");    for(k=1;k<(tagnum*12)+1;k++)      {        if(buf[k]<0x10)	{	sprintf(data+(k-1)*2,"%X",0);	sprintf(data+(k-1)*2+1,"%X",buf[k]);	qWarning("%s\n",data);         }       else	{sprintf(data+(k-1)*2,"%X",buf[k]);	qWarning("%s\n",data);	}   }    qWarning("data over");    k=0;    for(i=0;i<tagnum;i++)	{ 	for (j=0;j<12;j++)	   {	   s[j]=data[i*12+j];	   }	   k=(num+i)%100;	   qWarning("k= %d" ,k);	    qWarning("s= %s" ,s);	   table->setText(k,0,s);    		}   num=num+tagnum;   qWarning("%d \n", num);  // timer=startTimer(150);   //if( first_open != 0 )      //发射数据此处send(sockfd,buf,200,0);*/       return 0;  /* #else      int i = 0;   char j = 0;   for (i = 0; i < 8; i++)   {   	buf[i] = 'A';   }*/  // return 0; //  #endif*/  }/*void userDialog::exit_func(int signo){	int i;  if(signo==SIGTERM)  {		printf("in SIGTERM function\n");	}	else if(signo==SIGINT)	{		sleep(1);		close(fd_gb1);		printf("in SIGINT function\n");		exit(0);	}}*/int main(int argc,char **argv) {QApplication app(argc,argv);SetDataForm udlg;app.setMainWidget(&udlg);udlg.show();return(app.exec());}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -