使用pyqt5制作GUI(来自我的传感器的数据)

时间:2019-01-15 19:05:39

标签: python pyqt pyqt5 sensor

我本周刚刚开始尝试python,我急切地希望获得一些帮助(请不要判断我,请客气),我有一个传感器(X4M300存在传感器),我需要使用以下命令在GUI中显示其数据蟒蛇。我已经安装了pyqt5和qt设计器。

首先,我有一个python脚本,可以在此处打印出传感器的信息,例如其版本,固件ID,序列号等。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" \example X4M200_X4M300_printout_infromation.py

#Target module: X4M200,X4M300

#Introduction: This is an example of how to use the XEP interface from python to get module infromation.

#Command to run: "python X4M200_X4M300_printout_infromation.py -d com8" or "python3 X4M200_X4M300_printout_infromation.py -d com8"
                 Using TCP server address as device name is also supported, e.g. 
                 "python X4M200_sleep_record.py -d tcp://192.168.1.169:3000".


"""
from __future__ import print_function

import pymoduleconnector
from pymoduleconnector import ModuleConnector
from pymoduleconnector.ids import *
from optparse import OptionParser
import sys
import time


def try_xep(device_name):

    log_level = 0
    mc = ModuleConnector(device_name, log_level)
    x4m300 = mc.get_x4m300()

    # we have to go to manual mode
    x4m300.set_sensor_mode(XTS_SM_STOP, 0)
    x4m300.set_sensor_mode(XTS_SM_MANUAL, 0);

    xep = mc.get_xep()
    pong = xep.ping()
    print("Received pong:", hex(pong))

    print('ItemNumber =', xep.get_system_info(XTID_SSIC_ITEMNUMBER));
    print('OrderCode =', xep.get_system_info(XTID_SSIC_ORDERCODE));
    print('FirmWareID =', xep.get_system_info(XTID_SSIC_FIRMWAREID));
    print('Version =', xep.get_system_info(XTID_SSIC_VERSION));
    print('Build =', xep.get_system_info(XTID_SSIC_BUILD));
    print('SerialNumber =', xep.get_system_info(XTID_SSIC_SERIALNUMBER));
    print('VersionList =', xep.get_system_info(XTID_SSIC_VERSIONLIST));

    # inti x4driver
    xep.x4driver_init()

    # Set enable pin
    xep.x4driver_set_enable(1);

    # Set iterations
    xep.x4driver_set_iterations(16);
    # Set pulses per step
    xep.x4driver_set_pulses_per_step(256);
    # Set dac min
    xep.x4driver_set_dac_min(949);
    # Set dac max
    xep.x4driver_set_dac_max(1100);
    # Set TX power
    xep.x4driver_set_tx_power(2);

    # Enable downconversion
    xep.x4driver_set_downconversion(1);

    # Set frame area offset
    xep.x4driver_set_frame_area_offset(0.18)
    offset = xep.x4driver_get_frame_area_offset()
    print('x4driver_get_frame_area_offset returned: ', offset)


    # Set frame area
    xep.x4driver_set_frame_area(2,6)
    frame_area = xep.x4driver_get_frame_area()
    print('x4driver_get_frame_area returned: [', frame_area.start, ', ', frame_area.end, ']');

    # Set TX center freq
    xep.x4driver_set_tx_center_frequency(3);

    # Set PRFdiv
    xep.x4driver_set_prf_div(16)
    prf_div = xep.x4driver_get_prf_div()
    print('x4driver_get_prf_div returned: ', prf_div)


    # Start streaming
    xep.x4driver_set_fps(20)
    fps = xep.x4driver_get_fps()
    print('xep_x4driver_get_fps returned: ' ,fps)

    # Wait 5 sec.
    time.sleep(5)

    # Stop streaming
    xep.x4driver_set_fps(0);

    # Read data float if available.
    if xep.peek_message_data_float() > 0:
        data_float = xep.read_message_data_float()
    else:
        print('No data float messages available.')

    # Reset module
    xep.module_reset()


def main():
    parser = OptionParser()
    parser.add_option(
        "-d",
        "--device",
        dest="device_name",
        help="device file to use",
        metavar="FILE")

    (options, args) = parser.parse_args()

    if not options.device_name:
        print("you have to specify device, e.g.: python %s -d /dev/ttyACM0" % sys.argv[0])
        sys.exit(1)

    try_xep(options.device_name)

if __name__ == "__main__":
    main()

在Qt设计器中,我希望GUI如下所示:

在Qt Designer中创建的示例GUI

到目前为止,这是我的GUI代码:

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic

Ui_MainWindow, QtBaseClass = uic.loadUiType("x4m300gui1.ui")

class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.connect_button.clicked.connect(self.ConnectModule)

    def ConnectModule(self):
        itemnumber_string = "" 
        ordercode_string = ""
        firmwareid_string = ""
        version_string = ""
        serialnumber_string = ""

        self.ui.itemnumber_box.setText(itemnumber_string)
        self.ui.ordercode_box.setText(ordercode_string)
        self.ui.firmwareid_box.setText(firmwareid_string)
        self.ui.version_box.setText(version_string)
        self.ui.serialnumber_box.setText(serialnumber_string)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

我想知道在单击“ connect_button”按钮后如何在gui中显示传感器信息。我将不胜感激。非常感谢。

2 个答案:

答案 0 :(得分:0)

好像您想从try_xep调用ConnectModule过程。让try_xep返回您想要的信息,然后将其写入ui。

答案 1 :(得分:0)

您似乎正在尝试将传感器采样和GUI作为两个应用程序运行,它们将需要一个命名管道或共享文件来访问彼此的内存。为简单起见,您应该将其作为单个应用程序运行。

我将采用经典的OOP方法,并创建一个代表传感器的类

class X4M300():
    def __init__(self, options):
        # Initialise things required to sample sensor including the device options
        # Also get the device information and store it in local variables
        # Create a local variable that stores the sensor value
    def get_sample():
        # sequence of things required to trigger sensor to start sampling, get sample and save the value into the local variable, essentially the try_xep() method.

然后在PyQt应用程序的main方法内创建sensor类的实例,并将选项作为参数传递。

sensor = X4M300(options)

然后您可以执行以下操作:

def ConnectModule(self):
    sensor.get_sample()

    self.ui.itemnumber_box.setText(sensor.itemnumber)
    self.ui.ordercode_box.setText(sensor.ordercode)
    self.ui.firmwareid_box.setText(sensor.firmwareid)
    self.ui.version_box.setText(sensor.version)
    self.ui.serialnumber_box.setText(sensor.serialnumber)