Browse Source

gestion des flux binaires

Sebastien DI MERCURIO 2 years ago
parent
commit
2c66078b46

+ 12
- 2
software/monitor/monitor/Client.cs View File

@@ -11,6 +11,7 @@ namespace monitor
11 11
         private static NetworkStream myStream = null;
12 12
         private const int BufferMaxSize = 512;
13 13
         private static byte[] buffer = new byte[BufferMaxSize];
14
+
14 15
         private static StringBuilder sb = new StringBuilder();
15 16
         private static int newLength = 1;
16 17
 
@@ -51,10 +52,12 @@ namespace monitor
51 52
 
52 53
         private const int BufferMaxSize = 512;
53 54
         private static byte[] buffer = new byte[BufferMaxSize];
55
+        private static byte[] receiveBuffer;
56
+        private static int initialReceiveBufferIndex = 0;
54 57
         private static StringBuilder message = new StringBuilder();
55 58
         private static int newLength = 1;
56 59
 
57
-        public delegate void ReadEvent(string msg);
60
+        public delegate void ReadEvent(string msg, byte[] buffer);
58 61
         public static ReadEvent readEvent = null;
59 62
 
60 63
         public Client()
@@ -124,6 +127,11 @@ namespace monitor
124 127
                 if (bytesRead > 0)
125 128
                 {
126 129
                     message.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
130
+                    if (receiveBuffer == null) receiveBuffer = new byte[bytesRead];
131
+                    else Array.Resize<byte>(ref receiveBuffer, initialReceiveBufferIndex + bytesRead);
132
+
133
+                    System.Buffer.BlockCopy(buffer, 0, receiveBuffer, initialReceiveBufferIndex, bytesRead);
134
+                    initialReceiveBufferIndex = receiveBuffer.Length;
127 135
                 }
128 136
 
129 137
                 if (client.Available > 0)
@@ -134,9 +142,11 @@ namespace monitor
134 142
                 }
135 143
                 else
136 144
                 {
137
-                    readEvent?.Invoke(message.ToString());
145
+                    readEvent?.Invoke(message.ToString(), receiveBuffer);
138 146
 
139 147
                     message.Clear();
148
+                    receiveBuffer = null;
149
+                    initialReceiveBufferIndex = 0;
140 150
                 }
141 151
 
142 152
                 stream.BeginRead(buffer, 0, newLength, new AsyncCallback(ReadCallback), message);

+ 4
- 4
software/monitor/monitor/CommandManager.cs View File

@@ -4,7 +4,7 @@ namespace monitor
4 4
 {
5 5
     public class CommandManager
6 6
     {
7
-        public delegate void CommandReceivedEvent(string msg);
7
+        public delegate void CommandReceivedEvent(string msg, byte[] buffer);
8 8
         public CommandReceivedEvent commandReceivedEvent = null;
9 9
 
10 10
         private System.Timers.Timer waitTimer = new System.Timers.Timer();
@@ -50,7 +50,7 @@ namespace monitor
50 50
             Client.Close();
51 51
         }
52 52
 
53
-        private void OnMessageReception(string message)
53
+        private void OnMessageReception(string message, byte[] buffer)
54 54
         {
55 55
             waitTimer.Stop();
56 56
             this.messageReceived = message;
@@ -62,14 +62,14 @@ namespace monitor
62 62
             }
63 63
             else {
64 64
                 waitForAcknowledge = false;
65
-                this.commandReceivedEvent?.Invoke(message);
65
+                this.commandReceivedEvent?.Invoke(message, buffer);
66 66
             }
67 67
         }
68 68
 
69 69
         private void OnMessageTimeout(object sender, System.Timers.ElapsedEventArgs e)
70 70
         {
71 71
             messageReceived = null;
72
-            OnMessageReception(messageReceived);
72
+            OnMessageReception(messageReceived, null);
73 73
         }
74 74
 
75 75
         public CommandManagerStatus SendCommand(string cmd, out string answer, double timeout)

+ 3
- 3
software/monitor/monitor/DestijlCommandManager.cs View File

@@ -50,7 +50,7 @@ namespace monitor
50 50
         private string receivedHeader = null;
51 51
         private string receivedData = null;
52 52
 
