Skip to content

Commit

Permalink
enhance internal _send function and document it in the README
Browse files Browse the repository at this point in the history
  • Loading branch information
ftylitak committed Feb 11, 2022
1 parent 1cf51e4 commit 987de70
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 19 additions & 13 deletions microsdi12.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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 + '!')
Expand All @@ -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"):
Expand Down

0 comments on commit 987de70

Please sign in to comment.