diff --git a/README.md b/README.md index 35149f6..fad3958 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,28 @@ Set the time needed for a character to be transmitted over UART. Used to calcula Enable/disable the sleep command after a UART write. For micropython implementations that uart.write calls uart_wait_tx_done, this sleep can be deactivated. If enabled, after UART write, the application sleeps for (char_wait_duration_us) * (number of command characters). +**_send** (cmd, timeout_ms=2500, termination_line=None): + +The function to send command to the sensor and wait for incoming data. Mainly used for test purposes. + +* arguments: + - `cmd`: the command to send (ex. '1I!') + - `timeout_ms` (optional): the time in milliseconds to wait for incoming response after a succesful write command to the sensor. + - `termination_line` (optional): If _termination_line_ is defined, the function will poll and aggregate incoming messages till the _termination_line_ matches with the input. If not defined, the function will terminate with the first successfully received message. +* returns: + A multiline string with all the received messages. If _termination_line_ is not defined, the string is always one line. If no incoming messages received, returns `None` + +### Example call + +```python +>>> out = sdi12._send("1M!", 2000, '1') +SDI12 > [1M!] + < [10015] + < [1] +>>> out +'10015\n1' +``` + # Example ```python diff --git a/microsdi12.py b/microsdi12.py index b6c8741..41a8e7f 100644 --- a/microsdi12.py +++ b/microsdi12.py @@ -21,7 +21,7 @@ def set_timing_params(self, char_wait_duration_us): def set_wait_after_uart_write(self, wait_enabled): self.enable_wait_after_uart_write = wait_enabled - def _send(self, cmd): + def _send(self, cmd, timeout_ms=2500, termination_line=None): if self.uart: self.uart.deinit() @@ -48,23 +48,29 @@ def _send(self, cmd): self.p_dir.value(0) # output set to read start_timestamp = utime.ticks_ms() - timeout_timestamp = start_timestamp + 2500 - line = None + timeout_timestamp = start_timestamp + timeout_ms + line = "" while True: remaining_bytes = self.uart.any() if(utime.ticks_ms() >= timeout_timestamp): break - line = self.uart.readline() - if line: - try: - line = line.decode('utf-8').strip() - print(" < [" + line + "]") - break - except: - print("! " + str(line)) + if remaining_bytes > 0: + line_cur = self.uart.readline() + if line_cur: + try: + line_cur = line_cur.decode('utf-8').strip() + line += '\n' + line_cur + print(" < [" + line_cur + "]") + if termination_line is not None: + if line_cur == termination_line: + break + else: + break + except: + print("! " + str(line)) utime.sleep_ms(100) - return line + return line.strip() if line != "" else None def is_active(self, address): ack_act_cmd_resp = self._send(address + '!') @@ -77,7 +83,7 @@ def get_sensor_info(self, address): if id_cmd_resp: responseParts = id_cmd_resp.split(' ') manufacturer = responseParts[0][3:] - model = responseParts[1] + model = ' '.join(responseParts[1:]).strip() return (manufacturer, model) def get_measurement(self, address, measurement_name="M"):