From 0150bc0d35136c8fa2b104aea008449708f2e3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kiss=20=C3=81d=C3=A1m?= <kiss.adam.fkut@gmail.com> Date: Fri, 14 Apr 2017 00:18:01 +0200 Subject: [PATCH] Finished commenting, revised the code, added TODOes. --- devices/jelenletjelzo.cpp | 12 ++++++------ main.cpp | 2 ++ modbus_poll.cpp | 4 ++-- semmodbus_server.layout | 36 ++++++++++++++++++------------------ socket_listener.cpp | 12 +++++++++--- 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/devices/jelenletjelzo.cpp b/devices/jelenletjelzo.cpp index 4cb95ba..e8dfb43 100644 --- a/devices/jelenletjelzo.cpp +++ b/devices/jelenletjelzo.cpp @@ -1,19 +1,19 @@ #include "devices/jelenletjelzo.hpp" void jelenletjelzo::poll(modbus_t* modbus){ - modbus_flush(modbus); - modbus_set_slave(modbus, 1); + modbus_flush(modbus); //Flush modbus buffers + modbus_set_slave(modbus, 1); //Set slave address to 1 uint16_t asd; - if (modbus_read_input_registers(modbus, 0x00, 1, &asd) == -1) { + if (modbus_read_input_registers(modbus, 0x00, 1, &asd) == -1) { //read 1 (third arg) input register, from register index 0(sec arg), to the address of asd fprintf(stderr, "Error %d while reading: %s\n", errno, modbus_strerror(errno)); return; } - m.lock(); - stat = asd&(1<<7); - m.unlock(); + m.lock(); //lock semaphore + stat = asd&(1<<7); //set stat + m.unlock(); //unlock } json jelenletjelzo::getResp(const json& Request){ diff --git a/main.cpp b/main.cpp index e889156..3adf271 100644 --- a/main.cpp +++ b/main.cpp @@ -33,11 +33,13 @@ void signal_handler(int signo){ bool init(){ try{ + //Set signal handler if (signal(SIGTERM, signal_handler) == SIG_ERR) throw std::string("\ncan't catch SIGTERM\n"); if (signal(SIGINT, signal_handler) == SIG_ERR) throw std::string("\ncan't catch SIGINT\n"); + //create thread to listen socket SocketListenerThread = new std::thread(&SocketListener); }catch(std::string& str){ std::cout << str; diff --git a/modbus_poll.cpp b/modbus_poll.cpp index dae5784..7f04d04 100644 --- a/modbus_poll.cpp +++ b/modbus_poll.cpp @@ -25,7 +25,7 @@ void modbus_thread_terminate(){ void Modbus_Poll(){ std::cout << "Polling the slaves...\n\n"; - std::set_terminate(modbus_thread_terminate); //Sets the function wjivh run ehen rrhe rhread terminates + std::set_terminate(modbus_thread_terminate); //Sets the function which run when the thred terminates modbus = modbus_new_rtu("/dev/ttyUSB0", 9600, 'N', 8, 1); @@ -33,7 +33,7 @@ void Modbus_Poll(){ modbus_rtu_set_serial_mode(modbus, MODBUS_RTU_RS485); - if (modbus_connect(modbus) == -1) { + if (modbus_connect(modbus) == -1) { //If cannot connect to modbus fprintf(stderr, "Connexion failed: %s\n", modbus_strerror(errno)); modbus_free(modbus); return; diff --git a/semmodbus_server.layout b/semmodbus_server.layout index 769011b..e255f92 100644 --- a/semmodbus_server.layout +++ b/semmodbus_server.layout @@ -1,52 +1,52 @@ <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <CodeBlocks_layout_file> <ActiveTarget name="Release" /> - <File name="semmodbusdevice.hpp" open="1" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="main.cpp" open="0" top="0" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="301" topLine="0" /> + <Cursor1 position="1414" topLine="24" /> </Cursor> </File> - <File name="modbus_poll.cpp" open="1" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="devices\jelenletjelzo.hpp" open="0" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="344" topLine="4" /> + <Cursor1 position="318" topLine="0" /> </Cursor> </File> - <File name="devices\jelenletjelzo.hpp" open="1" top="0" tabpos="6" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="semmodbus.hpp" open="0" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="164" topLine="0" /> + <Cursor1 position="76" topLine="0" /> </Cursor> </File> - <File name="socket_listener.cpp" open="1" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="semmodbusdevice.hpp" open="0" top="0" tabpos="5" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="245" topLine="36" /> + <Cursor1 position="301" topLine="0" /> </Cursor> </File> - <File name="modbus_poll.hpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="socket_listener.cpp" open="0" top="0" tabpos="4" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="148" topLine="0" /> + <Cursor1 position="2121" topLine="7" /> </Cursor> </File> - <File name="socket_listener.hpp" open="1" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="socket_listener.hpp" open="0" top="0" tabpos="2" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> <Cursor1 position="0" topLine="0" /> </Cursor> </File> - <File name="devices\jelenletjelzo.cpp" open="1" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="modbus_poll.hpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="523" topLine="0" /> + <Cursor1 position="148" topLine="0" /> </Cursor> </File> - <File name="semmodbus.hpp" open="1" top="0" tabpos="9" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="modbus_poll.cpp" open="0" top="0" tabpos="3" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="76" topLine="0" /> + <Cursor1 position="176" topLine="4" /> </Cursor> </File> - <File name="main.cpp" open="1" top="1" tabpos="1" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="devices\jelenletjelzo.cpp" open="0" top="0" tabpos="7" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> - <Cursor1 position="479" topLine="0" /> + <Cursor1 position="813" topLine="0" /> </Cursor> </File> - <File name="Makefile" open="1" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> + <File name="Makefile" open="0" top="0" tabpos="8" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0"> <Cursor> <Cursor1 position="97" topLine="0" /> </Cursor> diff --git a/socket_listener.cpp b/socket_listener.cpp index e543054..e788143 100644 --- a/socket_listener.cpp +++ b/socket_listener.cpp @@ -25,8 +25,8 @@ using json = nlohmann::json; */ void serveConnect(Socket s){ std::cout << "A client connected\n"; - char buffer[256]; - int n; + char buffer[256]; //The buffer, where the socket buffer will be moved + int n; //general porpuse integer var try{ std::string string_input; @@ -39,6 +39,11 @@ void serveConnect(Socket s){ string_input = std::string(buffer); auto json_request = json::parse(string_input); + + /*! \brief Stores the requested name of the device + * + * When No device is found with the specified name, the program throws an exception + */ std::string req_device=json_request["Name"]; bool found = false; for(auto* i: Devices){ @@ -68,6 +73,7 @@ void serveConnect(Socket s){ close(s); } + /*! \brief Socket bind to listen */ Socket lisener_socket; @@ -120,6 +126,6 @@ void SocketListener(){ if (accpeted < 0) { perror("ERROR on accept"); } - Connects.push_back(new std::thread(serveConnect, accpeted)); + Connects.push_back(new std::thread(serveConnect, accpeted)); ///TODO: fix memory leak } } -- GitLab