RS485 sensor read/write reset?

Hello everyone. I am using a RS485 communications sensor, a max3485A, and ESP32.
I was able to get my sensor to read data and report it back to my esp correctly and I am able to write a value to it if it is a read/write register. HOWEVER, I wrote to a read/write reg and now I am not able to get the sensor to read from that register anymore. I dont know how to switch it back or get it to just read again. Please help.

Code that I'm using to successfully read registers at certain addresses.

.....
 uint8_t result = node.readHoldingRegisters( 0, 7 );

  delay (1000);
  
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Nitrogen (N): ");
    Serial.print(node.getResponseBuffer(4));
    Serial.println("mg/kg");
    float nitro = (node.getResponseBuffer(4));
  }
.....

Code that I used to write to the single reg address.

void writeNitrogen(uint16_t value)
{
  uint8_t result = node.writeSingleRegister(4, value); // Nitrogen register is at address 0x0004
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Successfully wrote ");
    Serial.print(value);
    Serial.println(" to the nitrogen register.");
  }else{
    Serial.println("Failed to write to nitrogen register.");
  }
}

I found this website link below helpful when dealing with the registers in the "Registers Functions" section. Is there one of these that I can possibly use?

Using node.writeSingleReg() should be writing to the holding registers since holding is used for the read/writes that will be modified by the master usually right? So I tried instead just node.readInputReg() for the raw data from the sensor but it was still displaying back the value I wrote to the holding register. That shouldn't be happening right??

Did I break my sensor or is its firmware just bad somehow? :frowning:

Were you randomly writing to holding registers? Not good idea. Reading is ok.
Can you find modbus protocol of your sensor?

@kmin Yeah I was just testing out if I could since it was a read/write register so I thought it would be fine but now I got myself into this problem.

I've been using #include <ModbusMaster.h> library to help me with the modbus handling.

This is from my sensor datasheet. The first image says code0x30 but I'm pretty sure thats just a typo because they then use the standard 0x03 read in their example.



Let me understand, only nitrogen register 0x04 doesn't work, everything else is ok? Whats the response of reading 0x04?

At first the sensor was giving nitrogen raw data values and all other data values as expected.

I then tried to only write to single register and I picked the nitrogen register to test being able to write to them as well. I wrote just a value of 100 to its holding register. This did update the output of node.readHoldingReg() to be =100.

I then got rid of my write function and just tried reading the input reg and let it run for a while which should have updated the values. The other values were updating just fine but not the nitrogen which stayed the value I wrote in.

node.readHoldingReg() is often for read/writes
node.readInputReg() is for just read and is the raw data from the sensor (I thought)

readInputReg() was also still giving me the wrote value

Do you know what writing to that register should do?
I have difficulties to understand your sensors functions. Normally writeable register is for user input, for example calibration, generally it can't be output of a measurement.

I just thought it would change the value of the data. The example from the datasheet just writes 32 into the nitrogen register. The datasheet is not helpful at all and doesn't fully explain everything. I guess I just shouldn't have touched it? since it was working at first for the raw data...

Correct.

I can't imagine why in the hell someone would allow write access to a sensor data register!

That's not the OP fault. The manual states reg 04 is a "read-write" type!

"...value of the data" What data?
I'm trying to say, generally writeable register can't be sensor output. If you write 100 to it and read it after, it's 100. I write generally, because out of modbus standard whatever can be implemented.

Right again!

Modbus protocol is a standard.
Unfortunately, not all implementers strictly adhere to the standard.
This can be the case for this sensor manufacturer.

oh... so as a beginner and not fully knowing this beforehand that its not common to have a sensor output data be writeable, trying to follow their datasheet messed me up and I most likely cant fix it since it doesn't say anything about it in their awful typo filled datasheet. I guess I should just get a new sensor now or don't use nitrogen measure anymore.

Or little more info about your sensor and it's registers. Factory reset for example.

Yeah, I have power supply, when I read one specific holding register, it's restarting.

Only way it could make little bit sense is that those registers are temporary. Did you try powering off?

me too, my friend accidentally write value to address and right now the value not change... can we reset the sensor to factory setting ?

I was able to finally figure out how to factory reset my RS485 sensor. it came with a config tool .exe. In this tool of mine, it has a reset button once you have your sensor connected to your pc. I had to get a usb dongle? as someone else called it and wire it up to match the connection of my sensor. I did not realize that the config.exe it came would also be its reset.

How do you get config tool.exe ? can i see it ? i dont have any software from my sensor :face_with_diagonal_mouth:
And if you want to connect to your pc yeah i think you have to get usb dongle. I use this..

ab829f2d28243eb487826eca7804fba2

I have a 5pin NPK Soil Sensor from CWT. They sent me a single piece of paper that lead me to a downloaded zip file that contained the sensor manual and the config.exe that I can use to make sure that I get values back. Mine has this reset button on the bottom.

If yours didn't send you any manuals or .exe maybe you could go back to the website you found it on and see if you can find one. Have you tried contacting the company support? They might be able to give you something to factory reset it.

Beyond cinese modbus issues, is there anybody here ever got any values that correspond to "real life" NPK levels from this sensors? I'm asking, because to my common sense these sensors are not able to measure NPK anyhow..