@JackBasson I updated the script, taking into account that commands and responses and with CR-LF:
# SDI-12 send/receive snippet
from machine import UART
from time import sleep_ms
def setup_uart(num=1, pins=('P3', 'P4'), timeout=120, inverse=False):
if inverse:
uart = UART(num, 1200, pins=pins, bits=7, timeout_chars=timeout,
parity=UART.EVEN, invert=UART.INV_RX|UART.INV_TX)
else:
uart = UART(num, 1200, pins=pins, bits=7, timeout_chars=timeout,
parity=UART.EVEN)
return uart
def sdi12_tx_rx(uart, tx_msg, send_break=True):
if tx_msg is not None:
if send_break:
uart.sendbreak(14) # Send the break char
sleep_ms(5) # Wait a little bit (may not be needed)
uart.write(tx_msg + b"\r\n") # Send the message
# and wait until it has been sent
# ESP has a long delay before sending data
uart.wait_tx_done(int(len(tx_msg) * 10000/1200) + 30)
# get the response back. It consists
# of the message being sent (if any) and
# the response.
if tx_msg is not None:
skip = uart.readline() # copy of the txmessage
rx_msg = uart.readline()
if rx_msg is not None:
rx_msg = rx_msg.strip()
# return the response only.
return rx_msg
CR-LF will be added to the message and removed from the responses.