plc_test.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // Created by zx on 2019/12/28.
  3. //
  4. #include <iostream>
  5. #include "plc_communicator.h"
  6. class Test{
  7. public:
  8. Test(Plc_Communicator* p){
  9. mp_pc = p;
  10. }
  11. static Error_manager callback(int terminal_id, void *p_owner)
  12. {
  13. Test* t = (Test*)p_owner;
  14. t->m_term_id = terminal_id;
  15. std::cout<<"callback "<<t->m_term_id<<" called."<<std::endl;
  16. usleep(1000 * 1000);
  17. if(t->mp_pc!=0){
  18. // Plc_Task task;
  19. struct measure_result result;
  20. result.terminal_id = t->m_term_id;
  21. result.correctness = terminal_id % 2 == 0?true:false;
  22. result.x = 3501.2f;
  23. result.angle = 91.35f;
  24. result.wheel_base = 2651.6f;
  25. Error_manager ec = t->m_task.set_result(result);
  26. std::cout<<"callback set result "<<ec.to_string()<<std::endl;
  27. std::thread* th = new std::thread(execute_thread, t);
  28. th->detach();
  29. }
  30. return Error_manager(Error_code::SUCCESS);
  31. }
  32. static void execute_thread(Test* t){
  33. usleep(1000 * 2000);
  34. if(t->mp_pc!= 0){
  35. Error_manager ec = t->mp_pc->execute_task(&t->m_task);
  36. std::cout << "thread execute task " << ec.to_string() << std::endl;
  37. int count = 1;
  38. while (count-- > 0)
  39. {
  40. std::cout << "---------- "<<count<<" --------" << std::endl;
  41. std::vector<uint16_t> vec;
  42. ec = t->mp_pc->get_plc_data(vec, t->m_term_id);
  43. std::cout << "get data " << ec.to_string() << std::endl;
  44. std::cout << "[ ";
  45. for (size_t i = 0; i < vec.size(); i++)
  46. {
  47. std::cout << vec[i] << " ";
  48. }
  49. std::cout << "]" << std::endl;
  50. ec = t->mp_pc->set_status_update_timeout(15000);
  51. std::cout << "set status " << ec.to_string() << std::endl;
  52. ec = t->mp_pc->get_error();
  53. std::cout << "get error " << ec.to_string() << std::endl;
  54. bool init_state = t->mp_pc->get_initialize_status();
  55. std::cout << "get initialize status " << init_state << std::endl;
  56. bool conn = t->mp_pc->get_connection();
  57. std::cout << "get connection status " << conn << std::endl;
  58. usleep(1000 * 1500);
  59. }
  60. }
  61. }
  62. private:
  63. int m_term_id;
  64. Plc_Communicator* mp_pc;
  65. Plc_Task m_task;
  66. };
  67. int main(int argc,char* argv[])
  68. {
  69. google::InitGoogleLogging("plc");
  70. plc_module::plc_connection_params params;
  71. params.set_ip("192.168.2.131");
  72. params.set_port(502);
  73. params.set_slave_id(1);
  74. Plc_Communicator pc(params);
  75. Test test(&pc);
  76. Error_manager ec = pc.set_plc_callback(test.callback, &test);
  77. std::cout<<"set callback "<<ec.to_string()<<std::endl;
  78. // Error_manager ec = Error_manager(Error_code::SUCCESS);
  79. // // Error_manager ec = Error_manager(Error_code::SUCCESS, Error_level::NEGLIGIBLE_ERROR, "测试。。。");
  80. // std::cout<<"test 000"<<std::endl;
  81. // std::cout<<"test "<<ec.get_error_code()<<std::endl;
  82. // std::cout<<"test "<<ec.get_error_level()<<std::endl;
  83. // std::cout<<"test "<<ec.get_error_description()<<std::endl;
  84. // std::cout<<"test "<<ec.to_string()<<std::endl;
  85. getchar();
  86. google::ShutdownGoogleLogging();
  87. return 0;
  88. }