eric6/MicroPython/MicroPythonSerialPort.py

branch
micropython
changeset 7067
3fc4082fc6ba
child 7070
3368ce0e7879
equal deleted inserted replaced
7066:e3d034e65afc 7067:3fc4082fc6ba
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a QSerialPort with additional functionality.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QIODevice
13 from PyQt5.QtSerialPort import QSerialPort
14
15
16 class MicroPythonSerialPort(QSerialPort):
17 """
18 Class implementing a QSerialPort with additional functionality
19 """
20 def __init__(self, parent=None):
21 """
22 Constructor
23
24 @param parent reference to the parent object
25 @type QObject
26 """
27 super(MicroPythonSerialPort, self).__init__(parent)
28
29 self.__connected = False
30
31 def openSerialLink(self, port):
32 """
33 Public method to open a serial link to a given serial port.
34
35 @param port port name to connect to
36 @type str
37 @return flag indicating success
38 @rtype bool
39 """
40 self.setPortName(port)
41 if self.open(QIODevice.ReadWrite):
42 self.setDataTerminalReady(True)
43 # 115.200 baud, 8N1
44 self.setBaudRate(115200)
45 self.setDataBits(QSerialPort.Data8)
46 self.setParity(QSerialPort.NoParity)
47 self.setStopBits(QSerialPort.OneStop)
48
49 self.__connected = True
50 return True
51 else:
52 return False
53
54 def closeSerialLink(self):
55 """
56 Public method to close the open serial connection.
57 """
58 if self.__connceted:
59 self.close()
60
61 self.__connected = False
62
63 def isConnected(self):
64 """
65 Public method to get the connection state.
66
67 @return flag indicating the connection state
68 @rtype bool
69 """
70 return self.__connected
71
72 def readUntil(self, expected=b"\n", size=None):
73 """
74 Public method to read data until an expected sequence is found
75 (default: \n) or a specific size is exceeded.
76
77 @param expected expected bytes sequence
78 @type bytes
79 @param size maximum data to be read
80 @type int
81 @return bytes read from the device including the expected sequence
82 @rtype bytes
83 """
84 data = bytearray()
85 while True:
86 if self.waitForReadyRead():
87 c = bytes(self.read(1))
88 if c:
89 data += c
90 if data.endswith(expected):
91 break
92 if size is not None and len(data) >= size:
93 break
94 else:
95 break
96 else:
97 break
98
99 return bytes(data)

eric ide

mercurial