|
@@ -1,29 +1,51 @@
|
|
|
#include "plc_communicator.h"
|
|
|
|
|
|
+#include "string.h"
|
|
|
+#include <iostream>
|
|
|
+#include <stdint.h>
|
|
|
+
|
|
|
+Error_manager Plc_Communicator::ReadProtoParam(std::string path)
|
|
|
+{
|
|
|
+ int fd = open(path.c_str(), O_RDONLY);
|
|
|
+ if (fd == -1) Error_manager(Error_code::PARAMETER_ERROR);
|
|
|
+ FileInputStream* input = new FileInputStream(fd);
|
|
|
+ bool success = google::protobuf::TextFormat::Parse(input, &m_connection_params);
|
|
|
+ // std::cout<<m_global_param.data_path()<<std::endl;
|
|
|
+ delete input;
|
|
|
+ close(fd);
|
|
|
+ if(success){
|
|
|
+ return Error_manager(Error_code::SUCCESS);
|
|
|
+ }else{
|
|
|
+ return Error_manager(Error_code::PARAMETER_ERROR);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// ××××××××××× 构造与析构 ×××××××××××
|
|
|
-Plc_Communicator::Plc_Communicator(plc_module::plc_connection_params connection_params) : mb_plc_initialized(false),
|
|
|
+Plc_Communicator::Plc_Communicator(std::string plc_config_path) : mb_plc_initialized(false),
|
|
|
mb_plc_is_connected(false),
|
|
|
mb_plc_is_updating(false),
|
|
|
mp_plc_owner(0),
|
|
|
m_plc_thread(0)
|
|
|
{
|
|
|
- m_plc_ip_str = connection_params.ip();
|
|
|
- m_plc_port = connection_params.port();
|
|
|
- m_plc_slave_id = connection_params.slave_id();
|
|
|
- m_plc_current_error = Error_manager(Error_code::SUCCESS, Error_level::NORMAL, "初始状态正常");
|
|
|
- m_plc_status_update_timeout = 10000;
|
|
|
- for (size_t i = 0; i < PLC_REGION_NUM; i++)
|
|
|
- {
|
|
|
- m_plc_region_status[i].last_time_point = std::chrono::steady_clock::now();
|
|
|
- m_plc_region_status[i].current_status = 255;
|
|
|
- m_plc_region_status[i].cmd = 0;
|
|
|
- }
|
|
|
+ if(ReadProtoParam(plc_config_path).is_equal_error_manager(Error_manager(Error_code::SUCCESS))){
|
|
|
+ m_plc_ip_str = m_connection_params.ip();
|
|
|
+ m_plc_port = m_connection_params.port();
|
|
|
+ m_plc_slave_id = m_connection_params.slave_id();
|
|
|
+ m_plc_current_error = Error_manager(Error_code::SUCCESS, Error_level::NORMAL, "初始状态正常");
|
|
|
+ m_plc_status_update_timeout = 10000;
|
|
|
+ for (size_t i = 0; i < PLC_REGION_NUM; i++)
|
|
|
+ {
|
|
|
+ m_plc_region_status[i].last_time_point = std::chrono::steady_clock::now();
|
|
|
+ m_plc_region_status[i].current_status = 255;
|
|
|
+ m_plc_region_status[i].cmd = 0;
|
|
|
+ }
|
|
|
|
|
|
- m_plc_data.resize(PLC_REGION_NUM * PLC_SIGNAL_NUM_PER_REGION);
|
|
|
- m_plc_current_error = connect();
|
|
|
- m_plc_cond_exit.Notify(false);
|
|
|
- m_plc_thread = new std::thread(plc_update_thread, this);
|
|
|
- mb_plc_initialized = true;
|
|
|
+ m_plc_data.resize(PLC_REGION_NUM * PLC_SIGNAL_NUM_PER_REGION);
|
|
|
+ m_plc_current_error = connect();
|
|
|
+ m_plc_cond_exit.Notify(false);
|
|
|
+ m_plc_thread = new std::thread(plc_update_thread, this);
|
|
|
+ mb_plc_initialized = true;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
Plc_Communicator::~Plc_Communicator()
|