diff --git a/devices/jelenletjelzo.cpp b/devices/jelenletjelzo.cpp index 4cb95ba37ca6e17d576e9a4c18712ab2042e10a7..e8dfb4379cf1f960d62373f59ae83d44c5d58385 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 e8891561b728521d2588e2de45bdc98b3b1f62d8..3adf2717005963344db4091c26c00a3503bfb540 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 dae578443f192f8ceb146e1daba39c3398e4a586..7f04d04ba2a2c63d73076b16c5643d0832b64e67 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 769011be3a6eadf50851d5c50ae0adfd4a01dca2..e255f9259e70b84a3a42c2827a27b8eebad78140 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 e5430545f161c6e854743319bcb58434c7c4bcbf..e78814305e485754b5980c778b98a1aee64abc28 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 } }