15
15
from atom .atom import set_default
16
16
from atom .api import List , Instance , Enum , Bool , Int , Str
17
17
from inkcut .core .api import Plugin , Model , log
18
+ from inkcut .device .transports .serialport .plugin import SerialConfigBase , SerialPortInfo
18
19
from inkcut .device .plugin import DeviceTransport
19
- from PyQt5 .QtSerialPort import QSerialPort
20
- from PyQt5 . QtSerialPort import QSerialPortInfo
20
+ from qtpy .QtSerialPort import QSerialPort , QSerialPortInfo
21
+ import serial
21
22
22
-
23
-
24
- class IdNameItem :
25
- def __init__ (self , id , name ):
26
- self .id :int = id
27
- self .name = name
28
-
29
-
30
- class QtSerialConfig (Model ):
31
- device_path = Str ()
32
-
33
- #: Available serial ports
34
- ports = List ()
35
- #: Serial port config
36
- port = Str ().tag (config = True )
37
-
38
- flowcontrols = []
39
- # Available FlowControls
40
- flowcontrols .append ( IdNameItem (QSerialPort .NoFlowControl , 'No flow control' ) )
41
- flowcontrols .append ( IdNameItem (QSerialPort .HardwareControl ,'Hardware flow control (RTS/CTS)' ) )
42
- flowcontrols .append ( IdNameItem (QSerialPort .SoftwareControl ,'Software flow control (XON/XOFF)' ) )
43
- flowcontrols .append ( IdNameItem (QSerialPort .UnknownFlowControl ,'Unknown flow control (obsolete value)' ) )
44
- #: FlowControl config
45
- flowcontrol = Int (0 ).tag (config = True )
46
-
47
- #: Available BaudRates
48
- baudrates = Enum (110 , 300 , 600 , 1200 , 2400 , 4800 , 9600 , 14400 , 19200 , 38400 , 57600 , 115200 , 128000 , 256000 )
49
- #: BaudRate config
50
- baudrate = Int (9600 ).tag (config = True )
51
-
52
- parities = []
53
- # Available Parities
54
- parities .append ( IdNameItem (QSerialPort .NoParity , 'No Parity' ) )
55
- parities .append ( IdNameItem (QSerialPort .EvenParity , 'Even' ) )
56
- parities .append ( IdNameItem (QSerialPort .OddParity , 'Odd' ) )
57
- parities .append ( IdNameItem (QSerialPort .SpaceParity , 'Space' ) )
58
- parities .append ( IdNameItem (QSerialPort .MarkParity , 'Mark' ) )
59
- parities .append ( IdNameItem (QSerialPort .UnknownParity , 'Unknown' ) )
60
- #: Parity config
61
- parity = Int (0 ).tag (config = True )
62
-
63
- list_stopbits = []
64
- # Available Stopbits
65
- list_stopbits .append ( IdNameItem (QSerialPort .OneStop , '1 stop bit' ) )
66
- list_stopbits .append ( IdNameItem (QSerialPort .OneAndHalfStop , '1.5 stop bits (Windows only)' ) )
67
- list_stopbits .append ( IdNameItem (QSerialPort .TwoStop , '2 stop bits' ) )
68
- #: Stopbits config
69
- stopbits = Int (1 ).tag (config = True )
70
-
71
- bytesize = Enum (8 , 7 , 6 , 5 ).tag (config = True )
72
-
73
-
74
- # -------------------------------------------------------------------------
75
- # Defaults
76
- # -------------------------------------------------------------------------
23
+ class QtSerialConfig (SerialConfigBase ):
77
24
def _default_ports (self ):
78
- return self .get_serial_ports ()
79
-
80
- def _default_port (self ):
81
- if self .ports :
82
- return self .ports [0 ].portName ()
83
- return ""
84
-
85
- def refresh (self ):
86
- self .ports = self ._default_ports ()
87
-
88
- def get_serial_ports (self ):
89
- info_list = QSerialPortInfo ()
90
- serial_list = info_list .availablePorts ()
91
- return serial_list
92
-
25
+ result = []
26
+ for port in QSerialPortInfo ().availablePorts ():
27
+ info = SerialPortInfo ()
28
+ info .device_path = port .portName ()
29
+ info .description = "{} {}" .format (port .portName (), port .description ())
30
+ if port .hasProductIdentifier () and port .hasVendorIdentifier ():
31
+ info .usb_pid = port .productIdentifier ()
32
+ info .usb_vid = port .vendorIdentifier ()
33
+ result .append (info )
34
+ return result
35
+
36
+ def map_flow_control (self ):
37
+ if self .rtscts :
38
+ return QSerialPort .HardwareControl
39
+ elif self .xonxoff :
40
+ return QSerialPort .SoftwareControl
41
+ return QSerialPort .NoFlowControl
42
+
43
+ STOP_BIT_MAPPING = {
44
+ serial .STOPBITS_ONE : QSerialPort .StopBits .OneStop ,
45
+ serial .STOPBITS_ONE_POINT_FIVE : QSerialPort .StopBits .OneAndHalfStop ,
46
+ serial .STOPBITS_TWO : QSerialPort .StopBits .TwoStop ,
47
+ }
48
+
49
+ def map_stop_bits (self ):
50
+ return QtSerialConfig .STOP_BIT_MAPPING [self .stopbits ]
51
+
52
+ PARITY_BIT_MAPPING = {
53
+ serial .PARITY_NONE : QSerialPort .Parity .NoParity ,
54
+ serial .PARITY_EVEN : QSerialPort .Parity .EvenParity ,
55
+ serial .PARITY_ODD : QSerialPort .Parity .OddParity ,
56
+ serial .PARITY_MARK : QSerialPort .Parity .SpaceParity ,
57
+ serial .PARITY_SPACE : QSerialPort .Parity .MarkParity ,
58
+ }
59
+
60
+ def map_parity (self ):
61
+ return QtSerialConfig .PARITY_BIT_MAPPING [self .parity ]
93
62
94
63
class QtSerialTransport (DeviceTransport ):
95
64
@@ -102,17 +71,17 @@ class QtSerialTransport(DeviceTransport):
102
71
103
72
#: Whether a serial connection spools depends on the device (configuration)
104
73
always_spools = set_default (False )
105
-
74
+
106
75
def open_serial_port (self , config ):
107
76
try :
108
77
serial_port = QSerialPort ()
109
- serial_port .setPortName (config .port )
78
+ serial_port .setPortName (config .device_path )
110
79
#Setting the AllDirections flag is supported on all platforms. Windows supports only this mode.
111
80
serial_port .setBaudRate (config .baudrate , QSerialPort .AllDirections )
112
- serial_port .setParity (config .parity )
113
- serial_port .setStopBits (config .stopbits )
81
+ serial_port .setParity (config .map_parity () )
82
+ serial_port .setStopBits (config .map_stop_bits () )
114
83
serial_port .setDataBits (config .bytesize )
115
- serial_port .setFlowControl (config .flowcontrol )
84
+ serial_port .setFlowControl (config .map_flow_control () )
116
85
serial_port .open (QSerialPort .ReadWrite )
117
86
return serial_port
118
87
except Exception as e :
0 commit comments