Commit da5be7b4 authored by pengelstein's avatar pengelstein
Browse files

Cross-plateform serial communication

Updated cross-plateform code
parent c17592e8
......@@ -3,4 +3,5 @@ build/
result.txt
*.ol
*.ow
output/
vgcore*
\ No newline at end of file
cmake_minimum_required (VERSION 3.14.4)
cmake_policy(VERSION 3.14.4)
project(TcpServer VERSION 0.1)
# Setup
set(PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR})
enable_language(CXX)
set (CMAKE_CXX_STANDARD 17)
find_package(Threads REQUIRED)
if(WIN32)
set(LIB wsock32 ws2_32)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/output/windows)
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/output/windows)
else()
set(LIB ${CMAKE_THREAD_LIBS_INIT})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/output/linux)
set(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/output/linux)
endif()
find_package(Threads REQUIRED)
# Thread library
file(GLOB_RECURSE tcpthreadsrc "src/thread/*.cpp")
add_library(tcpthread STATIC ${tcpthreadsrc})
target_include_directories(tcpthread PRIVATE ${PROJECT_DIR}/src/)
# Serial library
file(GLOB_RECURSE SerialSRC "src/serial/*.cpp")
add_library(serial STATIC ${SerialSRC})
target_include_directories(serial PRIVATE ${PROJECT_DIR}/src/)
# Main program
file(GLOB_RECURSE SOURCES
${PROJECT_DIR}/src/main.cpp
${PROJECT_DIR}/src/TestMessage.cpp
)
add_executable(server ${SOURCES})
target_include_directories(server PRIVATE ${PROJECT_DIR}/src/)
target_link_directories(server PRIVATE ${PROJECT_DIR}/lib)
target_link_libraries(server
PRIVATE
tcpthread
serial
${LIB}
)
\ No newline at end of file
......@@ -90,7 +90,7 @@ $(document).ready(function(){initNavTree('_client_8hpp_source.html','');});
<div class="title">Client.hpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="preprocessor">#ifndef CLIENT_H</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="preprocessor">#define CLIENT_H</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;</div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;<span class="preprocessor">#include &lt;memory&gt;</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;<span class="preprocessor">#include &lt;cstring&gt;</span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;</div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;<span class="preprocessor">#ifdef __linux</span></div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;<span class="preprocessor">#include &lt;netdb.h&gt;</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;<span class="preprocessor">#include &lt;arpa/inet.h&gt;</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;<span class="preprocessor">#elif defined __WIN32</span></div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;<span class="preprocessor">#include &lt;winsock2.h&gt;</span></div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;<span class="preprocessor">#include &lt;windows.h&gt;</span></div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;<span class="preprocessor">#include &lt;stdint.h&gt;</span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;<span class="preprocessor">#include &lt;windef.h&gt;</span></div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;<span class="preprocessor">#include &lt;ws2tcpip.h&gt;</span></div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160;<span class="keyword">typedef</span> <span class="keywordtype">int</span> __socklen_t;</div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;<span class="preprocessor">#define ACCEPT_MAX 5</span></div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;<span class="keyword">typedef</span> <span class="keyword">struct </span>sockaddr_in address_t;</div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160;</div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacethreads.html">threads</a></div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;{</div><div class="line"><a name="l00030"></a><span class="lineno"><a class="line" href="classthreads_1_1_client.html"> 30</a></span>&#160; <span class="keyword">class </span><a class="code" href="classthreads_1_1_client.html">Client</a></div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160; {</div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00034"></a><span class="lineno"><a class="line" href="classthreads_1_1_client.html#a28c57bc0a1cc21ce8d8f3eb81c596e52"> 34</a></span>&#160; <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#a28c57bc0a1cc21ce8d8f3eb81c596e52">nb_client_socket</a>;</div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160; <a class="code" href="classthreads_1_1_client.html">Client</a>();</div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; <span class="keyword">virtual</span> ~<a class="code" href="classthreads_1_1_client.html">Client</a>();</div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; std::string <a class="code" href="classthreads_1_1_client.html#ad7402801db23143512f3e7e1377f69bf">IP</a>();</div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; uint16_t <a class="code" href="classthreads_1_1_client.html#a0d9231d5bcd729958c9ac83efa480ff7">Port</a>();</div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#ae8b5b837c28fdca7af5d86b6fc7430fe">Socket</a>();</div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; __socklen_t <a class="code" href="classthreads_1_1_client.html#ab89cbf6635b041a23636581c5d8c3fce">Socket_Length</a>();</div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; </div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; <span class="keywordtype">void</span> <a class="code" href="classthreads_1_1_client.html#a20a4ba9f29551dbc72b633b092e8d9a2">Set</a>();</div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#a2210037971520845b2304073ca827414">SetSocket</a>(<span class="keywordtype">int</span> server_socket);</div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#a243a6bb440e0edb4505e7d066a1e9b3f">CloseClient</a>();</div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#ae4dc1c2cf2724313d9dfde24fb5f79e2">Close</a>(<span class="keywordtype">int</span> socket);</div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160;</div><div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; std::string ToString();</div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160;</div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <span class="comment">/* All the client description */</span></div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; std::string ip;</div><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160; uint16_t port;</div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; <span class="keywordtype">int</span> socket;</div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="keywordtype">bool</span> active;</div><div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; __socklen_t socket_length;</div><div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; <span class="keyword">struct </span>sockaddr_in socket_address;</div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; };</div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160;}</div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;</div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;<span class="preprocessor">#endif</span></div><div class="ttc" id="classthreads_1_1_client_html_a243a6bb440e0edb4505e7d066a1e9b3f"><div class="ttname"><a href="classthreads_1_1_client.html#a243a6bb440e0edb4505e7d066a1e9b3f">threads::Client::CloseClient</a></div><div class="ttdeci">int CloseClient()</div><div class="ttdef"><b>Definition:</b> Client.cpp:100</div></div>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="preprocessor">#ifndef CLIENT_H</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="preprocessor">#define CLIENT_H</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;</div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;<span class="preprocessor">#include &lt;memory&gt;</span></div><div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="preprocessor">#include &lt;string&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;<span class="preprocessor">#include &lt;cstring&gt;</span></div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="preprocessor">#include &lt;unistd.h&gt;</span></div><div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;</div><div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;<span class="preprocessor">#ifdef __linux</span></div><div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;<span class="preprocessor">#include &lt;netdb.h&gt;</span></div><div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;<span class="preprocessor">#include &lt;arpa/inet.h&gt;</span></div><div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;<span class="preprocessor">#elif defined _WIN32</span></div><div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;<span class="preprocessor">#include &lt;winsock2.h&gt;</span></div><div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;<span class="preprocessor">#include &lt;windows.h&gt;</span></div><div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;<span class="preprocessor">#include &lt;stdint.h&gt;</span></div><div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;<span class="preprocessor">#include &lt;windef.h&gt;</span></div><div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;<span class="preprocessor">#include &lt;ws2tcpip.h&gt;</span></div><div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160;<span class="keyword">typedef</span> <span class="keywordtype">int</span> __socklen_t;</div><div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;<span class="preprocessor">#define ACCEPT_MAX 5</span></div><div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160;<span class="keyword">typedef</span> <span class="keyword">struct </span>sockaddr_in address_t;</div><div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160;</div><div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacethreads.html">threads</a></div><div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160;{</div><div class="line"><a name="l00030"></a><span class="lineno"><a class="line" href="classthreads_1_1_client.html"> 30</a></span>&#160; <span class="keyword">class </span><a class="code" href="classthreads_1_1_client.html">Client</a></div><div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160; {</div><div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00034"></a><span class="lineno"><a class="line" href="classthreads_1_1_client.html#a28c57bc0a1cc21ce8d8f3eb81c596e52"> 34</a></span>&#160; <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#a28c57bc0a1cc21ce8d8f3eb81c596e52">nb_client_socket</a>;</div><div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160; <a class="code" href="classthreads_1_1_client.html">Client</a>();</div><div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; <span class="keyword">virtual</span> ~<a class="code" href="classthreads_1_1_client.html">Client</a>();</div><div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; std::string <a class="code" href="classthreads_1_1_client.html#ad7402801db23143512f3e7e1377f69bf">IP</a>();</div><div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160; uint16_t <a class="code" href="classthreads_1_1_client.html#a0d9231d5bcd729958c9ac83efa480ff7">Port</a>();</div><div class="line"><a name="l00048"></a><span class="lineno"> 48</span>&#160; <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#ae8b5b837c28fdca7af5d86b6fc7430fe">Socket</a>();</div><div class="line"><a name="l00052"></a><span class="lineno"> 52</span>&#160; __socklen_t <a class="code" href="classthreads_1_1_client.html#ab89cbf6635b041a23636581c5d8c3fce">Socket_Length</a>();</div><div class="line"><a name="l00053"></a><span class="lineno"> 53</span>&#160; </div><div class="line"><a name="l00057"></a><span class="lineno"> 57</span>&#160; <span class="keywordtype">void</span> <a class="code" href="classthreads_1_1_client.html#a20a4ba9f29551dbc72b633b092e8d9a2">Set</a>();</div><div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#a2210037971520845b2304073ca827414">SetSocket</a>(<span class="keywordtype">int</span> server_socket);</div><div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#a243a6bb440e0edb4505e7d066a1e9b3f">CloseClient</a>();</div><div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="classthreads_1_1_client.html#ae4dc1c2cf2724313d9dfde24fb5f79e2">Close</a>(<span class="keywordtype">int</span> socket);</div><div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160;</div><div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; std::string ToString();</div><div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160;</div><div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <span class="comment">/* All the client description */</span></div><div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; std::string ip;</div><div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160; uint16_t port;</div><div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; <span class="keywordtype">int</span> socket;</div><div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="keywordtype">bool</span> active;</div><div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160; __socklen_t socket_length;</div><div class="line"><a name="l00085"></a><span class="lineno"> 85</span>&#160; <span class="keyword">struct </span>sockaddr_in socket_address;</div><div class="line"><a name="l00086"></a><span class="lineno"> 86</span>&#160; };</div><div class="line"><a name="l00087"></a><span class="lineno"> 87</span>&#160;}</div><div class="line"><a name="l00088"></a><span class="lineno"> 88</span>&#160;</div><div class="line"><a name="l00089"></a><span class="lineno"> 89</span>&#160;<span class="preprocessor">#endif</span></div><div class="ttc" id="classthreads_1_1_client_html_a243a6bb440e0edb4505e7d066a1e9b3f"><div class="ttname"><a href="classthreads_1_1_client.html#a243a6bb440e0edb4505e7d066a1e9b3f">threads::Client::CloseClient</a></div><div class="ttdeci">int CloseClient()</div><div class="ttdef"><b>Definition:</b> Client.cpp:100</div></div>
<div class="ttc" id="classthreads_1_1_client_html_a0d9231d5bcd729958c9ac83efa480ff7"><div class="ttname"><a href="classthreads_1_1_client.html#a0d9231d5bcd729958c9ac83efa480ff7">threads::Client::Port</a></div><div class="ttdeci">uint16_t Port()</div><div class="ttdef"><b>Definition:</b> Client.cpp:23</div></div>
<div class="ttc" id="classthreads_1_1_client_html_a20a4ba9f29551dbc72b633b092e8d9a2"><div class="ttname"><a href="classthreads_1_1_client.html#a20a4ba9f29551dbc72b633b092e8d9a2">threads::Client::Set</a></div><div class="ttdeci">void Set()</div><div class="ttdef"><b>Definition:</b> Client.cpp:38</div></div>
<div class="ttc" id="classthreads_1_1_client_html_ab89cbf6635b041a23636581c5d8c3fce"><div class="ttname"><a href="classthreads_1_1_client.html#ab89cbf6635b041a23636581c5d8c3fce">threads::Client::Socket_Length</a></div><div class="ttdeci">__socklen_t Socket_Length()</div><div class="ttdef"><b>Definition:</b> Client.cpp:33</div></div>
......
......@@ -90,7 +90,7 @@ $(document).ready(function(){initNavTree('cross_8hpp_source.html','');});
<div class="title">cross.hpp</div> </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="preprocessor">#ifdef __WIN32</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="preprocessor">#include &lt;winsock2.h&gt;</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;</div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="keywordtype">void</span> Error();</div></div><!-- fragment --></div><!-- contents -->
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="preprocessor">#ifdef _WIN32</span></div><div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;<span class="preprocessor">#include &lt;winsock2.h&gt;</span></div><div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="preprocessor">#endif</span></div><div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;</div><div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="keywordtype">void</span> Error();</div></div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
......
.DEFAULT_GOAL := build_linux
src = $(wildcard src/*.cpp\
src/thread/*.cpp\
src/thread/Socket/*.cpp\
src/thread/Delay/*.cpp\
src/thread/Message/*.cpp)
obj_linux = $(src:.cpp=.ol)
obj_windows = $(src:.cpp=.ow)
B = g++
LINUX_FLAGS = -std=c++17 -lpthread -Wall -Werror
LINUX_BUILD = build/linux/server
LINUX_INCLUDE = -Isrc/
MINGW = x86_64-w64-mingw32-g++
WINDOWS_FLAGS = -std=c++17 -lws2_32 -static-libgcc -static-libstdc++ -pthread
WINDOWS_BUILD = build/windows/server.exe
WINDOWS_INCLUDE = -Isrc/
build_linux: $(obj_linux)
mkdir -p build/linux
$(B) $^ -o $(LINUX_BUILD) $(LINUX_FLAGS)
rm -f $(obj_linux)
%.ol:%.cpp
$(B) -Wall -c $^ -o $@ $(LINUX_INCLUDE)
run_linux:build_linux
build/linux/server
build_windows: $(obj_windows)
mkdir -p build/windows
$(MINGW) $^ -o $(WINDOWS_BUILD) $(WINDOWS_FLAGS)
cp lib/libwinpthread-1.dll build/windows/libwinpthread-1.dll
rm -f $(obj_windows)
%.ow:%.cpp
$(MINGW) -Wall -c $^ -o $@ $(WINDOWS_INCLUDE)
run_windows:build_windows
wine build/windows/server.exe
clean:
rm -f $(obj_linux)
rm -f $(obj_windows)
rm -rf build/
\ No newline at end of file
File deleted
......@@ -28,65 +28,42 @@ int TestMessage::TreatData(std::vector<char> buffer)
for(auto st : values)
{
Light l = {-1, -1};
//Get the ID of the light
if(st.at(0) >= '0' || st.at(0) <= '9')
l.index = atoi(&(st.at(0)));
if(st.size() == 3)
{
Light l = {-1, -1};
if(st.at(0) >= '0' || st.at(0) <= '9')
l.index = atoi(&(st.at(0)));
std::vector<char> buffer;
std::vector<char> buffer_out;
int state = atoi(&st.at(3));
int state = atoi(&st.at(3));
if(prev_state != state)
{
switch(state)
if(prev_state != state)
{
case 0:
buffer.push_back('2');
this->ser->Write(buffer);
break;
case 1:
buffer.push_back('3');
this->ser->Write(buffer);
break;
case 2:
buffer.push_back('5');
this->ser->Write(buffer);
break;
case 3:
buffer.push_back('6');
this->ser->Write(buffer);
break;
}
switch(state)
{
case 0:
buffer_out.push_back('2');
this->ser->Write(buffer_out);
break;
case 1:
buffer_out.push_back('3');
this->ser->Write(buffer_out);
break;
case 2:
buffer_out.push_back('5');
this->ser->Write(buffer_out);
break;
case 3:
buffer_out.push_back('6');
this->ser->Write(buffer_out);
break;
}
buffer.clear();
prev_state = state;
buffer_out.clear();
prev_state = state;
}
}
// switch(st.at(3))
// {
// case 'r':
// //Get the value of the light
// l.value = st.at(3);
// //Send it to the arduino
// buffer.push_back('1');
// this->ser->Write(buffer);
// break;
// case 'y':
// buffer.push_back('2');
// l.value = st.at(3);
// break;
// case 'g':
// buffer.push_back('3');
// l.value = st.at(3);
// break;
// default:
// break;
// }
buffer.clear();
// printf("light %d has value %c\n", l.index, l.value);
}
return 0;
}
......
......@@ -33,7 +33,7 @@ void handle_close(int signal_num)
running = false;
}
#ifdef __WIN32
#ifdef _WIN32
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
......@@ -49,13 +49,16 @@ BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
int main(int argc, char **argv)
{
printf("Hello World !\n");
int port = 9999;
std::string protocol = "tcp";
int max_queue = 10;
int max_pool = 5;
#ifdef __linux
std::string device = "/dev/ttyACM1";
#elif defined _WIN32
std::string device = "COM1";
#endif
int speed = 9600;
int byte_size = 8;
......@@ -128,7 +131,7 @@ int main(int argc, char **argv)
printf("Launching server using port %d, protocol %s, max_queue %d, max_pool %d\n", port, protocol.c_str(), max_queue, max_pool);
#ifdef __WIN32
#ifdef _WIN32
WSADATA wsa;
int err = WSAStartup(MAKEWORD(2, 2), &wsa);
if(err < 0)
......
......@@ -5,6 +5,7 @@ namespace Serial
int get_baud(int baud)
{
#ifdef __linux
switch (baud)
{
case 9600:
......@@ -46,13 +47,19 @@ namespace Serial
default:
return -1;
}
#elif defined _WIN32
return baud;
#endif
}
Serial::Serial(const char *port, unsigned int baudrate, unsigned int byteSize)
{
this->name = std::string(port);
this->baudrate = get_baud(baudrate);
this->connected = false;
#ifdef __linux
if(this->baudrate == -1)
{
fprintf(stderr, "[Serial] Got wrong baudrate %d, using 9600.\n", baudrate);
......@@ -91,15 +98,66 @@ namespace Serial
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
if(this->port == -1)
{
fprintf(stderr, "[Serial] Failed to create serial to %s.\n", port);
this->connected = false;
}
else
{
fprintf(stdout, "[Serial] Created serial %d\n", this->port);
this->connected = true;
}
#elif defined _WIN32
this->byteSize = byteSize;
this->handler = CreateFileA(static_cast<LPCSTR>(port), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(this->handler == INVALID_HANDLE_VALUE)
{
if(GetLastError() == ERROR_FILE_NOT_FOUND)
fprintf(stderr, "[Serial] Failed to create serial : handle was not attached, %s is not available.\n", port);
else
fprintf(stderr, "[Serial] Failed to create serial : ERROR.\n");
}else
{
DCB dcbSerialParameters = {0};
if(!GetCommState(this->handler, &dcbSerialParameters))
{
fprintf(stderr, "[Serial] Failed to get current serial parameters.\n");
}
else
{
dcbSerialParameters.BaudRate = static_cast<DWORD> (this->baudrate);
dcbSerialParameters.ByteSize = this->baudrate;
dcbSerialParameters.StopBits = ONESTOPBIT;
dcbSerialParameters.Parity = NOPARITY;
dcbSerialParameters.fDtrControl = DTR_CONTROL_ENABLE;
if(!SetCommState(this->handler, &dcbSerialParameters))
{
fprintf(stderr, "[Serial] Warning, could not set Serial port parameters.\n");
}
else
{
this->connected = true;
PurgeComm(this->handler, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(ARDUINO_WAIT_TIME);
}
}
}
#endif
}
Serial::~Serial()
{
if(this->port != -1)
if(this->connected)
{
#ifdef __linux
Close();
#elif defined _WIN32
CloseHandle(this->handler);
#endif
}
}
void Serial::Open()
......@@ -109,51 +167,116 @@ namespace Serial
void Serial::Close()
{
fprintf(stdout, "[Serial] Closing port %d\n", this->port);
if(this->port != -1)
if(this->connected)
#ifdef __linux
fprintf(stdout, "[Serial] Closing port %d\n", this->port);
close(this->port);
this->port = -1;
this->port = -1;
#elif defined _WIN32
fprintf(stdout, "[Serial] Closing serial\n");
CloseHandle(this->handler);
#endif
this->connected = false;
}
std::vector<char> Serial::Read(int size)
{
std::vector<char> res(size);
int error = -1;
if(this->port != -1)
error = read(this->port, &res[0], size);
if(error == -1)
if(this->connected)
{
res.resize(0);
return res;
#ifdef __linux
int error = read(this->port, &res[0], size);
if(error == -1)
{
res.resize(0);
return res;
}
else
return res;
#elif defined _WIN32
DWORD byteRead;
unsigned int toRead;
ClearCommError(this->handler, &this->errors, &this->status);
if(this->status.cbInQue > 0)
{
if(this->status.cbInQue > size)
toRead = size;
else
toRead = this->status.cbInQue;
}
if(ReadFile(this->handler, &res[0], toRead, &byteRead, NULL))
{
return res;
}else
{
res.resize(0);
return res;
}
#endif
}
else
return res;
res.resize(0);
return res;
}
std::string Serial::ReadLine()
{
std::string res = "";
char current = 0;
int error = -1;
do
if(this->connected)
{
if(this->port != -1)
error = read(this->port, &current, 1);
if(error != -1)
res += current;
}while(current != '\n');
#ifdef __linux
char current = 0;
int error = -1;
do
{
if(this->connected)
error = read(this->port, &current, 1);
if(error != -1)
res += current;
}while(current != '\n');
return res;
#elif defined _WIN32
char current = 0;
bool error = false;
DWORD byteRead;
do
{
ClearCommError(this->handler, &this->errors, &this->status);
error = ReadFile(this->handler, &current, 1, &byteRead, NULL);
if(!error)
res += current;
}while(current != '\n');
return res;
#endif
}
return res;
}
void Serial::Write(std::vector<char> buffer)
{
if(this->port != -1 && buffer.size() != 0)
if(this->connected && buffer.size() != 0)
{
#ifdef __linux
write(this->port, buffer.data(), buffer.size());
#elif defined _WIN32
DWORD bytesSend;
if(!WriteFile(this->handler, (void *) &buffer[0], buffer.size(), &bytesSend, 0 ))
ClearCommError(this->handler, &this->errors, &this->status);
#endif
}
}
int Serial::Port()
{
#ifdef __linux
return this->port;
#elif defined _WIN32
if(this->connected)
return 1;
return -1;
#endif
}
}
\ No newline at end of file
......@@ -2,11 +2,16 @@
#define SERIAL_H
#include <string>
#include <vector>
#ifdef __linux
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#elif defined _WIN32
#include <windows.h>
#define ARDUINO_WAIT_TIME 2000
typedef unsigned int speed_t;
#endif
namespace Serial
{
class Serial
......@@ -27,10 +32,17 @@ namespace Serial
int Port();
private:
int port;
std::string name;
speed_t baudrate;
unsigned int byteSize;
bool connected;
#ifdef __linux
int port;
#elif defined _WIN32
HANDLE handler;
COMSTAT status;
DWORD errors;
#endif
};
}
......
......@@ -24,7 +24,8 @@ namespace Serial
while(this->Running())
{
std::string result = ser->ReadLine();
printf("[Serial Reader] %s", result.c_str());
if(result.compare(""))
printf("[Serial Reader] %s", result.c_str());
}
}
......
#include <thread/Delay/DelayThread.hpp>
#include <chrono>
#ifdef __linux
#include <unistd.h>
#endif
namespace threads
{
......@@ -28,7 +31,8 @@ namespace threads
while(DelayThread::Running())
{
DelayThread::Decrease_All(); //Decrease by one second each thread
sleep(1);
std::this_thread::sleep_for(std::chrono::seconds(1));
//sleep(1);
}
return;
}
......
......@@ -4,7 +4,6 @@
#include <thread>
#include <string>
#include <cstring>
#include <sys/socket.h>
namespace threads
{
......
......@@ -40,7 +40,7 @@ namespace threads
#ifdef __linux
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(socket_address.sin_addr), ip, INET_ADDRSTRLEN);
#elif defined __WIN32
#elif defined _WIN32