@@ -35,10 +35,10 @@ def _default_ports(self):
35
35
36
36
def map_flow_control (self ):
37
37
if self .rtscts :
38
- return QSerialPort .HardwareControl
38
+ return QSerialPort .FlowControl . HardwareControl
39
39
elif self .xonxoff :
40
- return QSerialPort .SoftwareControl
41
- return QSerialPort .NoFlowControl
40
+ return QSerialPort .FlowControl . SoftwareControl
41
+ return QSerialPort .FlowControl . NoFlowControl
42
42
43
43
STOP_BIT_MAPPING = {
44
44
serial .STOPBITS_ONE : QSerialPort .StopBits .OneStop ,
@@ -60,7 +60,18 @@ def map_stop_bits(self):
60
60
def map_parity (self ):
61
61
return QtSerialConfig .PARITY_BIT_MAPPING [self .parity ]
62
62
63
+ BYTESIZE_MAPPING = {
64
+ 5 : QSerialPort .DataBits .Data5 ,
65
+ 6 : QSerialPort .DataBits .Data6 ,
66
+ 7 : QSerialPort .DataBits .Data7 ,
67
+ 8 : QSerialPort .DataBits .Data8 ,
68
+ }
69
+
70
+ def map_bytesize (self ):
71
+ return QtSerialConfig .BYTESIZE_MAPPING [self .bytesize ]
72
+
63
73
class QtSerialTransport (DeviceTransport ):
74
+ __slots__ = ('__weakref__' ,) # needed to for connection with Qt signals, at least with PyQt6
64
75
65
76
#: Default config
66
77
config = Instance (QtSerialConfig , ()).tag (config = True )
@@ -75,23 +86,32 @@ class QtSerialTransport(DeviceTransport):
75
86
def open_serial_port (self , config ):
76
87
try :
77
88
serial_port = QSerialPort ()
78
- serial_port .setPortName (config .device_path )
89
+ serial_port .setPortName (config .port )
79
90
#Setting the AllDirections flag is supported on all platforms. Windows supports only this mode.
80
- serial_port .setBaudRate (config .baudrate , QSerialPort .AllDirections )
91
+ serial_port .setBaudRate (config .baudrate , QSerialPort .Direction . AllDirections )
81
92
serial_port .setParity (config .map_parity ())
82
93
serial_port .setStopBits (config .map_stop_bits ())
83
- serial_port .setDataBits (config .bytesize )
94
+ serial_port .setDataBits (config .map_bytesize () )
84
95
serial_port .setFlowControl (config .map_flow_control ())
85
96
serial_port .open (QSerialPort .ReadWrite )
97
+ foo = self .on_data_receive_ready
98
+ serial_port .readyRead .connect (self .on_data_receive_ready )
86
99
return serial_port
87
100
except Exception as e :
88
101
log .error ("{}" .format (traceback .format_exc ()))
89
- return None
102
+ return None
103
+
104
+ def on_data_receive_ready (self ):
105
+ if not self .connection :
106
+ return
107
+ while self .connection .bytesAvailable () > 0 :
108
+ data = self .connection .read (self .connection .bytesAvailable ())
109
+ self .protocol .data_received (data )
110
+ self .last_read = data
90
111
91
112
def connect (self ):
92
113
config = self .config
93
- #self.device_path = config.port
94
- device_path = self .device_path = config .port
114
+ self .device_path = config .port
95
115
try :
96
116
#: Save a reference
97
117
self .protocol .transport = self
0 commit comments