SapFlow Probe
A low-cost HRM probe for measuring a tree's water consumption
weight.h File Reference
#include "pinout.h"
#include <string.h>
#include <Arduino.h>

Go to the source code of this file.

Functions

char * read_weight (char *buffer=weight_buf, int len=20)
 Gets the weight from the scale. More...
 

Variables

static char weight_buf [20]
 Stores the text recceived from the scale.
 

Function Documentation

◆ read_weight()

char* read_weight ( char *  buffer = weight_buf,
int  len = 20 
)

Gets the weight from the scale.

This function is intended for the OHAUS Defender 3000 scale. It communicates over RS232 using 5V and -5V voltage levels, which will fry your microcontroller if connected directly. You will need an RS232 line driver. This function prevents buffer overruns, but no data validation is performed.

Parameters
bufferThe string to store the data into (Default value is ok)
lenThe length of the buffer. (Default value is ok)
Returns
The same buffer that was passed in, containing text read from the scale.

Definition at line 6 of file weight.cpp.

6  {
7  // Clear the buffer in case someone was messing with the cable
8  int n = Serial1.available();
9  while( Serial1.available() > len ){
10  Serial1.readBytes(buffer, len);
11  }
12  if( Serial1.available() ){
13  Serial1.readBytes(buffer, Serial1.available());
14  }
15 
16  // Now request the weight
17  Serial1.print("P\r\n");
18  len = 1 + Serial1.readBytesUntil('\n', buffer, len-1);
19  buffer[len] = 0; // Null-terminate the string
20  int start_index, value_length, i;
21  // Filter out spaces and linefeed
22  for( i = 0; buffer[i]; ++i){
23  if( buffer[i] >= '0' )
24  break;
25  }
26  start_index = i;
27  for( ; buffer[i]; ++i){
28  if( buffer[i] < '.' )
29  break;
30  }
31  value_length = i - start_index;
32 
33  memmove(buffer, buffer+start_index, value_length);
34  buffer[value_length] = 0; // null terminate
35  return( buffer );
36 }