1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+ import serial
4+ from PyQt5 import (QtGui ,QtCore ,QtWidgets )
5+ from src .gui .sharedcomnponets .sharedcomponets import SerialPortComboBox
6+ from src .gui .sharedcomnponets .sharedcomponets import GUIToolKit
7+ from src .simpleFOCConnector import SimpleFOCDevice
8+
9+ class ConfigureConnection (QtWidgets .QGroupBox ):
10+
11+ def __init__ (self , parent = None ):
12+ super ().__init__ (parent )
13+
14+ self .device = SimpleFOCDevice .getInstance ()
15+
16+ self .setTitle ('Configure serial connection' )
17+ self .setObjectName ('configureConnection' )
18+
19+ self .configCoonLayout = QtWidgets .QHBoxLayout (self )
20+ self .configCoonLayout .setObjectName (
21+ 'configureConnectionorizontalLayout' )
22+
23+ self .portNameLabel = QtWidgets .QLabel (self )
24+ self .portNameLabel .setObjectName ('portNameLabel' )
25+ self .configCoonLayout .addWidget (self .portNameLabel )
26+
27+ self .portNameComboBox = SerialPortComboBox (self )
28+ self .portNameComboBox .setObjectName ('portNameComboBox' )
29+ self .portNameComboBox .setMinimumWidth (250 )
30+ self .configCoonLayout .addWidget (self .portNameComboBox )
31+
32+ self .bitRateLabel = QtWidgets .QLabel (self )
33+ self .bitRateLabel .setObjectName ('bitRateLabel' )
34+ self .configCoonLayout .addWidget (self .bitRateLabel )
35+
36+ self .bitRatelineEdit = QtWidgets .QLineEdit (self )
37+ self .bitRatelineEdit .setObjectName ('bitRatelineEdit' )
38+ self .bitRatelineEdit .setValidator (QtGui .QRegExpValidator (QtCore .QRegExp ("^[0-9]*$" )))
39+ self .bitRatelineEdit .setText ('115200' )
40+ self .configCoonLayout .addWidget (self .bitRatelineEdit )
41+
42+ self .parityLabel = QtWidgets .QLabel (self )
43+ self .parityLabel .setObjectName ('parityLabel' )
44+ self .configCoonLayout .addWidget (self .parityLabel )
45+
46+ self .parityComboBox = QtWidgets .QComboBox (self )
47+ self .parityComboBox .setObjectName ('parityComboBox' )
48+ self .parityComboBox .addItems (serial .PARITY_NAMES .values ())
49+ self .configCoonLayout .addWidget (self .parityComboBox )
50+
51+ serial .PARITY_NAMES .values ()
52+
53+ self .byteSizeLabel = QtWidgets .QLabel (self )
54+ self .byteSizeLabel .setObjectName ('byteSizeLabel' )
55+ self .configCoonLayout .addWidget (self .byteSizeLabel )
56+
57+ self .byteSizeComboBox = QtWidgets .QComboBox (self )
58+ self .byteSizeComboBox .setObjectName ('byteSizeComboBox' )
59+ byteSizeList = [str (serial .EIGHTBITS ), str (serial .FIVEBITS ),
60+ str (serial .SIXBITS ),
61+ str (serial .SEVENBITS )]
62+ self .byteSizeComboBox .addItems (byteSizeList )
63+ self .configCoonLayout .addWidget (self .byteSizeComboBox )
64+
65+ self .stopBitsLabel = QtWidgets .QLabel (self )
66+ self .stopBitsLabel .setObjectName ('stopBitsLabel' )
67+ self .configCoonLayout .addWidget (self .stopBitsLabel )
68+
69+ self .stopBitsComboBox = QtWidgets .QComboBox (self )
70+ byteStopBitsList = [str (serial .STOPBITS_ONE ),
71+ str (serial .STOPBITS_ONE_POINT_FIVE ),
72+ str (serial .STOPBITS_TWO )]
73+ self .stopBitsComboBox .addItems (byteStopBitsList )
74+ self .stopBitsComboBox .setObjectName ('stopBitsComboBox' )
75+ self .configCoonLayout .addWidget (self .stopBitsComboBox )
76+
77+ self .connectDisconnectButton = QtWidgets .QPushButton (self )
78+ self .connectDisconnectButton .setIcon (
79+ GUIToolKit .getIconByName ('connect' ))
80+ self .connectDisconnectButton .setObjectName ('connectDeviceButton' )
81+ self .connectDisconnectButton .setText ('Connect' )
82+ self .connectDisconnectButton .clicked .connect (
83+ self .connectDisconnectDeviceAction )
84+
85+ self .configCoonLayout .addWidget (self .connectDisconnectButton )
86+
87+ self .portNameLabel .setText ('Port Name' )
88+ self .bitRateLabel .setText ('Bit rate' )
89+ self .parityLabel .setText ('Parity' )
90+ self .byteSizeLabel .setText ('Byte size' )
91+ self .stopBitsLabel .setText ('Stop bits' )
92+
93+ self .device .addConnectionStateListener (self )
94+ self .connectionStateChanged (self .device .isConnected )
95+
96+ def getConfigValues (self ):
97+ values = {
98+ 'connectionID' : '' ,
99+ 'serialPortName' : self .portNameComboBox .currentText (),
100+ 'serialRate' : self .bitRatelineEdit .text (),
101+ 'stopBits' : self .stopBitsExtractor (self .stopBitsComboBox .currentText ()),
102+ 'serialByteSize' : int (str (self .byteSizeComboBox .currentText ())),
103+ 'serialParity' : list (serial .PARITY_NAMES .keys ())[list (serial .PARITY_NAMES .values ()).index (self .parityComboBox .currentText ())][0 ]
104+ }
105+ return values
106+
107+ def stopBitsExtractor (self , value ):
108+ if value == '1.5' :
109+ return float (self .stopBitsComboBox .currentText ())
110+ else :
111+ return int (self .stopBitsComboBox .currentText ())
112+
113+ def connectionStateChanged (self , isConnectedFlag ):
114+ if isConnectedFlag :
115+ self .connectDisconnectButton .setText ('Disconnect' )
116+ self .connectDisconnectButton .setIcon (
117+ GUIToolKit .getIconByName ('disconnect' ))
118+ else :
119+ self .connectDisconnectButton .setText ('Connect' )
120+ self .connectDisconnectButton .setIcon (
121+ GUIToolKit .getIconByName ('connect' ))
122+
123+ self .portNameLabel .setEnabled (not isConnectedFlag )
124+ self .portNameComboBox .setEnabled (not isConnectedFlag )
125+ self .bitRateLabel .setEnabled (not isConnectedFlag )
126+ self .bitRatelineEdit .setEnabled (not isConnectedFlag )
127+ self .parityLabel .setEnabled (not isConnectedFlag )
128+ self .parityComboBox .setEnabled (not isConnectedFlag )
129+ self .byteSizeLabel .setEnabled (not isConnectedFlag )
130+ self .byteSizeComboBox .setEnabled (not isConnectedFlag )
131+ self .stopBitsLabel .setEnabled (not isConnectedFlag )
132+ self .stopBitsComboBox .setEnabled (not isConnectedFlag )
133+
134+ def connectDisconnectDeviceAction (self ):
135+ if self .device .isConnected :
136+ self .disConnectAction ()
137+ else :
138+ self .connectAction ()
139+
140+ def connectAction (self ):
141+ deviceConfig = self .getConfigValues ()
142+ self .device .configureConnection (deviceConfig )
143+ self .device .connect (SimpleFOCDevice .ONLY_CONNECT )
144+
145+ def disConnectAction (self ):
146+ self .device .disConnect ()
0 commit comments