53
-        public delegate void CommandReceivedEvent(string header, string data);
53
+        public delegate void CommandReceivedEvent(string header, string data, byte[] buffer);
54 54
         public CommandReceivedEvent commandReceivedEvent = null;
55 55
 
56 56
         public double timeout = 100; // timeout pour les commandes avec acquitement
@@ -76,7 +76,7 @@ namespace monitor
76 76
             if (commandManager != null) commandManager.Close();
77 77
         }
78 78
 
79
-        private void OnCommandReceived(string msg)
79
+        private void OnCommandReceived(string msg, byte[] buffer)
80 80
         {
81 81
             string[] msgs = msg.Split(':');
82 82
 
@@ -86,7 +86,7 @@ namespace monitor
86 86
             if (msgs.Length >= 2) receivedData = msgs[1];
87 87
             else receivedData = null;
88 88
 
89
-            this.commandReceivedEvent?.Invoke(receivedHeader, receivedData);
89
+            this.commandReceivedEvent?.Invoke(receivedHeader, receivedData, buffer);
90 90
         }
91 91
 
92 92
         public bool Open(string hostname)

+ 11
- 4
software/monitor/monitor/MonitorUI.cs View File

@@ -129,7 +129,7 @@ public partial class MainWindow : Gtk.Window
129 129
         a.RetVal = true;
130 130
     }
131 131
 
132
-    public void OnCommandReceivedEvent(string header, string data)
132
+    public void OnCommandReceivedEvent(string header, string data, byte[] buffer)
133 133
     {
134 134
         if (header != null) Console.WriteLine("Received header (" + header.Length + "): " + header);
135 135
         if (data != null) Console.WriteLine("Received data (" + data.Length + "): " + data);
@@ -152,6 +152,13 @@ public partial class MainWindow : Gtk.Window
152 152
                     break;
153 153
             }
154 154
         }
155
+        else if (header.ToUpper() == DestijlCommandList.HeaderStmImage)
156
+        {
157
+            Console.WriteLine("Image received");
158
+            byte[] image = new byte[buffer.Length-4];
159
+            System.Buffer.BlockCopy(buffer, 4, image, 0, image.Length);
160
+            drawingareaCameraPixbuf = new Pixbuf(image.Length, image, true);
161
+        }
155 162
     }
156 163
 
157 164
     protected void OnQuitActionActivated(object sender, EventArgs e)
@@ -382,9 +389,9 @@ public partial class MainWindow : Gtk.Window
382 389
         {
383 390
             if (cmdManager.CameraClose() != DestijlCommandManager.CommandStatus.Success)
384 391
             {
385
-                MessagePopup(MessageType.Error,
386
-                             ButtonsType.Ok, "Error",
387
-                             "Error when closing camera: bad answer for supervisor or timeout");
392
+                //MessagePopup(MessageType.Error,
393
+                //             ButtonsType.Ok, "Error",
394
+                //             "Error when closing camera: bad answer for supervisor or timeout");
388 395
             }
389 396
         }
390 397
         else

+ 1
- 1
software/raspberry/superviseur-robot/lib/src/image.cpp View File

@@ -44,7 +44,7 @@ int open_camera(Camera  *camera)
44 44
     // open the default camera, use something different from 0 otherwise;
45 45
     // Check VideoCapture documentation.
46 46
     printf("Opening Camera...\n");
47
-    if(!cap.open(0))
47
+    if(!cap.open(1))
48 48
         return -1;
49 49
     
50 50
     sleep(1);

+ 2
- 0
software/raspberry/superviseur-robot/lib/src/server.cpp View File

@@ -52,6 +52,8 @@ int closeServer() {
52 52
     close(socketFD);
53 53
 
54 54
     socketFD = -1;
55
+    
56
+    return 0;
55 57
 }
56 58
 
