Skip to content

Commit 987de70

Browse files
committed
enhance internal _send function and document it in the README
1 parent 1cf51e4 commit 987de70

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ Set the time needed for a character to be transmitted over UART. Used to calcula
4949

5050
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).
5151

52+
**_send** (cmd, timeout_ms=2500, termination_line=None):
53+
54+
The function to send command to the sensor and wait for incoming data. Mainly used for test purposes.
55+
56+
* arguments:
57+
- `cmd`: the command to send (ex. '1I!')
58+
- `timeout_ms` (optional): the time in milliseconds to wait for incoming response after a succesful write command to the sensor.
59+
- `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.
60+
* returns:
61+
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`
62+
63+
### Example call
64+
65+
```python
66+
>>> out = sdi12._send("1M!", 2000, '1')
67+
SDI12 > [1M!]
68+
< [10015]
69+
< [1]
70+
>>> out
71+
'10015\n1'
72+
```
73+
5274
# Example
5375

5476
```python

microsdi12.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def set_timing_params(self, char_wait_duration_us):
2121
def set_wait_after_uart_write(self, wait_enabled):
2222
self.enable_wait_after_uart_write = wait_enabled
2323

24-
def _send(self, cmd):
24+
def _send(self, cmd, timeout_ms=2500, termination_line=None):
2525
if self.uart:
2626
self.uart.deinit()
2727

@@ -48,23 +48,29 @@ def _send(self, cmd):
4848
self.p_dir.value(0) # output set to read
4949

5050
start_timestamp = utime.ticks_ms()
51-
timeout_timestamp = start_timestamp + 2500
52-
line = None
51+
timeout_timestamp = start_timestamp + timeout_ms
52+
line = ""
5353
while True:
5454
remaining_bytes = self.uart.any()
5555
if(utime.ticks_ms() >= timeout_timestamp):
5656
break
5757

58-
line = self.uart.readline()
59-
if line:
60-
try:
61-
line = line.decode('utf-8').strip()
62-
print(" < [" + line + "]")
63-
break
64-
except:
65-
print("! " + str(line))
58+
if remaining_bytes > 0:
59+
line_cur = self.uart.readline()
60+
if line_cur:
61+
try:
62+
line_cur = line_cur.decode('utf-8').strip()
63+
line += '\n' + line_cur
64+
print(" < [" + line_cur + "]")
65+
if termination_line is not None:
66+
if line_cur == termination_line:
67+
break
68+
else:
69+
break
70+
except:
71+
print("! " + str(line))
6672
utime.sleep_ms(100)
67-
return line
73+
return line.strip() if line != "" else None
6874

6975
def is_active(self, address):
7076
ack_act_cmd_resp = self._send(address + '!')
@@ -77,7 +83,7 @@ def get_sensor_info(self, address):
7783
if id_cmd_resp:
7884
responseParts = id_cmd_resp.split(' ')
7985
manufacturer = responseParts[0][3:]
80-
model = responseParts[1]
86+
model = ' '.join(responseParts[1:]).strip()
8187
return (manufacturer, model)
8288

8389
def get_measurement(self, address, measurement_name="M"):

0 commit comments

Comments
 (0)