RS485 Wind Direction Transmitter

Hello forum,

I recently bought this wind vane:

RS485 Wind Direction Transmitte

I have tried to modify the test code that the "#include <ModbusMaster.h>" library has to make my device work but I have not been successful, reading the documentation I think my main problem is that I am not choosing the correct register for the reading and writing, I have already tried various combinations but have not been successful, maybe it is because I am new to using Modbus, maybe someone with more experience can help me with some suggestion, I would greatly appreciate all your respects, my current code is as follows:

#include <ModbusMaster.h>

#define MAX485_DE      3
#define MAX485_RE_NEG  2

ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
  
  Serial.begin(9600);

  // Modbus slave ID 2
  node.begin(2, Serial);   // wind sensor id = 

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}


void loop()
{
  uint8_t resultMain;

  resultMain = node.readHoldingRegisters(0x0003, 4);
  
  if (resultMain == node.ku8MBSuccess) 
  {
    Serial.print("Result: ");
    Serial.println(node.getResponseBuffer(0x10));
  }  

  delay(500);
}

My result on console is:

¿:Result: 0

This says that the hardware serial interface should be used for Modbus.

This writes debugging output to an interface that was previously reserved for Modbus.

Do you see the problem?

I understand that this part initializes my serial port for Modbus communication but this should not be enough to establish a link with my sensor?

In the same way, this process should be executed correctly if the Modbus communication is already initialized or I need something else?
I have been guided by the following code as a reference:

#include <ModbusMaster.h>

#define MAX485_DE      3
#define MAX485_RE_NEG  2


ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, 1);
  digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);
}
void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);
  pinMode(MAX485_DE, OUTPUT);

  
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  Serial.begin(9600);
  
  // Modbus slave ID 2
  node.begin(2, Serial);
  

  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}
//bool state = true;
void loop()
{
  uint8_t result;

  result=node.readHoldingRegisters(0x0017, 1);

  
  if (result == node.ku8MBSuccess)
  {
    Serial.print("Vbatt: ");
    Serial.println(node.getResponseBuffer(0x00));

  }
  delay(500);
}

Did you read the rest of my post? Sure, it establishes the communication but you ruin it afterwards by writing to the same interface. Your Modbus device won't recognize your debugging strings and mark the complete protocol as failed.

No this code is executed only if the transmission was correct. It cannot be correct because you interrupted the protocol by writing stuff as above "Result: " to the Modbus interface.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.