import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from UI.ui_shareMmap import Ui_ShareMServiceDlg
import mmap
class QMainDialog(QDialog, Ui_ShareMServiceDlg):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_ShareMServiceDlg()
self.ui.setupUi(self)
self.targetName = 'ShareMemoryTest'
self.ui.editTagetName.setText(self.targetName)
self.shareValue = '201400027'
self.ui.editValue.setText(self.shareValue)
self.mmap_file = None
@pyqtSlot()
def on_btnRegister_clicked(self):
print("注册")
try:
self.targetName = self.ui.editTagetName.toPlainText()
self.mmap_file = mmap.mmap(0, 1024, access=mmap.ACCESS_WRITE, tagname=self.targetName)
self.ui.labelResult.setText('注册成功')
except Exception as e:
print(e)
self.ui.labelResult.setText('注册失败')
@pyqtSlot()
def on_btnWrite_clicked(self):
print("写入")
try:
code_text = self.ui.editValue.toPlainText()
code_text_b = code_text.encode('utf-8')
self.mmap_file.seek(0)
self.mmap_file.write(code_text_b)
print(code_text)
self.ui.labelResult.setText('写入成功')
except Exception as e:
print(e)
self.ui.labelResult.setText('写入失败')
@pyqtSlot()
def on_btnRead_clicked(self):
print("读取")
try:
self.mmap_file.seek(0)
info_str = self.mmap_file.read().translate(None, b'\x00').decode()
print(info_str)
self.ui.editValue.setText(info_str)
self.ui.labelResult.setText('读取成功')
except Exception as e:
print(e)
self.ui.labelResult.setText('读取失败')
if __name__ == '__main__':
app = QApplication(sys.argv)
main_form = QMainDialog()
main_form.show()
sys.exit(app.exec_())