#include "snap7_buf.h" #include #include Snap7_buf::Snap7_buf() { m_id = 0; m_start_index = 0; m_size = 0; mp_buf_obverse = nullptr; mp_buf_reverse = nullptr; m_communication_mode = NO_COMMUNICATION; } Snap7_buf::Snap7_buf(const Snap7_buf &other) { m_id = other.m_id; m_start_index = other.m_start_index; m_communication_mode = other.m_communication_mode; if (other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr) { mp_buf_obverse = (void *) malloc(other.m_size); memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size); mp_buf_reverse = (void *) malloc(other.m_size); memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size); m_size = other.m_size; m_variable_information_vector = other.m_variable_information_vector; } } Snap7_buf &Snap7_buf::operator=(const Snap7_buf &other) { m_id = other.m_id; m_start_index = other.m_start_index; m_communication_mode = other.m_communication_mode; if (other.m_size > 0 && other.mp_buf_obverse != nullptr && other.mp_buf_reverse != nullptr) { mp_buf_obverse = (void *) malloc(other.m_size); memcpy(mp_buf_obverse, other.mp_buf_obverse, other.m_size); mp_buf_reverse = (void *) malloc(other.m_size); memcpy(mp_buf_reverse, other.mp_buf_reverse, other.m_size); m_size = other.m_size; m_variable_information_vector = other.m_variable_information_vector; } return *this; } Snap7_buf::~Snap7_buf() { if (mp_buf_obverse) { free(mp_buf_obverse); mp_buf_obverse = NULL; } if (mp_buf_reverse) { free(mp_buf_reverse); mp_buf_reverse = NULL; } } Snap7_buf::Snap7_buf(int id, int start_index, int size, std::vector &variable_information_vector, Communication_mode communication_mode) { m_id = id; m_start_index = start_index; m_communication_mode = communication_mode; if (size > 0) { mp_buf_obverse = (void *) malloc(size); memset(mp_buf_obverse, 0, size); mp_buf_reverse = (void *) malloc(size); memset(mp_buf_reverse, 0, size); m_size = size; m_variable_information_vector = variable_information_vector; } } Snap7_buf::Snap7_buf(int id, int start_index, int size, void *p_buf_obverse, void *p_buf_reverse, std::vector &variable_information_vector, Communication_mode communication_mode) { m_id = id; m_start_index = start_index; m_communication_mode = communication_mode; if (size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr) { mp_buf_obverse = (void *) malloc(size); memcpy(mp_buf_obverse, p_buf_obverse, size); mp_buf_reverse = (void *) malloc(size); memcpy(mp_buf_reverse, p_buf_reverse, size); m_size = size; m_variable_information_vector = variable_information_vector; } } void Snap7_buf::init(int id, int start_index, int size, std::vector &variable_information_vector, Communication_mode communication_mode) { m_id = id; m_start_index = start_index; m_communication_mode = communication_mode; if (mp_buf_obverse) { free(mp_buf_obverse); mp_buf_obverse = NULL; } if (mp_buf_reverse) { free(mp_buf_reverse); mp_buf_reverse = NULL; } if (size > 0) { mp_buf_obverse = (void *) malloc(size); memset(mp_buf_obverse, 0, size); mp_buf_reverse = (void *) malloc(size); memset(mp_buf_reverse, 0, size); m_size = size; m_variable_information_vector = variable_information_vector; } } void Snap7_buf::init(int id, int start_index, int size, void *p_buf_obverse, void *p_buf_reverse, std::vector &variable_information_vector, Communication_mode communication_mode) { m_id = id; m_start_index = start_index; m_communication_mode = communication_mode; if (mp_buf_obverse) { free(mp_buf_obverse); mp_buf_obverse = NULL; } if (mp_buf_reverse) { free(mp_buf_reverse); mp_buf_reverse = NULL; } if (size > 0 && p_buf_obverse != nullptr && p_buf_reverse != nullptr) { mp_buf_obverse = (void *) malloc(size); memcpy(mp_buf_obverse, p_buf_obverse, size); mp_buf_reverse = (void *) malloc(size); memcpy(mp_buf_reverse, p_buf_reverse, size); m_size = size; m_variable_information_vector = variable_information_vector; } } //正序数据 转为 倒序数据 void Snap7_buf::obverse_to_reverse() { char *p_in = (char *) mp_buf_obverse; char *p_out = (char *) mp_buf_reverse; for (auto &iter: m_variable_information_vector) { for (int i = 0; i < iter.m_variable_count; ++i) { for (int j = 0; j < iter.m_variable_size; ++j) { p_out[iter.m_variable_index + iter.m_variable_size * i + j] = p_in[iter.m_variable_index + iter.m_variable_size * (i + 1) - j - 1]; } } // for (int i = 0; i < iter.m_variable_count; ++i) // { // for (int j = iter.m_variable_index*i; j < iter.m_variable_index*i+iter.m_variable_size ; ++j) // { // p_out[j] = p_in[iter.m_variable_index*i+iter.m_variable_size - j -1]; // } // } } } //倒序数据 转为 正序数据 void Snap7_buf::reverse_to_obverse() { char *p_in = (char *) mp_buf_reverse; char *p_out = (char *) mp_buf_obverse; for (auto &iter: m_variable_information_vector) { for (int i = 0; i < iter.m_variable_count; ++i) { for (int j = 0; j < iter.m_variable_size; ++j) { p_out[iter.m_variable_index + iter.m_variable_size * i + j] = p_in[iter.m_variable_index + iter.m_variable_size * (i + 1) - j - 1]; } } // for (int i = 0; i < iter.m_variable_count; ++i) // { // for (int j = iter.m_variable_index*i; j < iter.m_variable_index*i+iter.m_variable_size ; ++j) // { // p_out[j] = p_in[iter.m_variable_index*i+iter.m_variable_size - j -1]; // } // } } } int Snap7_buf::get_id() { return m_id; } int Snap7_buf::get_start_index() { return m_start_index; } int Snap7_buf::get_size() { return m_size; } void *Snap7_buf::get_buf_obverse() { return mp_buf_obverse; } void *Snap7_buf::get_buf_reverse() { return mp_buf_reverse; } Snap7_buf::Communication_mode Snap7_buf::get_communication_mode() { return m_communication_mode; } void Snap7_buf::set_communication_mode(Communication_mode communication_mode) { m_communication_mode = communication_mode; }