1 |
/* Sample implementation of a H7 server for linux |
2 |
* this application uses boost::thread library (http://boost.org) |
3 |
* and cppsocket (http:://sourceforge.net/projects/cppsocket) |
4 |
*/ |
5 |
|
6 |
#include <iostream> |
7 |
#include <string> |
8 |
|
9 |
#include <boost/thread/thread.hpp> |
10 |
#include <boost/thread/mutex.hpp> |
11 |
|
12 |
#include "../H7Serial.h" |
13 |
#include "serialthread.h" |
14 |
#include "serverthread.h" |
15 |
|
16 |
#include "globalstorage.h" |
17 |
|
18 |
void my_pause() |
19 |
{ |
20 |
char d[1]; |
21 |
std::cout << "Press enter to continue" << std::endl; |
22 |
std::cin.getline(d,1); |
23 |
} |
24 |
|
25 |
int main(int argc, char* argv[]) |
26 |
{ |
27 |
CH7Serial& serialPort = GlobalStorage::instance()->serialPort; |
28 |
|
29 |
SerialThread& serial = GlobalStorage::instance()->serialThread; |
30 |
ServerThread server; |
31 |
// GlobalStorage::instance()->serialThread = &serial; |
32 |
|
33 |
try |
34 |
{ |
35 |
serialPort.open("/dev/ttyS0", CSerial::Baud9600); |
36 |
} |
37 |
catch(std::exception&e ) |
38 |
{ |
39 |
std::cout << "Error opening serial port : " << e.what() << std::endl; |
40 |
exit(1); |
41 |
} |
42 |
|
43 |
try |
44 |
{ |
45 |
GlobalStorage::instance()->server.start(9000); //listen on port 9000 |
46 |
} |
47 |
catch(...) |
48 |
{ |
49 |
std::cout << "Could not listen on port 9000" << std::endl; |
50 |
serialPort.close(); |
51 |
exit(2); |
52 |
} |
53 |
|
54 |
|
55 |
|
56 |
boost::thread serialThread(serial); |
57 |
boost::thread serverThread(server); |
58 |
|
59 |
my_pause(); |
60 |
|
61 |
serial.stop(); |
62 |
server.stop(); |
63 |
while(!serial.stopped() && !server.stopped() ) |
64 |
usleep(10000); |
65 |
} |