Il seguente programma Java permette la spedizione dati attraverso
la PC-Card:
import java.awt.*;
import java.awt.event.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import java.net.*;
import java.io.*;
public class PLC2
{
Seriale3 frame;
SeparateSubTask t1;
JPanel pane;
JButton b1 = new JButton("Avvia lo SCADA");
JButton b2 = new JButton("Fine");
JTextArea jta = new JTextArea(10,50);
int PORT=4000,dati=0;
long conta;
boolean ok=false;
public void attiva()
{
frame = new Seriale3();
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.crea();
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
}
public class SeparateSubTask extends Thread
{
ServerSocket s;
Socket socket2,socket;
InetAddress addr,addr2;
int c;
public SeparateSubTask()
{
try
{
addr2=InetAddress.getByName("10.10.10.128");
addr=InetAddress.getByName("10.10.10.129");
s = new ServerSocket(PORT,0,addr);
System.out.println("Inizio : "+s);
frame.stampa("SCADA attivo : "+s);
}
catch (IOException e)
{
System.err.println("Errore : impossibile creare Socket! "+e);
System.exit(1);
}
} // Fine del metodo Costruttore
public void run()
{
try
{
socket = s.accept();
socket2 = new Socket(addr2,PORT);
try
{
System.out.println("Connessione accettata "+socket);
frame.stampa("Connessione accettata : "+socket);
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
BufferedReader in = new BufferedReader(isr);
OutputStreamWriter osw = new OutputStreamWriter(socket2.getOutputStream());
PrintWriter out = new PrintWriter(new BufferedWriter(osw),true);
out.println("*");
System.out.println("Sto chiedendo i dati al PLC");
dati = in.read();
System.out.println("Dati dal PLC : "+dati); c=0;
while (!ok)
{
out.println("1"); // Test del PLC
dati=in.read();
if (dati==111)
{
c++;
if (c==25)
{
out.println("*");
dati=in.read();
System.out.println("Dati dal PLC : "+dati);
c=0;
}
}
if (dati=101)
{
out.println("*");
dati=in.read();
System.out.println("Dati dal PLC : "+dati);
}
}
}
catch (IOException e)
{
System.err.println("Errore : Problemi con lo stream "+e);
System.exit(1);
}
finally {
System.out.println("Chiusura della comunicazione");
socket.close();
socket2.close();
};
return;
}
catch (IOException e)
{
System.out.println("Errore : Connessione rifiutata "+e);
System.exit(1);
}
finally
{
System.out.println("Chiusura dello SCADA");
try {s.close();}
catch (IOException e) { System.out.println("Errore : Server non attivo "+e);System.exit(1); }
}
}
} // Fine del metodo Run
public class Seriale3 extends JFrame
implements ActionListener
{
public void crea()
{
setTitle("Barra del titolo");
b1.addActionListener(this);
b2.addActionListener(this);
pane = new JPanel();
BorderLayout p1 = new BorderLayout();
pane.setLayout(p1);
pane.add("North",b1);
pane.add("Center",jta);
pane.add("South",b2);
setContentPane(pane);
}
public void stampa(String str1)
{
str1 = str1 + "\n";
jta.append(str1);
repaint();
}
public void avvia()
{
// Qui si crea una nuova istanza del Thread MS-DOS
t1 = new SeparateSubTask();
t1.start();
}
public void actionPerformed(ActionEvent e)
{
// Gestore degli eventi bottone
Object source = e.getSource();
if (source == b1) {
this.avvia();
}
if (source == b2) {
System.exit(0);
}
repaint();
}
}
public static void main(String arg[])
{
PLC2 p1 = new PLC2();
p1.attiva();
}
}
