How to read RS485 output from my wind direction sensor with Controllino Maxi

64 Views Asked by At

I need outputs in degree to read in Arduino IDE

Products: Wind direction sensor (SEN0482) and Controllino Maxi.

Wiring

I have attempted to get any form of output from my wind direction sensor using a different code, but the only thing I could get was "Fail 226". This suggests that there might be an issue either with the connections or with the code that reads the sensor.

Here is my code:

#include <Controllino.h>
#include "ModbusMaster.h"

// Definer pins for RE og DE kontrol af RS485
const int rs485_re = 68; // Receiver Enable
const int rs485_de = 69; // Driver Enable

// Opret en instans af ModbusMaster klasse
ModbusMaster modbus;

void preTransmission() {
  digitalWrite(rs485_re, LOW);
  digitalWrite(rs485_de, HIGH);
}

void postTransmission() {
  digitalWrite(rs485_re, HIGH);
  digitalWrite(rs485_de, LOW);
}

void setup() {
  // Initialiser pins for RS485 kontrol
  pinMode(rs485_re, OUTPUT);
  pinMode(rs485_de, OUTPUT);

  // Sæt RE og DE til inaktiv tilstand (RE=HIGH, DE=LOW)
  digitalWrite(rs485_re, HIGH);
  digitalWrite(rs485_de, LOW);

  // Start RS485 kommunikation på Serial1
  Serial1.begin(9600); // Erstat med den korrekte baudrate for din enhed
  modbus.begin(2, Serial1); // Angiv slave ID og brug Serial1 porten

  // Definer preTransmission og postTransmission funktioner
  modbus.preTransmission(preTransmission);
  modbus.postTransmission(postTransmission);

  // Start serielle kommunikation til debugging
  Serial.begin(9600);
}

void loop() {
  uint8_t result;
  uint16_t windDirectionData[1]; // Array til at opbevare læste data

  // Læs vindretningen fra Modbus enheden
  result = modbus.readHoldingRegisters(0x0000, 1);

  if (result == modbus.ku8MBSuccess) {
    // Gem data fra responsbufferen
    windDirectionData[0] = modbus.getResponseBuffer(0);

    // Konverter data fra registret til en læsbar format
    int windDirection = windDirectionData[0];
    Serial.print("Vindretning (rå værdi): ");
    Serial.print(windDirection);
    Serial.print(". Dette svarer til ");

    // Fortolkning af vindretningen
    switch(windDirection) {
      case 0: Serial.print("Nord"); break;
      case 1: Serial.print("Nordøst-ved-Nord"); break;
      case 2: Serial.print("Nordøst"); break;
      case 3: Serial.print("Nordøst-ved-Øst"); break;
      case 4: Serial.print("Øst"); break;
      case 5: Serial.print("Sydøst-ved-Øst"); break;
      case 6: Serial.print("Sydøst"); break;
      case 7: Serial.print("Sydøst-ved-Syd"); break;
      case 8: Serial.print("Syd"); break;
      case 9: Serial.print("Sydvest-ved-Syd"); break;
      case 10: Serial.print("Sydvest"); break;
      case 11: Serial.print("Sydvest-ved-Vest"); break;
      case 12: Serial.print("Vest"); break;
      case 13: Serial.print("Nordvest-ved-Vest"); break;
      case 14: Serial.print("Nordvest"); break;
      case 15: Serial.print("Nordvest-ved-Nord"); break;
      default: Serial.print("Ukendt retning"); break;
    }
    Serial.println(".");
  } else {
    Serial.print("Fejl ved læsning af Modbus data: ");
    Serial.println(result);
  }

  delay(1000); // Vent 1 sekund før næste læsning
}
1

There are 1 best solutions below

7
Guillius On

Most of your code and configuration seems correct, as far as I can tell. Still, you encountered the 226 (0xE2) error, the meaning of this error is: slave response timeout.

Generally speaking, there are two possibilities for the source of the issue:

  1. wiring is wrong: change the A and B line and try again. Modbus RTU over RS-485 lines is famous for A-B wiring errors (and it is a historical thing).
  2. The second possibility is somewhat bad news: many users have reported the same issue with this library: https://github.com/4-20ma/ModbusMaster/issues/93#issuecomment-357524040.
    It seems like the controller switches too early from sending mode to receiving mode, causing the last byte to never reach the wires. Using a digital oscilloscope or logic analyser or something like that would help you to see the actual data on the wires.
    The easiest solution for this problem is: try using a different library?