《2022年用于mini与周立功非接触式ic卡模块通信.docx》由会员分享,可在线阅读,更多相关《2022年用于mini与周立功非接触式ic卡模块通信.docx(26页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选学习资料 - - - - - - - - - 一 个 在linux下 的 串 口 程 序 例 子用 于个人资料整理仅限学习使用ic卡 模 块 的 通 信mini2440与 周 立 功 非 接 触 式头Cardreader 件文#include #include #include #include #include #include #include : public #include #include #include #include class QThread Q_OBJECT public: Cardreaderfd_portcomportnStop; int ; int open_
2、portint ; int set_optint fd,int nSpeed, int nBits, char nEvent, int ; int initcardreaderint fd_port; unsigned int checkcardint fd_port; unsigned int readcardid int fd_port; int beepint fd_port; void autorunvoiddata void sendmessageint ;signals: void messageQString;protected: private: ;cpp文cardreader
3、件.h #include Cardreader:Cardreader 1 / 13 名师归纳总结 - - - - - - -第 1 页,共 13 页精选学习资料 - - - - - - - - - 个人资料整理 仅限学习使用 int Cardreader:open_portint comport /* 打开端口成功返回文件描述符失败返回0*/ /*open COM Port*/ /* comport=1 open COM1 comport=2 open COM2 comport=3 open COM3*/ /char *dev=/dev/ttySAC0,/dev/ttySAC1,/dev/tt
4、ySAC2; long vdisable;fd; int if comport=1 fd = open /dev/ttySAC0, O_RDWR|O_NOCTTY|O_NDELAY; if 0 = fd emit messageOpen port error 1; return0 else = open /dev/ttySAC1, ifcomport=2 ; fd O_RDWR|O_NOCTTY|O_NDELAYfd if 0 = return0emit messageOpen port error 1; else if comport=3 fd = open /dev/ttySAC2, O_
5、RDWR|O_NOCTTY|O_NDELAY;fd if 0 = return0emit messageOpen port error 1; iffcntlfd, F_SETFL, 0 return0emit messageOpen port error 2; ifisattySTDIN_FILENO=0 emit messageOpen port error 3;2 / 13 名师归纳总结 - - - - - - -第 2 页,共 13 页精选学习资料 - - - - - - - - - / 个人资料整理仅限学习使用;return0 return fd; int Cardreader:set
6、_optint fd,int nSpeed, int nBits, char nEvent, int nStop /* 端口设置,成功返回1,失败返回0 */ of /*set patmeter COM port*/ /*fd:file descript of COM port nSpeed:baut O:odd data rate nBits:unmber of bits nEven:PARE mode E:even N:none nStop:stop | newtio,oldtiobits*/ struct termios ; if tcgetattr fd,&oldtio .= 0 0
7、return ; bzero &newtio, sizeof newtio ; newtio.c_cflag |= CLOCAL CREAD; newtio.c_cflag &= CSIZEnBits switch case |= CS7;/7 bits 7: newtio.c_cflag data ; break; case |= CS8/8 bits 8: newtio.c_cflag data break; switch nEvent case |= |= INPCK PARENBISTRIPO:/odd newtio.c_cflag ; newtio.c_cflag |= PARODD
8、 newtio.c_iflag | ;|= ; breakE: case |= | ISTRIP/even newtio.c_iflag INPCK ; newtio.c_cflag PARENB; newtio.c_cflag &= PARODD; break3 / 13 名师归纳总结 - - - - - - -第 3 页,共 13 页精选学习资料 - - - - - - - - - case 个人资料整理仅限学习使用/none N: newtio.c_cflag &= PARENB; break switch nSpeed case B24002400: cfsetispeed&newti
9、o, ; cfsetospeed&newtio, B2400; break case B48004800: cfsetispeed&newtio, ; cfsetospeed&newtio, B4800; break case B960091000: cfsetispeed&newtio, ; cfsetospeed&newtio, B9600; break case B57600571000: cfsetispeed&newtio, ; cfsetospeed&newtio, B57600; break case B115200115200: cfsetispeed&newtio, ; cf
10、setospeed&newtio, B115200; break default: cfsetispeed&newtio, B9600; cfsetospeed&newtio, B9600; break if nStop = = 01 2 newtio.c_cflag &= CSTOPB; else if nStop newtio.c_cflag ,&newtio.=0 |= CSTOPB; newtio.c_ccVTIME = 0; newtio.c_ccVMIN = ; tcflushfd,TCIFLUSH; iftcsetattrfd,TCSANOW return 0; return 1
11、 int Cardreader:initcardreaderint fd_port 4 / 13 名师归纳总结 - - - - - - -第 4 页,共 13 页精选学习资料 - - - - - - - - - 个人资料整理 仅限学习使用 /* 读卡器初始化,成功返回否1,失败返回清0 */ char readnum冲区 unsigned buffer32; int ;int timer unsigned ;/buffer memsetbuffer,0,sizeofbuffer缓0 回符出错退 /发送开始0x20 buffer0=0x20; writefd_port,buffer,1;应就 /
12、等待0x06出;/等待区清40ms usleep40*1000 timer=0;/buffer 缓冲 memsetbuffer,0,sizeofbuffer0 do readnum=readfd_port,buffer,sizeofbuffer就退; usleep100 timer+等待超时; if timer1000/出 return 0;while readnum=0;buffer0.=0x06 ; if return 0 /0xad 0x03 发送0x00 0x52 0x00 /usleep40*1000; buffer1=0x52;/等出待40ms buffer0=0x00; buff
13、er2=0x00; buffer3=0xad; buffer4=0x03; writefd_port,buffer,5;回应否就错退 /等待0x20出;/等冲待50ms usleep40*1000,0,sizeofbuffer memsetbuffer;/buffer 缓区清0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer超时就退; usleep100 timer+等待; if timer1500/出0buffer0.=0x20 ; return while readnum=0; if 5 / 13 名师归纳总结 - - - - - -
14、 -第 5 页,共 13 页精选学习资料 - - - - - - - - - return 发个人资料整理仅限学习使用就出错;0送 /0x06 ,10xff 0x03回应否 buffer0=0x06; writefd_port,buffer; /等待0x00 0x00 0x00 退出等待区清40ms usleep40*1000;/ memsetbuffer,0,sizeofbuffer;/buffer 缓冲0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer超时就退; usleep100 timer+等待; if timer1000/出0;
15、 return ;while readnum=0 if buffer0.=0x00|buffer1.=0x00|buffer2.=0x00|buffer3.=0xff|buffer4.= 0x03 return ;回开/0否等符出待错退;0x20 /发送始 buffer0=0x20; writefd_port,buffer,1;应就 /等待0x06出;/buffer 缓冲区清40ms usleep40*1000 memsetbuffer,0,sizeofbuffer0 timer=0; do readnum=readfd_port,buffer08 ,sizeofbuffer超时ff 就退;
16、usleep100 timer+等待; if timer1000/出00 00 0buffer0.=0x06 ; return while readnum=0; if 0; return ff ff bb /发送00 4c ff ff ff 03 buffer0=0x00; buffer1=0x4c; buffer2=0x08; buffer3=0x00; buffer4=0x00;buffer5=0xffbuffer6=0xff;6 / 13 名师归纳总结 - - - - - - -第 6 页,共 13 页精选学习资料 - - - - - - - - - buffer7=0xff; buff
17、er8=0xff;个人资料整理仅限学习使用; buffer9=0xff; buffer10=0xff; buffer11=0xbb;buffer12=0x03,13 writefd_port,buffer;回应否就出错退 /等待0x20出/等待40ms usleep40*1000,0,sizeofbuffer memsetbuffer;/buffer 缓冲区清0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer时否; usleep100 timer+等待超; if timer1000/就退出0; return ; while readnum
18、=0buffer0.=0x20 if 0 return ;0x06 /发送0xff 0x03回应 buffer0=0x06; writefd_port,buffer,1; /等待0x00 0x00 0x00 就出错退出冲待40ms usleep40*1000;/等 memsetbuffer,0,sizeofbuffer;/buffer 缓区清0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer超时就退; usleep100 timer+等待; if timer1000/出0; return ;while readnum=0 if buffe
19、r0.=0x00|buffer1.=0x00|buffer2.=0x00|buffer3.=0xff|buffer4.=0x03 return 10; return unsigned int Cardreader:checkcardint fd_port /* 检查天线范围内有无卡,有返回1,无或失败返回0 */ 7 / 13 名师归纳总结 - - - - - - -第 7 页,共 13 页精选学习资料 - - - - - - - - - unsigned char 个人资料整理仅限学习使用待区清;buffer32readnum; int int timer unsigned ;/buffer
20、 memsetbuffer,0,sizeofbuffer缓冲0 回符错退 /发送开始0x20 buffer0=0x20; writefd_port,buffer,1;应否就出 /等待0x06出;/等区清40ms usleep40*1000 timer=0;/buffer 缓冲 memsetbuffer,0,sizeofbuffer0 do readnum=readfd_port,buffer,sizeofbuffer超时error 就退; usleep100 timer+等待; if timer1000/出messagecard found 1; /emit ; return 0 while
21、readnum=0buffer0.=0x06 ; if emit messagecard found 0error 2; return /发送;00 41 01 01 be 03 ; buffer1=0x41 buffer0=0x00; buffer2=0x01; buffer3=0x01; buffer4=0xbe;buffer5=0x03;,6 writefd_port,buffer出;回应否就错退 /等待0x20出/等冲待50ms usleep40*1000,0,sizeofbuffer memsetbuffer;/buffer 缓区清0 timer=0; do readnum=read
22、fd_port,buffer,sizeofbuffer0超时就退; usleep100 timer+等待; if timer1000/出; return 8 / 13 名师归纳总结 - - - - - - -第 8 页,共 13 页精选学习资料 - - - - - - - - - while 个人资料整理仅限学习使用buffer0.=0x20 ;readnum=0 if emit messagecard found 0error 3; return /发0x02 /0x04 送缓0xf9 待0x03清回0x06 ,10x00 buffer0=0x06; writefd_port,buffer;
23、 /等待0x00 0x00 应40ms usleep50*1000;等;/buffer 冲区 memsetbuffer,0,sizeofbuffer0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer超时就退; usleep100 timer+等待; if timer1000/出 emit messagecard found 0error 4; return while readnum=0; if buffer0.=0x00|buffer1.=0x00|buffer2.=0x02|buffer3.=0x04|buffer4.=0x00|bu
24、ffer5.=0xf9|buffer6.=0x03 /emit messagecard found error 5; return 0 return 1; unsigned int Cardreader:readcardid int fd_port /* 读取卡id号码,成功返回32位整数,无或失败返回0 */ char ;buffer32冲区清 unsigned ;readnum int ;int timer unsigned ;,0,sizeofbuffer unsigned int result;/buffer memsetbuffer缓0 9 / 13 名师归纳总结 - - - - -
25、 - -第 9 页,共 13 页精选学习资料 - - - - - - - - - /发送开个人资料整理仅限学习使用0x20 始符 buffer0=0x20,1;回应/buffer 否等就出待错退; writefd_port,buffer出 /等待0x06;缓冲区清40ms usleep40*1000 timer=0,0,sizeofbuffer; memsetbuffer0 do readnum=readfd_port,buffer,sizeofbuffer; usleep100 timer+等待超时; if timer1000/就退出0; return ; while readnum=0bu
26、ffer0.=0x06 if 0 return ;00 8c 03 /发送00 71 02 00 buffer0=0x00; buffer1=0x71; buffer2=0x02; buffer3=0x00; buffer4=0x00;buffer5=0x8c;buffer6=0x03; writefd_port,buffer,7回应否就 /等待0x20出错退出;/等待50ms usleep40*1000 memsetbuffer,0,sizeofbuffer;/buffer 缓冲区清0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer0超
27、时; usleep100 timer+等待; if timer1000/就退出发0. ; return while readnum=0; if buffer0.=0x20 return ;送 /0x06 buffer0=0x06; writefd_port,buffer,1;0x00 0x04 0x04 /等待0x00 0x03回应 usleep40*1000;/等待40ms 10 / 13 名师归纳总结 - - - - - - -第 10 页,共 13 页精选学习资料 - - - - - - - - - memsetbuffer,0,sizeofbuffer;个人资料整理仅限学习使用区清0
28、/buffer 缓冲 timer=0; do readnum=readfd_port,buffer,sizeofbuffer就退; usleep100; timer+出 if timer1000/等待超时; return 0; while readnum=0; if buffer0.=0x00|buffer1.=0x00|buffer2.=0x04|buffer8.=0x03 return 0; result=0; result=buffer3; result+=buffer48; result+=buffer516; result+=buffer6 /* 蜂鸣器响,成功返回32位整数,无或失
29、败返回0 */ 否buffer32冲待区清 unsigned char ; int readnum;timer unsigned int ; unsigned int result; memsetbuffer,0,sizeofbuffer;/buffer 缓0 /发送开始符0x20 buffer0=0x20;出错退 writefd_port,buffer,1;回应就 /等待0x06出 usleep40*1000;/等40ms 缓冲区清 timer=0;/buffer memsetbuffer,0,sizeofbuffer0 do readnum=readfd_port,buffer,sizeo
30、fbuffer0超时就退; usleep100 timer+等待; if timer1000/出; return 11 / 13 名师归纳总结 - - - - - - -第 11 页,共 13 页精选学习资料 - - - - - - - - - return 送while 60 个人资料整理仅限学习使用buffer0.=0x06 ;readnum=0 if 0; /00 01 90 03 发04 0a 19 19 buffer0=0x00; buffer1=0x60; buffer2=0x04; buffer3=0x0a出; buffer4=0x19;buffer5=0x19;buffer6=
31、0x01;buffer7=0x90;buffer8=0x03; writefd_port,buffer,9回应否就错退 /等待0x20出/等冲待50ms usleep40*1000; memsetbuffer,0,sizeofbuffer;/buffer 缓区清0 timer=0; do readnum=readfd_port,buffer,sizeofbuffer时冲; usleep100 timer+等待超; if timer1000/就退出0; return ; while readnum=0buffer0.=0x20 if 0 return ;0x06 /发送0x00 0x00 0xff buffer0=0x06; writefd_port,buffer,1; /等待0x00 0x03回应 usleep40*1000;/等待40ms memsetbuffer,0,sizeofbuffer;/buffe