Files
ANSLIB/IOBox/Backup/v1.0.0/iobox_api.h

125 lines
5.6 KiB
C
Raw Normal View History

2026-03-28 11:39:04 +11:00
#ifndef _IO_BOX_API_H
#define _IO_BOX_API_H
#define ANSIO_API __declspec(dllexport)
#pragma once
#include "extcode.h"
#include <vector>
#include <string>
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <chrono>
#pragma comment(lib,"WS2_32")
// #pragma comment(lib, "Ws2_32.lib")
// #define TEST_FIX_IP
#define DEVICE_TCP_PORT 502
#define DEVICE_DATA_ADDRESS 100
#define DEVICE_DATA_LENGTH 17
namespace ANSCENTER {
typedef struct {
std::string ip_address;
uint8_t modbus_address;
} iobox_info_t;
typedef union {
/* data */
uint16_t raw_data[DEVICE_DATA_LENGTH];
struct
{
uint16_t modbus_address;
uint8_t serial_number[10];
uint16_t version;
int16_t channel_DO[4];
int16_t channel_DI[4];
int16_t channel_AI[2];
};
} iobox_data_t;
typedef enum {
IOBOX_CHANNEL_DO1 = 0,
IOBOX_CHANNEL_DO2,
IOBOX_CHANNEL_DO3,
IOBOX_CHANNEL_DO4,
IOBOX_CHANNEL_DI1,
IOBOX_CHANNEL_DI2,
IOBOX_CHANNEL_DI3,
IOBOX_CHANNEL_DI4,
IOBOX_CHANNEL_AI1,
IOBOX_CHANNEL_AI2,
IOBOX_CHANNEL_NUM
} iobox_channel_t;
typedef struct {
int counting_get;
bool is_connected;
SOCKET sock_tcp;
iobox_info_t iobox_info;
iobox_data_t iobox_data;
} iobox_profile_t;
class ANSIO_API iobox_api
{
private:
std::string ip_mcast; //ip multicast
int port_mcast; //port multicast
std::string channel_name[IOBOX_CHANNEL_NUM] = {
"DO1", "DO2", "DO3", "DO4",
"DI1", "DI2", "DI3", "DI4",
"AI1", "AI2"
};
uint16_t iobox_channel_address[IOBOX_CHANNEL_NUM] = {
107, 108, 109, 110, 111, 112, 113, 114, 115, 116
};
std::vector<iobox_profile_t> iobox_profiles;
iobox_profile_t* findProfileByIp(const std::string& ip);
void save_info_iobox(iobox_info_t iobox_info);
void setSocketTimeout(SOCKET sock, int timeout);
void sendMulticastMessage(const std::string& message);
bool sendTcpMessage(const std::string& ip, const char* buffer, size_t length);
bool receiveTcpMessage(const std::string& ip, char* buffer, size_t& length);
bool getAllDataFromIobox(const std::string& ip);
bool setValueDataToIobox(const std::string& ip, uint16_t address, uint16_t value);
bool isValidChannelName(const std::string& channelName);
void show_profile_iobox(iobox_profile_t profile);
public:
iobox_api(std::string ip_mcast, int port_mcast);
~iobox_api();
void show_profile_iobox(std::string ip);
void show_profile_ioboxs();
std::vector<std::string> scanNetworkDevicesMulticast(int timeout);
bool setAuthenticationIobox(const std::string& ip, const std::string& username, const std::string& password);
bool resetAuthenticationIobox(const std::string& ip, const std::string& token) { return true; }
bool resetIobox(const std::string& ip, const std::string& token) { return true; }
bool connectToIobox(const std::string& ip, int port, const std::string& username, const std::string& password);
bool disconnectToIobox(const std::string& ip);
std::vector<std::string> getDeviceChannelNames(const std::string& ip,int timeout);
std::string getValueDataStringIoboxFromChannelName(const std::string& ip, const std::string& channelName);
bool setValueDataStringIoboxFromChannelName(const std::string& ip, const std::string& channelName, const std::string& value);
bool getAllValueIobox(const std::string& ip);
};
}
extern "C" ANSIO_API int CreateANSIOHandle(ANSCENTER::iobox_api** Handle, const char* multicastIPAddress, int multicastPort);
extern "C" ANSIO_API int ScanANSIOHandle(ANSCENTER::iobox_api** Handle, int timeout, LStrHandle ipAddresses);
extern "C" ANSIO_API int ConnectANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int ioBoxPort, const char* userName, const char* passWord);
extern "C" ANSIO_API int DisconnectANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP);
extern "C" ANSIO_API int GetChannelNamesANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP,int timeout, LStrHandle channelNames);
extern "C" ANSIO_API int SetValueANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, const char* value);
extern "C" ANSIO_API int GetValueANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, LStrHandle lStrValue);
extern "C" ANSIO_API int SetAuthenticationANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* userName, const char* passWord);
extern "C" ANSIO_API int ReleaseANSIOHandle(ANSCENTER::iobox_api** Handle);
extern "C" ANSIO_API int ResetAuthenticationANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* token);
extern "C" ANSIO_API int ResetANSIOHandle(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* token);
// C++ interface
extern "C" ANSIO_API int ScanANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, int timeout, std::string& ipAddresses);
extern "C" ANSIO_API int GetChannelNamesANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, int timeout, std::string& channelNames);
extern "C" ANSIO_API int GetValueANSIOHandle_CPP(ANSCENTER::iobox_api** Handle, const char* ioBoxIP, const char* channelName, std::string& lStrValue);
#endif