2017-06-26 9 views
1

私はPythonには初めてです。私はPython 3.6(pyqt5)を使ってQtでGUIを開発しています。私はデバイスとのシリアル接続を確立する接続ボタンがあります。 GUIには他のボタンもあります。しかし、接続ボタンが押され、シリアル接続が確立されている場合にのみ機能するはずです。それ以外の場合は、メッセージ'Device not connected'を送信してください。ここでは、コードの一部です:PyQT5でシリアル接続を確立する方法

import serial, time 
import sys 
import PyQt5 
from PyQt5.QtWidgets import * 
import mainwindow_auto 
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow): 
    def __init__(self): 
     #define gui actions here 
     super(self.__class__, self).__init__() 
     self.setupUi(self) 
     self.constat=0 

     self.plus_y_button.clicked.connect(self.moveplusy) 
     self.connect_button.clicked.connect(self.connect_printer) 

    def connect_printer(self): 

     port=str(self.port_sel_box.currentText()) 
     baudrate=str(self.baud_sel_box.currentText()) 

     try: 
      ser = serial.Serial(port, baudrate) 
      self.log_box.append('Connecting to printer....\nPort selected :'+self.port_sel_box.currentText()+'\nBaud Rate :'+self.baud_sel_box.currentText()) 

     except serial.serialutil.SerialException: 
      self.log_box.append('No device available....Please connect a device') 
    def moveplusy(self): 
     if(ser.isOpen()==true): 

      print('moving Y by +1') 
      self.log_box.append('moving Y by +1') 
     else: 
      print('No device available') 

が、これはコードの一部でしかありません、私は、コード内のすべてのウィジェットが含まれていますが、私は、GUIウィンドウが開き、コードを実行するだけで、関連するparts.Whenしていませんしかし、私はplus_y_button pythonクラッシュを押します.1つの方法は、コンストラクタ関数でシリアル接続を行うことですが、私はconnect_buttonを押すときにのみ、シリアル接続が発生します。これは私がplus_y_buttonを押したときに何が起こるかのスクリーンショットです:

それを解決する方法についてなど

screenshot

任意のアイデア?

+0

対処したい問題を正確に指定できますか? – user1993

+0

私は 'connect_button'を押したときにのみシリアル接続を確立したいと思っています。' plus_y_button'を押すと、 'connect_button'が押されてシリアル接続が確立されたかどうかに応じて、 –

答えて

0

関数 "connect_printer"では、変数 "ser"をローカルに定義し、関数が終了すると破棄されます。関数 "moveplusy"で "ser"を使用するには、それをクラスのメンバ(例えばself.ser)として定義する必要があります。さらに、関数で "True"を大文字にする必要があります。

import serial, time 
import sys 
import PyQt5 
from PyQt5.QtWidgets import * 
import mainwindow_auto 
class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow): 
    def __init__(self): 
     #define gui actions here 
     super(self.__class__, self).__init__() 
     self.setupUi(self) 
     self.constat=0 

     self.plus_y_button.clicked.connect(self.moveplusy) 
     self.connect_button.clicked.connect(self.connect_printer) 

    def connect_printer(self): 

     port=str(self.port_sel_box.currentText()) 
     baudrate=str(self.baud_sel_box.currentText()) 

     try: 
      self.ser = serial.Serial(port, baudrate) 
      self.log_box.append('Connecting to printer....\nPort selected :'+self.port_sel_box.currentText()+'\nBaud Rate :'+self.baud_sel_box.currentText()) 

     except serial.serialutil.SerialException: 
      self.log_box.append('No device available....Please connect a device') 
    def moveplusy(self): 
     if self.ser.isOpen(): 

      print('moving Y by +1') 
      self.log_box.append('moving Y by +1') 
     else: 
      print('No device available') 
関連する問題