1
1
import java .io .DataInputStream ;
2
2
import java .io .DataOutputStream ;
3
- import java .io .File ;
4
3
import java .io .IOException ;
5
- import java .net .ServerSocket ;
6
4
import java .net .Socket ;
7
5
8
6
9
7
public class ControlThread extends Thread
10
8
{
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 ;
22
9
private int finish = 0 ;
10
+ private double setpoint ;
23
11
private Window gui ;
24
- private ServerSocket server ;
25
12
private Socket socket ;
26
13
private DataOutputStream out ;
27
14
private DataInputStream in ;
28
15
private volatile boolean alive = true ;
29
16
30
- public ControlThread (Window _gui )
31
- {
17
+ public ControlThread (Window _gui , Socket _socket , double _setpoint )
18
+ {
19
+ this .socket = _socket ;
20
+ this .setpoint = _setpoint ;
32
21
this .gui = _gui ;
33
- this .alive = true ;
34
- // Create server
35
- System .out .println ("Starting Loggin thread" );
36
22
try
37
23
{
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 ();
40
27
}
41
28
catch (IOException e )
42
29
{
43
- System .out .println ("problema creando el servidor Java " );
30
+ System .out .println ("Error obteniendo los streams de un cliente controlador " );
44
31
e .printStackTrace ();
45
32
return ;
46
33
}
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" );
75
35
}
76
-
36
+
77
37
public void run ()
78
38
{
79
39
double output_process_val = 0 ;
80
40
double input_process_val = 0 ;
81
41
double time_value = 0 ;
82
-
83
42
while (this .alive )
84
43
{
85
44
try
@@ -95,7 +54,35 @@ public void run()
95
54
}
96
55
this .gui .output_I .add (time_value , output_process_val );
97
56
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 );
99
58
}
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" );
100
79
}
80
+
81
+
82
+ public void terminate ()
83
+ {
84
+ this .alive = false ;
85
+ this .finish = 1 ;
86
+ }
87
+
101
88
}
0 commit comments