123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include "snap7_buf.h"
- #include <string>
- #include <string.h>
- 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<Snap7_buf::Variable_information> &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<Snap7_buf::Variable_information> &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<Snap7_buf::Variable_information> &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<Snap7_buf::Variable_information> &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;
- }
|