eric6/MicroPython/MicroPythonSerialPort.py

branch
micropython
changeset 7067
3fc4082fc6ba
child 7070
3368ce0e7879
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/eric6/MicroPython/MicroPythonSerialPort.py	Thu Jul 18 20:30:03 2019 +0200
@@ -0,0 +1,99 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a QSerialPort with additional functionality.
+"""
+
+from __future__ import unicode_literals
+
+from PyQt5.QtCore import QIODevice
+from PyQt5.QtSerialPort import QSerialPort
+
+
+class MicroPythonSerialPort(QSerialPort):
+    """
+    Class implementing a QSerialPort with additional functionality
+    """
+    def __init__(self, parent=None):
+        """
+        Constructor
+        
+        @param parent reference to the parent object
+        @type QObject
+        """
+        super(MicroPythonSerialPort, self).__init__(parent)
+        
+        self.__connected = False
+    
+    def openSerialLink(self, port):
+        """
+        Public method to open a serial link to a given serial port.
+        
+        @param port port name to connect to
+        @type str
+        @return flag indicating success
+        @rtype bool
+        """
+        self.setPortName(port)
+        if self.open(QIODevice.ReadWrite):
+            self.setDataTerminalReady(True)
+            # 115.200 baud, 8N1
+            self.setBaudRate(115200)
+            self.setDataBits(QSerialPort.Data8)
+            self.setParity(QSerialPort.NoParity)
+            self.setStopBits(QSerialPort.OneStop)
+            
+            self.__connected = True
+            return True
+        else:
+            return False
+    
+    def closeSerialLink(self):
+        """
+        Public method to close the open serial connection.
+        """
+        if self.__connceted:
+            self.close()
+            
+            self.__connected = False
+    
+    def isConnected(self):
+        """
+        Public method to get the connection state.
+        
+        @return flag indicating the connection state
+        @rtype bool
+        """
+        return self.__connected
+    
+    def readUntil(self, expected=b"\n", size=None):
+        """
+        Public method to read data until an expected sequence is found
+        (default: \n) or a specific size is exceeded.
+        
+        @param expected expected bytes sequence
+        @type bytes
+        @param size maximum data to be read
+        @type int
+        @return bytes read from the device including the expected sequence
+        @rtype bytes
+        """
+        data = bytearray()
+        while True:
+            if self.waitForReadyRead():
+                c = bytes(self.read(1))
+                if c:
+                    data += c
+                    if data.endswith(expected):
+                        break
+                    if size is not None and len(data) >= size:
+                        break
+                else:
+                    break
+            else:
+                break
+        
+        return bytes(data)

eric ide

mercurial