基于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 }
基于Python的简单读卡上位机界面的更多相关文章
- 【专题教程第8期】基于emWin模拟器的USB BULK上位机开发,仅需C即可,简单易实现
说明:1.如果你会emWin话的,就可以轻松制作上位机.做些通信和控制类上位机,比使用C#之类的方便程度一点不差,而且你仅会C语言就可以.2.并且成功将emWin人性化,可以做些Windows系统上的 ...
- 基于Python的信用评分卡模型分析(二)
上一篇文章基于Python的信用评分卡模型分析(一)已经介绍了信用评分卡模型的数据预处理.探索性数据分析.变量分箱和变量选择等.接下来我们将继续讨论信用评分卡的模型实现和分析,信用评分的方法和自动评分 ...
- Websocket - Websocket原理(握手、解密、加密)、基于Python实现简单示例
一.Websocket原理(握手.解密.加密) WebSocket协议是基于TCP的一种新的协议.WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符.它实 ...
- 基于Arduino和python的串口通信和上位机控制
引言 经常的时候我们要实现两个代码之间的通信,比如说两个不同不同人写的代码要对接,例如将python指令控制Arduino控件的开关,此处使用串口通信是非常方便的,下面笔者将结合自己踩过的坑来讲述下自 ...
- [python] 3 、基于串口通信的嵌入式设备上位机自动测试程序框架(简陋框架)
星期一, 20. 八月 2018 01:53上午 - beautifulzzzz 1.前言 做类似zigbee.ble mesh...无线网络节点性能测试的时候,手动操作然后看表象往往很难找出真正的原 ...
- 基于QT的全自动超声波焊接机上位机追溯系统(已经在设备上应用)
应用说明: 本上位机程序是我在做锂电池产线项目的时候开发的,用于采集设备数据以及实现设备自动控制,下位机采用基恩士PLC,超声波机采用上海一家的超声波焊接机,实现电芯极耳的自动焊接,上位在设备焊接过程 ...
- 基于Python的信用评分卡模型分析(一)
信用风险计量体系包括主体评级模型和债项评级两部分.主体评级和债项评级均有一系列评级模型组成,其中主体评级模型可用“四张卡”来表示,分别是A卡.B卡.C卡和F卡:债项评级模型通常按照主体的融资用途,分为 ...
- 基于python实现简单web服务器
做web开发的你,真的熟悉web服务器处理机制吗? 分析请求数据 下面是一段原始的请求数据: b'GET / HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nConnectio ...
- 基于Python 的简单推荐系统
def loadExData(): return[[1,1,1,0,0], [2,2,2,0,0], [1,1,1,0,0], [5,5,5,0,0], [1,1,0,2,2], [0,0,0,3,3 ...
- VC++编写简单串口上位机程序
VC++编写简单串口上位机程序 转载: http://blog.sina.com.cn/s/articlelist_1809084904_0_1.html VC++编写简单串口上位机程序 串口通信 ...
随机推荐
- apollo兼容mysql的时候报错Table 'apolloportaldb.hibernate_sequence' doesn't exist
解决方案: 配置文件里加: spring.jpa.properties.hibernate.hbm2ddl.auto=update
- Recycle Queue Sample
public class RecycleQueue<T> { public int len; T[] v; int max; int front; int rear; public Rec ...
- 移动web_平面转换
平面转换 平面 平移 旋转 缩放 概念:使用transform属性实现元素的位移.旋转.缩放等效果 注意点:行内元素所有的平面转换是没有效果的 平面: 平面转换的平面指的是二维平面(2D)只有X和Y轴 ...
- python生成一个WAV文件的正弦波
import numpy as np import matplotlib.pyplot as plt T = 1.0 / sample_rate #周期 x = np.arange(0, 1.0, T ...
- Python-闭包(Closure)
一.认识闭包 有时候需要在函数外部得到函数内部的局部变量,但是由于Python作用域的关系,这一点是无法实现的. def f(): n = 22 print(n) #NameError:name 'n ...
- zookeeper在关闭服务时报could not find file /opt/module/zookeeper-3.5.10/zkData/zookeeper_server.pid
遇到了个问题,zookeeper在执行 bin/zkServer.sh stop时 会报题目中的错误,我搜了一下博客,好像是因为第一次启动的不是zkServer.sh中的服务 我先 kill -9 加 ...
- 前端导出文件流[object Object]
情景:后台返回文件流,前端导出. 参照网上的文章配置responseType:'blob' :blob导出文件乱码_前端小菜鸟__简单的博客-CSDN博客_blob导出乱码 后台管理项目blob导出文 ...
- 在windows上远程linux (待完善)
一.准备工具 windows linux 系统 win10 centos7 软件 远程桌面连接(自带) xrdp(epel库提供):开源的远程桌面协议(RDP)服务 二.Linux(被连接端) 2 ...
- 关于安装hadoop时在centos上修改主机名总是不成功
按照老师给的文件和网上搜的代码改了很多次,比如改/etc/sysconfig/network这些,无论改几次都没用,找了个帖子,说可能是因为 CentOS7版本由于与之前版本变化比较大,修改主机名的方 ...
- Vue watch监听 date中的变量 与 数组或者对象的数据变化
直接看下面代码: 1.红色的的为一个对象,watch监听时.需要借助 computed 属性,否则watch监听打印出来的新旧值看不出.(注:方法可以随便写,但是 computed 中 与 watch ...