Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dced44d

Browse files
committed
Interfaz gráfica y sockets generados dinamicamente que atienden las peticiones de datos de diferentes clientes y realizan la captura de datos, aunque no se ha confirmado que dicha captura sea consistente
1 parent 11fa3c9 commit dced44d

File tree

2 files changed

+89
-65
lines changed

2 files changed

+89
-65
lines changed

ControlThread.java

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,44 @@
11
import java.io.DataInputStream;
22
import java.io.DataOutputStream;
3-
import java.io.File;
43
import java.io.IOException;
5-
import java.net.ServerSocket;
64
import java.net.Socket;
75

86

97
public class ControlThread extends Thread
108
{
11-
private int usbPort;
12-
private double tsT;
13-
private double setpointT;
14-
private double kpT;
15-
private double kiT;
16-
private double kdT;
17-
private double tsF;
18-
private double setpointF;
19-
private double kpF;
20-
private double kiF;
21-
private double kdF;
229
private int finish = 0;
10+
private double setpoint;
2311
private Window gui;
24-
private ServerSocket server;
2512
private Socket socket;
2613
private DataOutputStream out;
2714
private DataInputStream in;
2815
private volatile boolean alive = true;
2916

30-
public ControlThread(Window _gui)
31-
{
17+
public ControlThread(Window _gui, Socket _socket, double _setpoint)
18+
{
19+
this.socket = _socket;
20+
this.setpoint = _setpoint;
3221
this.gui = _gui;
33-
this.alive = true;
34-
// Create server
35-
System.out.println("Starting Loggin thread");
3622
try
3723
{
38-
this.server = new ServerSocket(54067);
39-
this.socket = server.accept();
24+
this.out = new DataOutputStream(this.socket.getOutputStream());
25+
this.in = new DataInputStream(this.socket.getInputStream());
26+
out.flush();
4027
}
4128
catch (IOException e)
4229
{
43-
System.out.println("problema creando el servidor Java");
30+
System.out.println("Error obteniendo los streams de un cliente controlador");
4431
e.printStackTrace();
4532
return;
4633
}
47-
finally
48-
{
49-
System.out.println("Servidor creado con éxito");
50-
try
51-
{
52-
this.out = new DataOutputStream(this.socket.getOutputStream());
53-
this.in = new DataInputStream(this.socket.getInputStream());
54-
out.flush();
55-
}
56-
catch (IOException e)
57-
{
58-
System.out.println("Error obteniendo los streams");
59-
e.printStackTrace();
60-
return;
61-
}
62-
}
63-
// get user parameters
64-
this.usbPort = Integer.parseInt(this.gui.txt_port.getText());
65-
this.tsT = Double.parseDouble(this.gui.txt_sample_time_I.getText());
66-
this.setpointT = Double.parseDouble(this.gui.txt_setpoint_I.getText());
67-
this.kpT = Double.parseDouble(this.gui.txt_kp_T.getText());
68-
this.kiT = Double.parseDouble(this.gui.txt_ki_T.getText());
69-
this.kdT = Double.parseDouble(this.gui.txt_kd_T.getText());
70-
this.tsF = Double.parseDouble(this.gui.txt_sample_time_II.getText());
71-
this.setpointF = Double.parseDouble(this.gui.txt_setpoint_II.getText());
72-
this.kpF = Double.parseDouble(this.gui.txt_kp_F.getText());
73-
this.kiF = Double.parseDouble(this.gui.txt_ki_F.getText());
74-
this.kdF = Double.parseDouble(this.gui.txt_kd_F.getText());
34+
System.out.println("Hilo controlador creado");
7535
}
76-
36+
7737
public void run()
7838
{
7939
double output_process_val = 0;
8040
double input_process_val = 0;
8141
double time_value = 0;
82-
8342
while(this.alive)
8443
{
8544
try
@@ -95,7 +54,35 @@ public void run()
9554
}
9655
this.gui.output_I.add(time_value, output_process_val);
9756
this.gui.input_I.add(time_value, input_process_val);
98-
this.gui.setpoint_I.add(time_value, this.setpointT);
57+
this.gui.setpoint_I.add(time_value, this.setpoint);
9958
}
59+
try
60+
{
61+
System.out.println("Esperando para cerrar los sockets");
62+
Thread.sleep(2000);
63+
} catch (InterruptedException e1)
64+
{
65+
System.out.println("Error esperando para cerrar los sockets");
66+
e1.printStackTrace();
67+
}
68+
try
69+
{
70+
this.out.close();
71+
this.in.close();
72+
this.socket.close();
73+
} catch (IOException e)
74+
{
75+
System.out.println("No se pudo cerrar el socket al dejar el hilo");
76+
e.printStackTrace();
77+
}
78+
System.out.println("Hilo controlador terminado");
10079
}
80+
81+
82+
public void terminate()
83+
{
84+
this.alive = false;
85+
this.finish = 1;
86+
}
87+
10188
}

