• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Problem z komunikacja via RS485 ModbusRTU
#2
Nie będę Ci wymyślał koła na nowo, w załączniku działający przykład, ale dla innego licznika OR-WE-504. Zasada działania jest taka sama.
Parę wskazówek.
Do testów użyj sprzętowego UART, jak konwerter jest na logice 5V to użyj na początek typowego Arduino UNO/NANO, a najlepiej Leonardo/Micro i Serial1 gadasz z licznikiem, Serial drukujesz na PC.  Oprócz UART mam podłączony tylko pin DE z konwertera, a UART to TX-DI, RX-RO. No i musisz mieć tę bibliotekę https://github.com/4-20ma/ModbusMaster.
W Node MCU serial nie jest potrzebny, do programowania użyj OTA, do podglądu danych użyj przykładu z TELNET i Putty jako klienta.
Ja używam RS485 w NANO na tym samym serialu co UART USB, tak że ten niezalecany sposób również działa. Przez ten licznik zacząłem zabawę z Arduino, więc to było już dawno temu.

Kod:
/*

 RS485_HalfDuplex.pde - example using ModbusMaster library to communicate
 with EPSolar LS2024B controller using a half-duplex RS485 transceiver.

 This example is tested against an EPSolar LS2024B solar charge controller.
 See here for protocol specs:
 http://www.solar-elektro.cz/data/dokumenty/1733_modbus_protocol.pdf

 Library:: ModbusMaster
 Author:: Marius Kintel <marius at kintel dot net>

 Copyright:: 2009-2016 Doc Walker

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

*/

#include <ModbusMaster.h>

/*!
 We're using a MAX485-compatible RS485 Transceiver.
 Rx/Tx is hooked up to the hardware serial port at 'Serial'.
 The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE      4
//#define MAX485_RE_NEG  6 //podłączony do MAX485_DE

// instantiate ModbusMaster object
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);
 // Init in receive mode
 //digitalWrite(MAX485_RE_NEG, 0);
 digitalWrite(MAX485_DE, 0);

 // Modbus communication runs at 115200 baud
 Serial.begin(9600);

 // Modbus slave ID 2
 node.begin(2, Serial);
 // Callbacks allow us to configure the RS485 transceiver correctly
 node.preTransmission(preTransmission);
 node.postTransmission(postTransmission);
}

bool state = true;

void loop()
{
 uint8_t result;
 uint16_t data[16];
 uint32_t kwh;
 // Toggle the coil at address 0x0002 (Manual Load Control)
 //result = node.writeSingleCoil(0x0002, state);
 //state = !state;
Serial.begin(9600);
 // Read 16 registers starting at 0x3100)
//  result = node.readHoldingRegisters(0x40000, 10);
 //OR
 result = node.readHoldingRegisters(0, 10);
 if (result == node.ku8MBSuccess)
 {
  Serial.begin(115200);  
 Serial.write(27);       // ESC command
 Serial.print("[2J");    // clear screen command
 Serial.write(27);
 Serial.print("[H");     // cursor to home command
     Serial.print("Voltage: \t\t");
   Serial.println(node.getResponseBuffer(0x00)/10);
   Serial.print("Current: \t\t");
   Serial.println(node.getResponseBuffer(0x01)/10);
   Serial.print("Freq: \t\t\t");
   Serial.println(node.getResponseBuffer(0x02)/10);
   Serial.print("Ac. Power: \t\t");
   Serial.println(node.getResponseBuffer(0x03));
   Serial.print("Reac. Power: \t\t");
   Serial.println(node.getResponseBuffer(0x04));
   Serial.print("App. Power: \t\t");
   Serial.println(node.getResponseBuffer(0x05));
   Serial.print("Power fact: \t\t");
   Serial.println(node.getResponseBuffer(0x06)/1000);
/*        
   Serial.print("x06: ");
       Serial.println(node.getResponseBuffer(0x06));
   Serial.print("x07: ");
       Serial.println(node.getResponseBuffer(0x07));
*/

kwh=((uint32_t) node.getResponseBuffer(0x07))<<16;
//kwh=kwh<<16; Albo tak
kwh=kwh|(node.getResponseBuffer(0x08));
    Serial.print("x07|08 Energy Act: \t");
       Serial.println( kwh/1000);

kwh=((uint32_t) node.getResponseBuffer(0x09))<<16;

kwh=kwh|(node.getResponseBuffer(0x0A));
    Serial.print("x09|0A Energy Reac: \t");
       Serial.println( kwh/1000);
       
/*    Serial.print("x08: ");
       Serial.println(node.getResponseBuffer(0x08));
   Serial.print("x09: ");
       Serial.println(node.getResponseBuffer(0x09));
   Serial.print("x0A: ");
       Serial.println(node.getResponseBuffer(0x0A));
  */
/*  
   kwh=(node.getResponseBuffer(0x06));
   kwh=kwh<<16;
   
  uint32_t temp=(node.getResponseBuffer(0x07));
  kwh=(kwh|temp);
   Serial.println(kwh);*/
   
 }

 delay(5000);
}

Wynik działania w Putty:
   
Miło być decenianym https://buycoffee.to/kaczakat
 
Odpowiedź
  


Wiadomości w tym wątku
RE: Problem z komunikacja via RS485 ModbusRTU - przez kaczakat - 02-06-2019, 06:04

Skocz do:


Przeglądający: 1 gości