Hello everyone! I am trying to communicate with a TUF-2000M ultrasonic flow meter using MODBUS RTU (RS485). I noticed that there is the a MODBUS-Master library available in Web IDE but I am having issues understanding the syntax.
Hi, here is the example code how to get flow rate:
// This #include statement was automatically added by the Particle IDE.
#include <ModbusMaster.h>
#define FLOW_REGISTER 1
#define FLOW_DATA_SIZE 2
// instantiate ModbusMaster object as slave ID 1
ModbusMaster node(1);
void setup() {
// initialize Modbus communication baud rate
node.begin(9600);
node.enableTXpin(D7); //D7 is the pin used to control the TX enable pin of RS485 driver
//node.enableDebug(); //Print TX and RX frames out on Serial. Beware, enabling this messes up the timings for RS485 Transactions, causing them to fail.
Serial.begin(9600);
while(!Serial.available()) Particle.process();
Serial.println("Starting Modbus Transaction:");
}
void readFlow() {
uint8_t j, result;
uint16_t buf[FLOW_DATA_SIZE];
uint16_t temp;
float flow;
Serial.println("Reading registers");
result = node.readHoldingRegisters(FLOW_REGISTER, FLOW_DATA_SIZE);
if (result == node.ku8MBSuccess)
{
Serial.println("Success! Processing...");
for (j = 0; j < FLOW_DATA_SIZE; j++)
{
buf[j] = node.getResponseBuffer(j);
Serial.print(buf[j]);
Serial.print(" ");
}
Serial.println("<- done");
// swap bytes because the data comes in Big Endian!
temp = buf[1];
buf[1]=buf[0];
buf[0]=temp;
// hand-assemble a single-precision float from the bytestream
memcpy(&flow, &buf, sizeof(float));
Serial.print("Flow is ");
Serial.println(flow, 6);
}
else {
Serial.print("Failure. Code: ");
Serial.println(result);
}
}
void loop() {
readFlow();
delay(3000);
}
here is an original website from where I get the example which is useful:
Base on this example you can obtain the rest of your needs
and here is complete code that you can use for tests.
Is compiling fine under Boron V2.0.1 but not tested:
I wonder if I need to change which pins the ModbusMaster library is using for TX/RX. I have the converter connected to the Boron’s TX/RX pins, as shown below:
Firstly don’t enable this line as is messing with timing as described
2nd you have to swap your wires I mean TX ( Boron red wire) has to be connected to RX on your RS485 converter and RX ( Boron green wire ) has to be connected to TX on the converter make sure that GND on Boron and converter are also connected and then try again also setup your flow meter to Modbus RTU
Also make sure that flow meter communication is setup for 9600 baud
The error code that you are receiving is request timeout
And don’t worry about D7 is used with some converters for TX control
Another question for you: do you think it is possible to turn the flow meter on and off using the Boron? I am trying to figure out a way to conserve power.
Would anyone be willing to share their experience with the TUF-2000?
I’ve been looking for a cheap Ultrasonic Flow Meter, but this one seems very inexpensive.
Are the results stable in the long term ?
We are currently using an Xtreme Universal Soundbar Power Adapter that can be regulated from 12v-24v. The manual suggests a voltages between 8-24v for optimal performance. We currently researching into buying a battery for sake of portability.