Демонстрирует подключение к конвертеру, уведомления о потере/восстановлении связи.
#include <exception>
#include <fstream>
#include <iomanip>
#include <iostream>
#define ILG_LOG_FILE
using namespace ilg;
#ifdef ILG_LOG
const char kLogLevelChars[] = {'-', 'E', 'W', 'I', 'D'};
const char kLogFileName[] = "ilguard.log";
#if 1
std::fstream file(kLogFileName, std::ios_base::out | std::ios_base::app);
auto& out = file;
#else
auto& out = std::cout;
#endif
auto t = std::time(nullptr);
auto tmb = std::localtime(&t);
out << std::put_time(tmb, "%d-%m-%Y %H:%M:%S") << " [" << kLogLevelChars[level] << ' '
<< pContext << "] " << pMessage << std::endl;
}
#endif
try {
switch (nStatus) {
case ILG_CONNECTION_DISCONNECTED:
std::cout << "{!} Конвертер отключён" << std::endl;
break;
std::cout << "{!} Конвертер подключён" << std::endl;
break;
std::cout << "{!} Идёт подключение к конвертеру" << std::endl;
break;
default:
break;
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
}
if (*pszPortName == '\0')
if (strncmp(pszPortName, "/dev/tty", 8) == 0)
auto p = strchr(pszPortName, '@');
if (p != nullptr) {
int nSn;
uint nTcpPort;
if (sscanf(pszPortName, "%d@%u", &nSn, &nTcpPort) == 2)
}
uint a[4];
if (sscanf(pszPortName, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) == 4)
}
#if 0
const uint16_t aListenPorts[] = {25000};
oSearch.SetListenPorts(aListenPorts, std::size(aListenPorts));
#endif
std::cout << "Поиск конвертеров..." << std::endl;
oSearch.Scan();
auto nCount = oSearch.GetConverterCount();
std::cout << "Найдено конвертеров: " << nCount << std::endl;
constexpr int kEnterPortNameCommand = 99;
int nCommand;
if (nCount > 0) {
std::cout << std::endl << "Выберите конвертер:" << std::endl;
for (size_t i = 0; i < nCount; i++) {
oSearch.GetConverterInfo(i, rInfo);
std::stringstream ss;
ss << kConverterModelNames[rInfo.
nModel];
ss <<
" с/н:" << rInfo.
nSn;
ss <<
" прошивка:" <<
VersionToStr(rInfo.
nFwVersion);
<< "]: " << ss.str() << std::endl;
}
std::cout << kEnterPortNameCommand << " - Ввести имя порта..." << std::endl;
std::cout << "0 - Выйти из программы" << std::endl;
std::cin >> nCommand;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail()) {
std::cin.clear();
nCommand = -1;
}
}
else
nCommand = kEnterPortNameCommand;
std::string sPortName;
switch (nCommand) {
case 0:
return false;
case kEnterPortNameCommand:
{
std::cout << "Введите имя порта конвертера:" << std::endl;
std::getline(std::cin, sPortName);
nPortType = GetPortTypeByName(sPortName.c_str());
}
break;
default:
if ((nCommand > 0) && (static_cast<size_t>(nCommand) <= nCount)) {
oSearch.GetConverterInfo(static_cast<size_t>(nCommand - 1), rInfo);
}
break;
}
std::cout << "Подключение к конвертеру [" << kPortTypeNames[nPortType] << ": " << sPortName
<< "]..." << std::endl;
oConverter = oILG.
GetConverter(nPortType, sPortName.c_str());
}
std::stringstream ss;
ss << kConverterModelNames[rInfo.
nModel];
ss <<
" с/н:" << rInfo.
nSn;
ss <<
" прошивка:" << VersionToStr(rInfo.
nFwVersion);
std::cout << "Конвертер успешно подключён [" << ss.str() << ']' << std::endl;
}
return true;
}
int main() {
try {
#ifdef ILG_LOG
#ifdef ILG_LOG_FILE
std::ofstream file(kLogFileName, std::ios_base::out | std::ios_base::trunc);
file.close();
#endif
CILG::SetLogCallback(LogCallback);
#endif
{
}
if (!DoConnectToConverter(oILG, oConverter))
return 0;
while (true) {
std::cout << "-----" << std::endl;
std::cout << "Введите номер команды:" << std::endl;
std::cout << "1 - Подключиться к..." << std::endl;
std::cout << "2 - Подключиться" << std::endl;
std::cout << "3 - Отключиться" << std::endl;
std::cout << "0 - Выйти из программы" << std::endl;
int nCommand;
std::cin >> nCommand;
if (std::cin.fail()) {
std::cin.clear();
nCommand = -1;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
switch (nCommand) {
case 0:
return 0;
case 1:
if (!DoConnectToConverter(oILG, oConverter))
return 0;
break;
case 2:
break;
case 3:
break;
}
}
}
catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
Класс поиска конвертеров.
Definition: ilg_cpp_helpers.h:265
Класс конвертера.
Definition: ilg_cpp_helpers.h:2778
ilg_connection_status GetConnectionStatus() const
Возвращает состояние подключения к конвертеру.
Definition: ilg_cpp_helpers.h:3283
void Disconnect()
Отключается от конвертера.
Definition: ilg_cpp_helpers.h:3273
void SetOptions(const ilg_converter_options &rOptions)
Устанавливает параметры конвертера.
Definition: ilg_cpp_helpers.h:3255
void GetConverterInfo(ilg_converter_info &rInfo) const
Возвращает информацию о конвертере.
Definition: ilg_cpp_helpers.h:3289
void Connect(bool fReconnect=false)
Подключается к конвертеру.
Definition: ilg_cpp_helpers.h:3263
void SetMessageCallback(ilg_converter_message_callback pCallback, void *pUserData=nullptr)
Устанавливает функцию обратного вызова для уведомлений конвертера.
Definition: ilg_cpp_helpers.h:3240
void GetOptions(ilg_converter_options &rOptions) const
Возвращает параметры конвертера.
Definition: ilg_cpp_helpers.h:3259
Класс для инициализации/финализации библиотеки SDK.
Definition: ilg_cpp_helpers.h:3425
void SetOptions(const ilg_options &rOptions)
Устанавливает глобальные настройки библиотеки.
Definition: ilg_cpp_helpers.h:3567
CConverterSearch GetSearch()
Создаёт дескриптор поиска конвертеров.
Definition: ilg_cpp_helpers.h:3575
CConverter GetConverter(ilg_port_type nPortType, const char *pszPortName)
Создаёт дескриптор конвертера.
Definition: ilg_cpp_helpers.h:3581
void GetOptions(ilg_options &rOptions) const
Возвращает глобальные настройки библиотеки.
Definition: ilg_cpp_helpers.h:3571
ilg_converter_msg
Сообщение конвертера.
Definition: ilguard.h:728
@ ILG_CONNECTION_CONNECTED
Подключён.
Definition: ilguard.h:671
@ ILG_CONNECTION_CONNECTING
Подключение.
Definition: ilguard.h:672
@ ILG_CONVERTER_MSG_CONNECTION_CHANGED
Definition: ilguard.h:739
Заголовочный файл SDK Guard с классами-помощниками C++.
Заголовочный файл API SDK Guard.
#define ILG_CALL
Макрос, определяющий соглашение о вызове функций.
Definition: ilguard.h:68
ilg_port_type
Definition: ilguard.h:241
@ ILG_PORT_TYPE_CLIENT
Адрес конвертера в режиме "Клиент" (например "40200@25000").
Definition: ilguard.h:266
@ ILG_PORT_TYPE_UNKNOWN
Definition: ilguard.h:243
@ ILG_PORT_TYPE_COMPORT
Definition: ilguard.h:245
@ ILG_PORT_TYPE_PROXY
Адрес конвертера в режиме "Прокси" (например "40200:36D782FB@127.0.0.1:25001").
Definition: ilguard.h:275
@ ILG_PORT_TYPE_SERVER
Адрес конвертера в режиме "Сервер" (например "10.0.0.2:1000").
Definition: ilguard.h:255
@ ILG_FALSE
Ложь. =0.
Definition: ilguard.h:124
ilg_log_level
Уровень лога.
Definition: ilguard.h:434
@ ILG_LOG_LEVEL_DEBUG
Отладочные сообщения.
Definition: ilguard.h:464
ilg_converter_model
Модель конвертера.
Definition: ilguard.h:285
@ ILG_CONVERTER_MODEL_UNKNOWN
Definition: ilguard.h:287
Информация о конвертере.
Definition: ilguard.h:527
ilg_port_type nPortType
Definition: ilguard.h:529
const char * pszPortName
Definition: ilguard.h:531
int64_t nFwBuildDate
Definition: ilguard.h:545
ilg_converter_model nModel
Definition: ilguard.h:536
const char * pszConnect
Definition: ilguard.h:534
uint32_t nFwVersion
Definition: ilguard.h:542
int nSn
Definition: ilguard.h:538
Настройки конвертера.
Definition: ilguard.h:801
ilg_converter_model nConnectModel
Definition: ilguard.h:803
Настройки библиотеки.
Definition: ilguard.h:493
ilg_bool fTwoStopBits
Definition: ilguard.h:497
int nRequestAttempts
Definition: ilguard.h:505
int nRequestTimeout
Definition: ilguard.h:501