ThreadServer.java

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class ThreadServer extends Thread
1010
{
1111
private int usbPort;
12+
private final int SERVER_PORT = 34869;
1213
private double tsT;
1314
private double setpointT;
1415
private double kpT;
@@ -21,13 +22,15 @@ public class ThreadServer extends Thread
2122
private double kdF;
2223
private int server_answer = 2958;
2324
private int valid_client_request = 45862;
24-
private int finish = 0;
2525
private Window gui;
2626
private ServerSocket server;
2727
private Socket socket;
2828
private DataOutputStream out;
2929
private DataInputStream in;
30-
private volatile boolean alive = true;
30+
private ControlThread threadTemp;
31+
private ControlThread threadFlow;
32+
33+
3134

3235
public ThreadServer(Window _gui)
3336
{
@@ -39,7 +42,7 @@ public ThreadServer(Window _gui)
3942
System.out.println("Starting Loggin thread");
4043
try
4144
{
42-
this.server = new ServerSocket(2222);
45+
this.server = new ServerSocket(this.SERVER_PORT);
4346
this.socket = server.accept();
4447
}
4548
catch (IOException e)
@@ -108,9 +111,8 @@ public ThreadServer(Window _gui)
108111
public void terminate()
109112
{
110113
// llamar al terminate de cada hilo controlador y venir a capturar sus datos
111-
112-
this.alive = false;
113-
this.finish = 1;
114+
threadTemp.terminate();
115+
threadFlow.terminate();
114116
System.out.format("La cantidad de elementos es: %d\n", this.gui.output_I.getItemCount());
115117

116118
// option to save the captured data
@@ -137,13 +139,11 @@ public void terminate()
137139

138140
}
139141

142+
143+
144+
140145
public void run()
141146
{
142-
ControlThread control_1 = new ControlThread(this.gui);
143-
double output_process_val = 0.0;
144-
double input_process_val = 0.0; //PID computed values
145-
double time_value = 0.0;
146-
147147
// get user parameters
148148
this.usbPort = Integer.parseInt(this.gui.txt_port.getText());
149149
this.tsT = Double.parseDouble(this.gui.txt_sample_time_I.getText());
@@ -180,7 +180,44 @@ public void run()
180180
}
181181

182182
// crear un par de hilos por cada controlador
183+
// Control temperatura
184+
try
185+
{
186+
this.socket = server.accept();
187+
threadTemp = new ControlThread(this.gui, this.socket, this.setpointT);
188+
threadTemp.start();
189+
}
190+
catch(Exception e)
191+
{
192+
e.printStackTrace();
193+
System.out.println("Connection Error: creating the temp thread");
194+
}
195+
196+
// Control temperatura
197+
try
198+
{
199+
this.socket = server.accept();
200+
threadFlow = new ControlThread(this.gui, this.socket, this.setpointF);
201+
threadFlow.start();
202+
}
203+
catch(Exception e)
204+
{
205+
e.printStackTrace();
206+
System.out.println("Connection Error: creating the flow thread");
207+
}
208+
209+
210+
//Esperando a que los hilos terminen
211+
try
212+
{
213+
threadTemp.join();
214+
threadFlow.join();
215+
} catch (InterruptedException e2)
216+
{
217+
e2.printStackTrace();
218+
}
183219

220+
// cerrando el socket principal, los demás sockets se deben cerrar dentro de los respectivos hilos
184221
try
185222
{
186223
System.out.println("Esperando para cerrar los sockets");
@@ -201,6 +238,6 @@ public void run()
201238
System.out.println("No se pudo cerrar el socket al dejar el hilo");
202239
e.printStackTrace();
203240
}
204-
System.out.println("Socket cerrado exitosamente & hilo terminado");
241+
System.out.println("Socket principal cerrado exitosamente & hilo terminado");
205242
}
206243
}

0 commit comments

Comments
 (0)