57 59
 int acceptClient() {

+ 16
- 2
software/raspberry/testeur/testeur/main.cpp View File

@@ -114,6 +114,20 @@ int sendAnswer(string cmd, string data) {
114 114
     return status;
115 115
 }
116 116
 
117
+int sendBinaryData(string cmd, unsigned char* data) {
118
+    int status = 0;
119
+    string msg;
120
+
121
+//TODO: Faire une version binaire de sendDataToServer
122
+    //TODO: Gerer la concatenation du header et des données binaires
123
+    msg = cmd + ':' + data;
124
+    cout << "Answer: " + msg;
125
+    cout << "\n";
126
+    sendDataToServer((char*) msg.c_str(), msg.length());
127
+
128
+    return status;
129
+}
130
+
117 131
 int decodeMessage(MessageFromMon *mes, int dataLength) {
118 132
     int status = 0;
119 133
     string header(mes->header, 4);
@@ -169,12 +183,12 @@ int main(int argc, char** argv) {
169 183
     namedWindow("Sortie Camera");
170 184
 
171 185
     // Ouverture de la com robot
172
-    if (open_communication_robot("/dev/ttyUSB0") != 0) {
186
+    /*if (open_communication_robot("/dev/ttyUSB0") != 0) {
173 187
         cerr << "Unable to open /dev/ttyUSB0: abort\n";
174 188
         return -1;
175 189
     }
176 190
     cout << "/dev/ttyUSB0 opened\n";
177
-
191
+*/
178 192
     // Ouverture de la camera
179 193
     if (open_camera(0) == -1) {
180 194
         cerr << "Unable to open camera: abort\n";

+ 5
- 5
software/raspberry/testeur/testeur/nbproject/Makefile-Debug.mk View File

@@ -69,27 +69,27 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/testeur: ${OBJECTFILES}
69 69
 ${OBJECTDIR}/_ext/e4d40e25/image.o: ../../superviseur-robot/lib/src/image.cpp
70 70
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
71 71
 	${RM} "$@.d"
72
-	$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/image.o ../../superviseur-robot/lib/src/image.cpp
72
+	$(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/image.o ../../superviseur-robot/lib/src/image.cpp
73 73
 
74 74
 ${OBJECTDIR}/_ext/e4d40e25/message.o: ../../superviseur-robot/lib/src/message.cpp
75 75
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
76 76
 	${RM} "$@.d"
77
-	$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/message.o ../../superviseur-robot/lib/src/message.cpp
77
+	$(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/message.o ../../superviseur-robot/lib/src/message.cpp
78 78
 
79 79
 ${OBJECTDIR}/_ext/e4d40e25/robot.o: ../../superviseur-robot/lib/src/robot.cpp
80 80
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
81 81
 	${RM} "$@.d"
82
-	$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/robot.o ../../superviseur-robot/lib/src/robot.cpp
82
+	$(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/robot.o ../../superviseur-robot/lib/src/robot.cpp
83 83
 
84 84
 ${OBJECTDIR}/_ext/e4d40e25/server.o: ../../superviseur-robot/lib/src/server.cpp
85 85
 	${MKDIR} -p ${OBJECTDIR}/_ext/e4d40e25
86 86
 	${RM} "$@.d"
87
-	$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/server.o ../../superviseur-robot/lib/src/server.cpp
87
+	$(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/_ext/e4d40e25/server.o ../../superviseur-robot/lib/src/server.cpp
88 88
 
89 89
 ${OBJECTDIR}/main.o: main.cpp
90 90
 	${MKDIR} -p ${OBJECTDIR}
91 91
 	${RM} "$@.d"
92
-	$(COMPILE.cc) -g -D__FOR_PC__ -DD_REENTRANT -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp
92
+	$(COMPILE.cc) -g -DD_REENTRANT -D__FOR_PC__ -I../../superviseur-robot/lib `pkg-config --cflags opencv`   -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/main.o main.cpp
93 93
 
94 94
 # Subprojects
95 95
 .build-subprojects:

+ 8
- 0
software/raspberry/testeur/testeur/nbproject/private/private.xml View File

@@ -4,4 +4,12 @@
4 4
         <activeConfTypeElem>1</activeConfTypeElem>
5 5
         <activeConfIndexElem>0</activeConfIndexElem>
6 6
     </data>
7
+    <editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
8
+    <open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
9
+        <group>
10
+            <file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/server.cpp</file>
11
+            <file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/superviseur-robot/lib/src/image.cpp</file>
12
+            <file>file:/home/dimercur/Documents/Travail/dumber/software/raspberry/testeur/testeur/main.cpp</file>
13
+        </group>
14
+    </open-files>
7 15
 </project-private>

Loading…
Cancel
Save