Python判别不同平台操作系统调用相应的动态库读写NFC
本示例使用的发卡器:https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.52de2c1bV0E4YV&ft=t&id=615391857885
import sys
import struct # struct的pack函数把任意数据类型变成字符串
import ctypes # 调用DLL动态库要有这个引用if __name__ == "__main__":app = QtWidgets.QApplication(sys.argv)mainWindow = MainWindow()screen = QDesktopWidget().screenGeometry()size = mainWindow.geometry()# 获得窗口相关坐标newLeft = (screen.width() - size.width()) // 2newTop = (screen.height() - size.height()) // 2# 移动窗口使其居中mainWindow.pushButton_clearbuff.setStyleSheet("color:red;")mainWindow.pushButton_writetag.setStyleSheet("color:blue;")mainWindow.move(newLeft, newTop)if sys.platform == 'win32':# windows系统加载当前目录下的DLL库dllfile = sys.path[0] + '\OUR_MIFARE.dll'Objdll = ctypes.windll.LoadLibrary(dllfile)elif sys.platform == 'linux':# Linux加载当前目录下的so库dllfile = sys.path[0] + '/libOURMIFARE.so'Objdll = ctypes.cdll.LoadLibrary(dllfile)else:# macOS加载当前目录下的.dylib库dllfile = sys.path[0] + '/libOURMIFARE.dylib'Objdll = ctypes.cdll.LoadLibrary(dllfile)mainWindow.show()sys.exit(app.exec_())def pushButton_Url_clicked(self):languagecodestr = "en".encode('gbk')titlestr=self.lineEdit_title.text().strip().encode('gbk')uriheaderindex = self.comboBox_headindex.currentIndex()uristr=self.lineEdit_uri.text().strip().encode('gbk')if len(uristr)<1:QMessageBox.critical(self,'提示','请输入要加入写卡缓冲区的网址URI!',QMessageBox.Yes)self.lineEdit_uri.setFocus()returnelse:status = Objdll.tagbuf_adduri(languagecodestr, len(languagecodestr), titlestr, len(titlestr), uriheaderindex,uristr, len(uristr)) % 256if status == 0:self.ListBottom("网址URL加入NDEF写卡缓冲区成功,可继续添加标签信息到缓冲区,也可以将缓冲区现有信息写入标签!")else:self.ListBottom("网址URL加入NDEF写卡缓冲区失败!")