class SerialOperate(Serial): def __init__(self, developer_email="*****@*****.**"): self.developer_email = developer_email self.console = None self.outputinfo = '' self.cmd = '' def open_serial(self, *args, **kwargs): try: self.console = Serial(*args, **kwargs) except Exception as e: raise IndoorException("open serial failed") if self.console.isOpen(): logging.info(u'[ open serial successfully\n') else: logging.info(u'[open serial failed\n') return self.console def exec_cmd(self, cmd): cmd = str(cmd) cmd += "\n" self.console.flushInput() try: self.console.write(cmd) traceback.print_exc() except Exception, e: logging.error(u"Write error") self.console.flushOutput() sleep(0.4)
def open_serial(self, *args, **kwargs): try: self.console = Serial(*args, **kwargs) except Exception as e: raise IndoorException("open serial failed") if self.console.isOpen(): logging.info(u'[ open serial successfully\n') else: logging.info(u'[open serial failed\n') return self.console
class PortManager: def __init__(self, port_num): super(PortManager, self).__init__() self.port_num = port_num self.ser = Serial(self.port_num, 115200, timeout=port_time) # windows系统使用com8口连接串行口 # self.port_num = port_num def close_port(self): if self.ser is not None: self.ser.close() # def interface_swicth(self, b): # try: # if self.ser is not None: # self.ser = serial.Serial(self.port_num, 115200, timeout=0.5) # winsows系统使用com8口连接串行口 # if b: # print('打开端口') # if not self.ser.is_open: # self.ser.open() # else: # print('关闭端口') # if self.ser.is_open: # self.ser.close() # return True # except Exception as e: # print('错误') # print(e) # return False def get_data_from_interface(self): if not self.ser.is_open: printlog('端口开启') self.ser.open() # enable port else: printlog('端口已经开启') while True: line_str = self.ser.readline() # print(line_str) yield line_str def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): self.close_port()
def dumpOutput(sio: Serial): while True: res = sio.readline().strip() if res == b"": break res = bytes_filter(res) res = res.decode() if res == "": break print(res)
def __init__(self, port_num): super(PortManager, self).__init__() self.port_num = port_num self.ser = Serial(self.port_num, 115200, timeout=port_time) # windows系统使用com8口连接串行口
def getdatafromserial(self): threshold = 514.0 oldvalue = 0 newvalue = 0 reading123 = 0 oldmillis = 0 newmillis = 0 timings = [0] * 16 self.HasEntered1stView = 1 #insert ur COM here ser = Serial('COM15') ser.flushInput() while self.Ugraph_1.Plot_Stop == 0: try: ser_bytes = ser.readline() decoded_bytes = float(ser_bytes[0:len(ser_bytes) - 2].decode("utf-8")) except: print("Keyboard Interrupt") break oldvalue = newvalue newvalue = 0 for i in range(16): # Average over 16 measurements newvalue += decoded_bytes newvalue = newvalue / 16 self.Ugraph_1.data_X.append( reading123 ) #self.Ugraph_1.data_X.append((time.strftime("%H:%M:%S", time.localtime()))) #np.append(self.Ugraph_1.data_X,((time.strftime("%H:%M:%S", time.localtime())))) reading123 += 1 self.Ugraph_1.data_Y.append( newvalue) #np.append(self.Ugraph_1.data_Y,newvalue) self.Graph1() # find triggering edge if oldvalue < threshold and newvalue >= threshold: oldmillis = newmillis newmillis = time.time() * 1000 # fill in the current time difference a ringbuffer timings.append((int)(newmillis - oldmillis)) if len(timings) > 16: timings.pop(0) #filling queue of time differences totalmillis = 0 # calculate average of the last 16 time differences for i in range(16): totalmillis += timings[i] # calculate heart rate heartrate = 60000 // (totalmillis / 16) if heartrate > 40 and heartrate < 130: print(heartrate) else: print("Measuring Heart rate") QtCore.QCoreApplication.processEvents()
def open_serial_connection(self, port='com1',baudrate = 115200, alias=None, timeout=None, newline=None, prompt=None, term_type=None, width=None, height=None, path_separator=None, encoding=None): """Opens a new serial connection to the given `host` and `port`. The new connection is made active. Possible existing connections are left open in the background. This keyword returns the index of the new connection which can be used later to switch back to it. Indices start from `1` and are reset when `Close All Connections` is used. Optional `alias` can be given for the connection and can be used for switching between connections, similarly as the index. See `Switch Connection` for more details. Connection parameters, like [#Default timeout|`timeout`] and [#Default newline|`newline`] are documented in `configuration`. If they are not defined as arguments, [#Configuration|the library defaults] are used for the connection. All the arguments, except `host`, `alias` and `port` can be later updated with `Set Client Configuration`. Starting from SerialLibrary 1.1, a shell is automatically opened by this keyword. Port `22` is assumed by default: | ${index}= | Open Connection | my.server.com | Non-standard port may be given as an argument: | ${index}= | Open Connection | 192.168.1.1 | port=23 | Aliases are handy, if you need to switch back to the connection later: | Open Connection | my.server.com | alias=myserver | | # Do something with my.server.com | | Open Connection | 192.168.1.1 | | Switch Connection | myserver | | # Back to my.server.com | Settings can be overridden per connection, otherwise the ones set on `library importing` or with `Set Default Configuration` are used: | Open Connection | 192.168.1.1 | timeout=1 hour | newline=CRLF | | # Do something with the connection | | Open Connection | my.server.com | # Default timeout | # Default line breaks | [#Default terminal settings|The terminal settings] are also configurable per connection: | Open Connection | 192.168.1.1 | term_type=ansi | width=40 | Arguments [#Default path separator|`path_separator`] and [#Default encoding|`encoding`] were added in SSHLibrary 2.0. """ timeout = timeout or self._config.timeout newline = newline or self._config.newline prompt = prompt or self._config.prompt term_type = term_type or self._config.term_type width = width or self._config.width height = height or self._config.height path_separator = path_separator or self._config.path_separator encoding = encoding or self._config.encoding # client = SSHClient(host, alias, port, timeout, newline, prompt, # term_type, width, height, path_separator, encoding) client = Serial(port,baudrate, timeout = 0.1) connection_index = self._connections.register(client, alias) client.config.update(index=connection_index) return connection_index
def serial_a(self): return Serial("3", 9600)