Skip to content

Commit

Permalink
log as option
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanzhe Lyu committed Aug 15, 2021
1 parent 729f6fe commit 3cfbe71
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"C_Cpp.formatting": "Default"
"C_Cpp.formatting": "Default",
"editor.formatOnSave": true
}
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ install(FILES
# BUILD EXAMPLE
set(EXAMPLE_FILES example/example.cpp)
add_executable(modbuspp_example ${EXAMPLE_FILES})
target_compile_definitions(modbuspp_example
PUBLIC
ENABLE_MODBUSPP_LOGGING
)
target_include_directories(modbuspp_example
PUBLIC
.
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ UPDATES in v0.3 compared to V0.2:
1. Bug Fixing
2. Type fixing
3. Initial Windows support (in dev, not fully tested)
4. Cmake install script
4. Logging as Option
5. Cmake install script


# 1 Usage
Expand All @@ -41,6 +42,8 @@ Download the MODBUS++, you can:
and include the code whenever appropriate
> include "modbuspp/modbus.h"
## 1.3 Logging as Option
To enable logging, simply set `ENABLE_MODBUSPP_LOGGING` in your compile option, see CMakeLists.txt with example.

# 2 Getting Started with a Example
## 2.1 Getting the Example
Expand Down
25 changes: 16 additions & 9 deletions modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
#define MODBUSPP_MODBUS_H

#include <cstring>
#include <iostream>
#include <stdint.h>
#include <string>

#if _WIN32
#ifdef ENABLE_MODBUSPP_LOGGING
#include <cstdio>
#define LOG(fmt, ...) printf("[ modbuspp ]" fmt, ##__VA_ARGS__)
#else
#define LOG(...) (void)0
#endif

#ifdef _WIN32
// WINDOWS socket
#define _WINSOCK_DEPRECATED_NO_WARNINGS

Expand Down Expand Up @@ -165,12 +172,12 @@ bool modbus::modbus_connect()
{
if (HOST.empty() || PORT == 0)
{
std::cout << "Missing Host and Port" << std::endl;
LOG("Missing Host and Port");
return false;
}
else
{
std::cout << "Found Proper Host " << HOST << " and Port " << PORT << std::endl;
LOG("Found Proper Host %s and Port %d", HOST.c_str(), PORT);
}

#ifdef _WIN32
Expand All @@ -183,15 +190,15 @@ bool modbus::modbus_connect()
_socket = socket(AF_INET, SOCK_STREAM, 0);
if (!X_ISVALIDSOCKET(_socket))
{
std::cout << "Error Opening Socket" << std::endl;
LOG("Error Opening Socket");
#ifdef _WIN32
WSACleanup();
#endif
return false;
}
else
{
std::cout << "Socket Opened Successfully" << std::endl;
LOG("Socket Opened Successfully");
}

#ifdef WIN32
Expand All @@ -212,14 +219,14 @@ bool modbus::modbus_connect()

if (!X_ISCONNECTSUCCEED(connect(_socket, (SOCKADDR *)&_server, sizeof(_server))))
{
std::cout << "Connection Error" << std::endl;
LOG("Connection Error");
#ifdef _WIN32
WSACleanup();
#endif
return false;
}

std::cout << "Connected" << std::endl;
LOG("Connected");
_connected = true;
return true;
}
Expand All @@ -233,7 +240,7 @@ void modbus::modbus_close() const
#ifdef _WIN32
WSACleanup();
#endif
std::cout << "Socket Closed" << std::endl;
LOG("Socket Closed");
}

/**
Expand Down

0 comments on commit 3cfbe71

Please sign in to comment.