Starting work on implementing structures
This commit is contained in:
parent
974ae853b9
commit
670ca56b0b
3 changed files with 72 additions and 10 deletions
|
|
@ -1,16 +1,31 @@
|
|||
#include "hoymiles.h"
|
||||
#include "modbus.h"
|
||||
|
||||
Dtu::Dtu(const char *ip_address, int port){
|
||||
this->modbus_t = modbus_new_tcp(ip_address, port);
|
||||
modbus_connect(this->modbus_t);
|
||||
#include <iostream>
|
||||
|
||||
Dtu::Dtu(const char *ip_address, int port) {
|
||||
this->modbus_t = modbus_new_tcp(ip_address, port);
|
||||
if(modbus_connect(this->modbus_t) == -1){
|
||||
std::cerr << "conn_error";
|
||||
modbus_free(this->modbus_t);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void Dtu::readTest(){
|
||||
uint16_t tempArray[32];
|
||||
modbus_read_registers(this->modbus_t, 0x13ef, 1, tempArray);
|
||||
void Dtu::readTest(uint16_t address, int registers) {
|
||||
uint16_t readArray[registers];
|
||||
int registerCount;
|
||||
registerCount = modbus_read_registers(this->modbus_t, address, registers, readArray);
|
||||
if(registerCount == -1){
|
||||
std::cerr << "read_error";
|
||||
return;
|
||||
}
|
||||
for (int i{0}; i < registerCount; i++) {
|
||||
std::clog << readArray[i] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Dtu::~Dtu(){
|
||||
delete this->modbus_t;
|
||||
Dtu::~Dtu() {
|
||||
modbus_close(this->modbus_t);
|
||||
modbus_free(this->modbus_t);
|
||||
}
|
||||
11
src/main.cpp
11
src/main.cpp
|
|
@ -1,6 +1,9 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "hoymiles.h"
|
||||
#include "modbus.h"
|
||||
|
||||
|
|
@ -10,7 +13,13 @@ int main(){
|
|||
int port {502};
|
||||
|
||||
Dtu dtu {ip_address.c_str(), port};
|
||||
dtu.readTest();
|
||||
|
||||
bool buttonPressed{false};
|
||||
for(int i{0}; i<10; i++){
|
||||
dtu.readTest(0x1034, 2);
|
||||
std::clog << std::endl;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue