基于Python的简单读卡上位机界面
使用到的模块,pyserial pyqt5 pymysql
(我先是实现在命令行下通过串口进行对板子硬件的控制,比如Led ,beep等,在实现检测卡(是否选卡成功),连接卡(验证哪一个扇区),读卡,写卡)(对于m1卡的读写地址,在连接卡时就指定了,在没有halt操作或移开卡的操作之前,是没法更改扇区的)
后续慢慢加。。
先传几张,调试好的图(第三张图的 读区 显示少了一个s是因为在写的时候只写16个字符(不满16我直接填0了)
(所有于卡有关的操作都是通过硬件实现,程序只是实现串口交互和命令定义而已
界面的使用过程:1.先是(不必须)点击Check按钮,在INFO TIPS富文本区会显示当前选卡与否,选卡成功则显示 MIF card is ok(选卡是通过硬件操作的)
2.必须连接(Connect)成功才能读写操作,先输入扇区号,我这里是1号扇区,然后点Connect botton 如果验证成功 返回 验证成功
3.读操作和写操作必须先输入地址或数据,然后在点击Botton
贴上相关代码,仅供参考(注释没怎么搞,代码有过修改,所有有的地方注释还是错的,请不要介意)
需要注意的是,对于界面交互的程序而言,需要做到,界面文件和业务逻辑文件分开,比如界面文件demo.py(一般由demo.ui转过来),业务逻辑文件:calldemo.py
当然这下面的代码,缺少一个serconfig的文件(这个就因人而异了)还有我只给出C代码的命令处理的程序,只提供一个思路而已
还有一个扩展的功能,我没做,就是MIF_HALT这个功能,可以实现,不用移卡就切换扇区(当然这个命令是暂停卡的功能)
主界面的代码
1 import pyserial 2 import serconfig 3 import sys 4 from PyQt5.QtWidgets import QApplication,QMainWindow 5 #from PyQt5 import QtCore 6 #from test import * 7 from serguyui import * 8 from serconfig import command_win_code as commands #0:read 1:write 2:check 3:found 9 10 class MainWindow(QMainWindow,Ui_MainWindow): 11 def __init__(self,serguy,parent=None): 12 super(MainWindow,self).__init__(parent) 13 self.serguy=serguy 14 self.setupUi(self) 15 self.lineEdit.setPlaceholderText("enter section") 16 self.lineEdit_2.setPlaceholderText("enter read address") 17 self.lineEdit_3.setPlaceholderText("enter write address") 18 self.lineEdit_4.setPlaceholderText("enter data") 19 self.pushButton_3.clicked.connect(self.Connect_Btn_Handler) 20 self.pushButton.clicked.connect(self.Read_Btn_Handler) 21 self.pushButton_2.clicked.connect(self.Write_Btn_Handler) 22 self.pushButton_4.clicked.connect(self.Check_Btn_Handler) 23 self.pushButton_5.clicked.connect(self.Close_Btn_Handler) 24 self.read_mode="str" 25 self.radioButton.setChecked(True) 26 self.radioButton.toggled.connect(lambda :self.Read_Mode_Set(self.radioButton)) 27 self.radioButton_2.toggled.connect(lambda :self.Read_Mode_Set(self.radioButton_2)) 28 29 def Read_Mode_Set(self,btn): 30 if (btn.text() == "str"): 31 if btn.isChecked() == True: 32 self.read_mode="str" 33 if (btn.text() == "hex"): 34 if btn.isChecked() == True: 35 self.read_mode="hex" 36 def Connect_Btn_Handler(self): 37 Section = self.lineEdit.text() 38 feedback=self.serguy.MIF_Connect_Handler(commands.get("found"),Section) 39 self.textBrowser.setPlainText(feedback) 40 41 def Read_Btn_Handler(self): 42 block_addr=int(self.lineEdit_2.text()) 43 mode = self.read_mode 44 feedback=self.serguy.MIF_Read_Handler(commands.get("read"),mode,block_addr) 45 self.textBrowser_2.setPlainText(feedback) 46 47 def Write_Btn_Handler(self): 48 block_addr=int(self.lineEdit_3.text()) 49 block_data=self.lineEdit_4.text() 50 feedbake=self.serguy.MIF_Write_Handler(commands.get("write"),block_addr,block_data) 51 self.textBrowser.setPlainText(feedbake) 52 53 def Check_Btn_Handler(self): 54 feedback=self.serguy.MIF_Check_Handler(commands.get("check")) #attent the length of tuple 55 self.textBrowser.setPlainText(feedback) #feedback expect str type 56 57 def Close_Btn_Handler(self): 58 self.serguy.Close_serial() 59 self.close() 60 def Halt_Btn_Handler(self): 61 self.serguy.MIF_Halt_Handler(commands.get("halt")) 62 63 64 class CommandHandler(pyserial.Serial_Handler): 65 def __init__(self,port,baud,timeout): 66 super(CommandHandler, self).__init__(port, baud, timeout) 67 #the function of the communication with mifate card following: 68 def LedHandler(self, command_formal): 69 if (commands.get(command_formal[0], False)): 70 ctlobj = commands.get(command_formal[0]) 71 # print(ctlobj,type(ctlobj)) 72 if (ctlobj.get(command_formal[1], False)): 73 actflag = ctlobj[command_formal[1]] 74 self.Write_To_Serial(actflag[0].encode("utf-8")) 75 self.Write_To_Serial(actflag[1].encode("utf-8")) 76 self.Write_To_Serial(actflag[2].encode("utf-8")) 77 self.Write_To_Serial('\n'.encode("utf-8")) 78 else: 79 print("action singal is wrong") 80 else: 81 print("command singal is wrong") 82 83 # 0:read 1:write 2:check 3:found 84 def MIF_Read_Handler(self, command_formal,mode,block_addr): 85 self.Write_To_Serial(command_formal[0].encode("utf-8")) # send the 'M' flag 86 self.Write_To_Serial(command_formal[1].encode("utf-8")) # send the read/write flag 87 self.Write_To_Serial(chr(command_formal[2]).encode("utf-8")) # send the datalen 88 self.Write_To_Serial(chr(block_addr).encode("utf-8")) # send the block addr 89 self.Write_To_Serial('\n'.encode("utf-8")) # send the end of flag 90 data=self.Win_Read_From_Serial(mode) 91 return data 92 93 def MIF_Write_Handler(self, command_formal,block_addr,block_data): 94 if (len(block_data) < 16): 95 for i in range(len(block_data), 16): 96 block_data += "0" # write 0 when the length of data less then 16 97 self.Write_To_Serial(command_formal[0].encode("utf-8")) # send the 'M' flag 98 self.Write_To_Serial(command_formal[1].encode("utf-8")) # send the read/write flag 99 self.Write_To_Serial(chr(command_formal[2]).encode("utf-8")) # send the datalen 100 self.Write_To_Serial(chr(block_addr).encode("utf-8")) # send the block addr 101 self.Write_To_Serial(block_data.encode("utf-8")) # send the block addr 102 self.Write_To_Serial('\n'.encode("utf-8")) # send the end of flag 103 data=self.Win_Read_From_Serial("str") 104 return data 105 106 def MIF_Check_Handler(self, command_formal): 107 self.Write_To_Serial(command_formal[0].encode("utf-8")) # send the 'M' flag 108 self.Write_To_Serial(command_formal[1].encode("utf-8")) # send the read/write flag 109 self.Write_To_Serial('\n'.encode("utf-8")) # send the end of flag 110 info=self.Win_Read_From_Serial("str") 111 return info 112 113 def MIF_Connect_Handler(self, command_formal,section): 114 self.Write_To_Serial(command_formal[0].encode("utf-8")) # send the 'C' flag 115 self.Write_To_Serial(command_formal[1].encode("utf-8")) # send the check obj 116 self.Write_To_Serial(chr(int(section)).encode("utf-8")) # send the block addr 117 self.Write_To_Serial('\n'.encode("utf-8")) # send the end of flag 118 data=self.Win_Read_From_Serial("str") 119 return data 120 121 122 def MIF_Halt_Handler(self,command_formal): 123 pass 124 125 126 if __name__ == "__main__": 127 serguy= CommandHandler(serconfig.portx, serconfig.band, serconfig.timeout) 128 app = QApplication(sys.argv) 129 mywin = MainWindow(serguy) 130 mywin.show() 131 sys.exit(app.exec_())
1 ''' 2 这是使用串口的类, 3 下面一部分代码是以前命令行用的 4 ''' 5 6 import serial 7 from serconfig import color_mode as strcolor 8 #ser = serial.Serial(portx,bps,timeout=timeout) 9 class Serial_Handler(serial.Serial): 10 def __init__(self,port,baud,timeout): 11 self.portx=port 12 self.baud=baud 13 self.timeover=timeout 14 #Serial class will use the farther's class serialbase class initially 15 #and the serialbase have a arrtrubution of @port...that issue wiil be done immediately 16 #when the expression of "port=xxx" is happend,so here is only portx instead of port 17 super(Serial_Handler,self).__init__(self.portx,self.baud,timeout=self.timeover) 18 def Win_Read_From_Serial(self,mode): 19 data = super(Serial_Handler, self).readline() 20 if mode=="str": 21 return data.decode("utf-8") 22 elif mode =="hex": 23 return self.win_show_bytes_hex(data) 24 25 def Read_From_Serial(self,mode): #,block_addr,datalen 26 #data=super(Mifare_Interface,self).read(datalen) 27 data=super(Serial_Handler,self).readline() 28 if(mode=="hex"): 29 self.print_bytes_hex(data)#print(data) #the serial data is encode by gbk 30 elif(mode=="str"): 31 print(strcolor["blue"]%data) 32 def Write_To_Serial(self,data): 33 super(Serial_Handler,self).write(data) 34 def connect_to_Serial(self): 35 super(Serial_Handler,self).open() 36 def flush_buffer(self): 37 super(Serial_Handler,self).reset_input_buffer() 38 super(Serial_Handler,self).reset_output_buffer() 39 super(Serial_Handler,self).flush() 40 def print_bytes_hex(self,chardata): 41 hexdata=["%02X" % i for i in chardata] 42 print(" ".join([strcolor["light_green"]% i for i in hexdata])) 43 def win_show_bytes_hex(self,chardata): 44 hexdata = ["%02X" % i for i in chardata] 45 return (" ".join(hexdata)) 46 def Close_serial(self): 47 super(Serial_Handler,self).close() 48 49 50 #print("serials: ",ser) 51 #print(ser.port) 52 #print(ser.baudrate) 53 def delay(): 54 for i in range(10000): 55 for i in range(1000): 56 a=0 57 def SendCommand(ser): 58 while(1): 59 command=input("please entry the command:") 60 if(compareStr(command.split()[0],"led0")): 61 if(compareStr(command.split()[1],"on")): 62 #print("led0 on") 63 ser.Write_To_M1Block(chr(0x30).encode("utf-8")) 64 ser.Write_To_M1Block(chr(0x31).encode("utf-8")) 65 continue 66 elif(compareStr(command.split()[1],"off")): 67 #print("led0 off") 68 ser.Write_To_M1Block(chr(0x30).encode("utf-8")) 69 ser.Write_To_M1Block(chr(0x30).encode("utf-8")) 70 continue 71 else: 72 print("someting is wrong") 73 elif(compareStr(command.split()[0],"led1")): 74 if(compareStr(command.split()[1],"on")): 75 #print("led1 on") 76 ser.Write_To_M1Block(chr(0x31).encode("utf-8")) 77 ser.Write_To_M1Block(chr(0x31).encode("utf-8")) 78 continue 79 elif(compareStr(command.split()[1],"off")): 80 #print("led1 off") 81 ser.Write_To_M1Block(chr(0x31).encode("utf-8")) 82 ser.Write_To_M1Block(chr(0x30).encode("utf-8")) 83 continue 84 else: 85 print("someting is wrong") 86 elif command=="exit": 87 exit(0) 88 else: 89 print("command not found") 90 #while(1): 91 #ser.write("hello".encode("utf-8")) 92 #delay() 93 def compareStr(obj1,obj2): 94 for i,v in enumerate(obj1): 95 if(obj2[i]!=v): 96 return False 97 return True 98 if __name__ == "__main__": 99 #SendCommand() 100 portx = "COM3" 101 bps = 115200 102 timeout=5 103 104 m1=Serial_Handler(portx,bps,timeout) #the serial port will be open initially 105 SendCommand(m1) 106 #m1.connect_to_M1() 107 #m1.Read_From_M1Block(0,30) 108 109 ''' 110 def SendCommand(ser): 111 while(1): 112 command=input("please entry the command:") 113 if(compareStr(command.split()[0],"led0")): 114 if(compareStr(command.split()[1],"on")): 115 #print("led0 on") 116 ser.write(chr(0x30).encode("utf-8")) 117 ser.write(chr(0x31).encode("utf-8")) 118 continue 119 elif(compareStr(command.split()[1],"off")): 120 #print("led0 off") 121 ser.write(chr(0x30).encode("utf-8")) 122 ser.write(chr(0x30).encode("utf-8")) 123 continue 124 else: 125 print("someting is wrong") 126 elif(compareStr(command.split()[0],"led1")): 127 if(compareStr(command.split()[1],"on")): 128 #print("led1 on") 129 ser.write(chr(0x31).encode("utf-8")) 130 ser.write(chr(0x31).encode("utf-8")) 131 continue 132 elif(compareStr(command.split()[1],"off")): 133 #print("led1 off") 134 ser.write(chr(0x31).encode("utf-8")) 135 ser.write(chr(0x30).encode("utf-8")) 136 continue 137 else: 138 print("someting is wrong") 139 elif command=="exit": 140 exit(0) 141 else: 142 print("command not found") 143 '''
1 //关于命令处理的程序 2 #include "led.h" 3 #include "serguys.h" 4 #include "FM1702.h" 5 #include "seriels.h" 6 #include "spi.h" 7 //static u8 str_to_char(u8 *str){ 8 // u8 decode[]={0x00,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F}; 9 // u8 dechar,tmp; 10 // if((str[0]>=0x30)&&(str[0]<=0x39)) dechar=str[0]-0x30; 11 // else dechar=str[0]&0x0f; 12 // if((str[1]>=0x30)&&(str[1]<=0x39)) tmp=str[1]-0x30; 13 // else tmp=str[1]&0x0f; 14 // dechar=(dechar<<4)+tmp; 15 // return dechar; 16 //} 17 18 19 static void ledctl(u8 *command_buffer){ 20 u8 tmp; 21 tmp=*command_buffer; 22 switch(tmp){ 23 case 0x30:{ 24 tmp=*(command_buffer+1); 25 if(tmp==0x31) LED0=0; 26 else LED0=1; 27 break; 28 } 29 case 0x31:{ 30 tmp=*(command_buffer+1); 31 if(tmp==0x31) LED1=0; 32 else LED1=1; 33 break; 34 } 35 default:break; 36 } 37 } 38 39 static void MIF_Read_Handler(u8 *command_buffer){ 40 u8 block_addr,status,datalen; 41 42 //u8 uid[5]={0xdb,0x31,0x45,0xbc}; 43 44 datalen=*command_buffer; 45 block_addr=*(command_buffer+1); 46 // section=*(command_buffer+2); 47 status=MIF_READ(command_buffer,block_addr); 48 if(status==FM1702_OK) { 49 Send_16bytes(command_buffer); 50 SendString("\n"); 51 return; 52 } 53 else { 54 SendString("mifare card read fail\n"); 55 return; 56 } 57 } 58 static void MIF_Write_Handler(u8 *command_buffer){ 59 u8 block_addr,status,datalen; 60 datalen=*command_buffer; 61 block_addr=*(command_buffer+1); 62 status=MIF_Write(&command_buffer[2],block_addr); 63 if(status==FM1702_OK) { 64 SendString("mifare card write is ok \n"); 65 return; 66 } 67 else { 68 SendString("mifare card write fail\n"); 69 return; 70 } 71 } 72 static void MIF_Handler(u8 *command_buffer){ 73 u8 tmp; 74 tmp=*command_buffer; 75 switch(tmp){ 76 case '0':MIF_Read_Handler(&command_buffer[1]);break; 77 case '1':MIF_Write_Handler(&command_buffer[1]);break; 78 default:break; 79 } 80 return; 81 } 82 83 static void MIF_Check_Handler(u8 *command_buffer){ 84 u8 tmp; 85 tmp=*command_buffer; 86 switch(tmp){ 87 case 'm':{ 88 if(check_mif_flag) SendString("MIF Card is ok\n"); 89 else SendString("MIF Card is fail\n"); 90 break; 91 } 92 default:break; 93 } 94 } 95 96 static void MIF_Connect_Handler (u8 *command_buffer){ 97 u8 status,section; 98 u8 key[6],i; 99 u8 tmp; 100 tmp=*command_buffer; 101 section=*(command_buffer+1); 102 switch(tmp){ 103 case 'm':{ 104 if(check_conn_flag) { 105 SendString("MIF Card has been connected\n"); 106 return; 107 } 108 else { 109 for(i=0;i<6;i++) key[i]=0xff; 110 status=LoadKeyCPY(key); 111 if(status==FM1702_OK) SendString("LoadKey is ok "); //sthe card have to autenticate at initial and only one time 112 else{ 113 SendString("LoadKey fail \n"); 114 return; 115 } 116 status=Authentication(UID,section,RF_CMD_AUTH_LA); 117 if(status==FM1702_OK) SendString("Authentication is ok "); 118 else if(status==FM1702_NOTAGERR){ 119 SendString("mifare card test fail \n"); 120 return; 121 } 122 else { 123 SendString("mifare card Authent fail \n"); 124 return; 125 } 126 SendString("MIF Card connect is ok\n"); 127 check_conn_flag=1; 128 return; 129 } 130 } 131 default:break; 132 } 133 } 134 void Command_Handler(u8 *command_buffer){ 135 switch((*command_buffer)){ 136 case 'L':ledctl(&command_buffer[1]);break; 137 case 'M':{ 138 if(check_conn_flag){ 139 MIF_Handler(&command_buffer[1]); 140 break; 141 } 142 else SendString("MIF Card connect have not be done\n");break; 143 } 144 case 'C':MIF_Check_Handler(&command_buffer[1]);break; 145 case 'F':MIF_Connect_Handler(&command_buffer[1]);break; 146 default:break; 147 } 148 return; 149 }
本文来自博客园,作者:pie_thn,转载请注明原文链接:https://www.cnblogs.com/pie-o/p/13199809.html