Using PySerial to communicate with SDI-12 probe

1.9k views Asked by At

Background: I'm trying to write a script to send commands to a soil moisture probe (Stevens 'Hydra II Soil and Salinity probe') so I can incorporate its output with a GPS into QGIS to record soil moisture at specific geocoded points.

Setup: I am using Windows 8 and am connected to the probe through port COM6 over a USB and via a USB to SDI-12 adapter (Stevens 'SDI-12 Explorer'). I'm using Python 2.7.6.

Tests: I have used the SDI-12 Explorer propriety interface and can see and take measurements from the probe. I have installed a port monitor to see the port traffic from both the PySerial script I've written and the SDI-12 Explorer interface and there are differences in the way the COM port is opened. (The below is formatted like python, but its just the COM port traffic).

SDI-12 Explorer
COM is open
<20150605101018.855 SYS>
Baud rate 9600
<20150605101018.855 SYS>
RTS on
<20150605101018.855 SYS>
DTR off
<20150605101018.855 SYS>
Data bits=8, Stop bits=1, Parity=None
<20150605101018.855 SYS>
Set chars: Eof=0x1A, Error=0x00, Break=0x00, Event=0x1A, Xon=0x11, Xoff=0x13
<20150605101018.855 SYS>
Handflow: ControlHandShake=(), FlowReplace=(TRANSMIT_TOGGLE, RTS_CONTROL), XonLimit=1024, XoffLimit=1024


PySerial
COM is open
<20150605150222.045 SYS>
In/out queue size 4096/4096
<20150605150222.045 SYS>
Set timeouts: ReadInterval=0, ReadTotalTimeoutMultiplier=0, ReadTotalTimeoutConstant=0, WriteTotalTimeoutMultiplier=0, WriteTotalTimeoutConstant=0
<20150605150222.045 SYS>
Baud rate 1200
<20150605150222.045 SYS>
RTS on
<20150605150222.045 SYS>
DTR on
<20150605150222.045 SYS>
Data bits=8, Stop bits=1, Parity=None
<20150605150222.045 SYS>
Set chars: Eof=0x00, Error=0x00, Break=0x00, Event=0x00, Xon=0x11, Xoff=0x13
<20150605150222.045 SYS>
Handflow: ControlHandShake=(DTR_CONTROL), FlowReplace=(TRANSMIT_TOGGLE, RTS_CONTROL), XonLimit=2048, XoffLimit=512
<20150605150222.045 SYS>
Purge the serial port: RXABORT, RXCLEAR, TXABORT, TXCLEAR

This won't be exactly reproducible unless you have one of these probes handy, but the script should be able to talk to any instrument that uses SDI-12 protocol.

Questions: 1. Anyone know how I can get this script to work? 2. How do I turn 'DTR_CONTROL' off or change the Xon Xoff limits? These seem to be the key differences in the com port opening header. 3. Does anyone know the correct timing sequences necessary for communicating with an SDI-12 device?

Here are the documents for the commands for the: probe (http://www.stevenswater.com/catalog/Assets/products/soil_sensors/manual/Hydra%20Probe%20Manual%2092915%20Jan%202015.pdf)

protocol (http://www.sdi-12.org/specification.php?fileID=1)

Pyserial script # Import libraries import serial import time

# Serial port setup
ser = serial.Serial('COM6',
                    baudrate=1200,
                    bytesize=serial.EIGHTBITS,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    timeout=0,
                    writeTimeout=0,
                    rtscts=False,
                    xonxoff=1024)

# Check if port is open and print name
if ser.isOpen():
    print 'Open: ' + ser.portstr

# Send break to wake probe
ser.sendBreak(duration=0.012)
time.sleep(0.25)

# Command and readline 
ser.write('0I!')
time.sleep(0.25)
print 'Output: ' + ser.readline()

Thanks!

0

There are 0 answers