Browse Source

add open(string host, int port)

pehladik 4 years ago
parent
commit
1d2df7017d

+ 32
- 2
software/raspberry/superviseur-robot/lib/comrobot.cpp View File

@@ -31,7 +31,7 @@
31 31
 #include <netinet/in.h>
32 32
 #include <arpa/inet.h>
33 33
 int sock = 0;
34
-const char* host = "127.0.0.1";
34
+string host = "127.0.0.1";
35 35
 #define PORT 6699
36 36
 #endif
37 37
 
@@ -96,7 +96,7 @@ int ComRobot::Open(string usart) {
96 96
     serv_addr.sin_port = htons(PORT);
97 97
 
98 98
     // Convert IPv4 and IPv6 addresses from text to binary form 
99
-    if (inet_pton(AF_INET, host, &serv_addr.sin_addr) <= 0) {
99
+    if (inet_pton(AF_INET, host.c_str(), &serv_addr.sin_addr) <= 0) {
100 100
         printf("\nInvalid address/ Address not supported \n");
101 101
         return -1;
102 102
     }
@@ -127,6 +127,36 @@ int ComRobot::Open(string usart) {
127 127
 #endif
128 128
 }
129 129
 
130
+int ComRobot::Open(string shost, int nport) {
131
+#ifdef __SIMULATION__
132
+
133
+    struct sockaddr_in serv_addr;
134
+    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
135
+        printf("\n Socket creation error \n");
136
+        return -1;
137
+    }
138
+    struct timeval tv;
139
+    tv.tv_sec = 0;
140
+    tv.tv_usec = 200000;
141
+    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char*) &tv, sizeof tv);
142
+
143
+    serv_addr.sin_family = AF_INET;
144
+    serv_addr.sin_port = htons(nport);
145
+
146
+    if (inet_pton(AF_INET, shost.c_str(), &serv_addr.sin_addr) <= 0) {
147
+        cout << "Invalid address/ Address not supported" << endl;
148
+        return -1;
149
+    }
150
+
151
+    if (connect(sock, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) < 0) {
152
+        return -2;
153
+    }
154
+    return 1;
155
+#else
156
+    return -1
157
+#endif
158
+}
159
+
130 160
 /**
131 161
  * Close serial link
132 162
  * @return Success if above 0, failure if below 0

+ 10
- 2
software/raspberry/superviseur-robot/lib/comrobot.h View File

@@ -60,6 +60,14 @@ public:
60 60
     int Open(string usart);
61 61
 
62 62
     /**
63
+     * Open tcp client for the graphical simulator
64
+     * @param host Host address
65
+     * @param port Port number
66
+     * @return 1 if ok or -1
67
+     */
68
+    int Open(string shost, int nport);
69
+
70
+    /**
63 71
      * Close serial link
64 72
      * @return Success if above 0, failure if below 0
65 73
      */
@@ -89,9 +97,9 @@ public:
89 97
      */
90 98
     virtual void Write_Post() {
91 99
     }
92
-    
100
+
93 101
     Message *SendCommand(Message* msg, MessageID answerID, int maxRetries);
94
-    
102
+
95 103
     static Message *Ping() {
96 104
         return new Message(MESSAGE_ROBOT_PING);
97 105
     }

Loading…
Cancel
Save