Major fixes to MODBUS Driver
This commit is contained in:
75
src/main.cpp
75
src/main.cpp
@@ -1,18 +1,29 @@
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <DebugLogEnable.h>
|
||||
#include <Arduino.h>
|
||||
#include <RS485_Driver.h>
|
||||
#include <TCA9554PWR_Driver.h>
|
||||
|
||||
#ifdef ESP32
|
||||
auto bus = drivers::MODBUS(9600, SERIAL_8N1);
|
||||
#else
|
||||
auto bus = drivers::MODBUS(9600);
|
||||
#endif
|
||||
|
||||
void setup()
|
||||
{
|
||||
bool success = true;
|
||||
Serial.begin(9600);
|
||||
LOG_ATTACH_SERIAL(Serial);
|
||||
|
||||
LOG_INFO("Create i2c driver");
|
||||
auto i2c = drivers::I2C();
|
||||
LOG_INFO("Create relay driver");
|
||||
auto relays = drivers::TCA9554PWR(i2c, TCA9554_ADDRESS);
|
||||
|
||||
/*
|
||||
for (auto i(0); i < drivers::TCA9554PWR::OUT_PIN_MAX; i++)
|
||||
{
|
||||
LOG_INFO("Toggle relay [%d]=ON", i);
|
||||
@@ -29,15 +40,38 @@ void setup()
|
||||
LOG_INFO("Toggle port [0xAA]");
|
||||
success &= relays.setPort(0xAA);
|
||||
delay(2000);
|
||||
*/
|
||||
|
||||
LOG_INFO("Create modbus driver");
|
||||
#ifdef ESP32
|
||||
auto bus = drivers::MODBUS(9600, SERIAL_8N1);
|
||||
#else
|
||||
auto bus = drivers::MODBUS(9600);
|
||||
#endif
|
||||
|
||||
const uint8_t devAddress(0x01);
|
||||
const uint8_t baseRegister(0x02);
|
||||
const uint8_t baseRegister(0x00);
|
||||
|
||||
std::vector<uint16_t> results;
|
||||
LOG_INFO("Set Slave Address");
|
||||
/*
|
||||
while (false & !bus.writeRegister(devAddress, 0x00FE, 0x00AA)){
|
||||
std::vector<uint16_t> rv;
|
||||
if(bus.readHoldingRegisters(0x00AA, 0x00FE, 1, rv)) {
|
||||
LOG_INFO("New Slave address: ", rv[0]);
|
||||
break;
|
||||
}
|
||||
delay(500);
|
||||
};
|
||||
*/
|
||||
|
||||
/*
|
||||
while (true)
|
||||
{
|
||||
success &= bus.readHoldingRegisters(devAddress, baseRegister, 8, results);
|
||||
if (success) {
|
||||
for (auto i(0); i< results.size(); i++){
|
||||
LOG_INFO("[",i,"]Temperature: ", results.at(i)/10.0f);
|
||||
}
|
||||
results.clear();
|
||||
}
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
LOG_INFO("Write single coil");
|
||||
success &= bus.writeCoil(devAddress, baseRegister, true);
|
||||
@@ -64,8 +98,37 @@ void setup()
|
||||
{
|
||||
regValues[i] = i * 2;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
const uint8_t devAddress(0xAA);
|
||||
const uint8_t baseRegister(0x00);
|
||||
LOG_INFO("Looop");
|
||||
std::vector<uint16_t> results;
|
||||
bool success = bus.readHoldingRegisters(devAddress, baseRegister, 8, results);
|
||||
if (success)
|
||||
{
|
||||
for (auto i(0); i < results.size(); i++)
|
||||
{
|
||||
LOG_INFO("[", i, "]Temperature: ", results.at(i) / 10.0f);
|
||||
}
|
||||
results.clear();
|
||||
}
|
||||
|
||||
for (auto i(0); i < 8; i++)
|
||||
{
|
||||
LOG_DEBUG("\nCHANNEL ", i);
|
||||
std::vector<bool> values;
|
||||
bus.writeCoil(0x01, (uint8_t)i, true);
|
||||
delay(500);
|
||||
bus.readCoils(0x01,0x00,8, values);
|
||||
for (auto v: values ) {
|
||||
LOG_DEBUG("Coil", i, v ? "True" : "False");
|
||||
}
|
||||
bus.writeCoil(0x01, (uint8_t)i, false);
|
||||
delay(500);
|
||||
}
|
||||
delay(5000);
|
||||
}
|
||||
Reference in New Issue
Block a user