1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| import javax.comm.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import com.dalsemi.tininet.*;
import com.dalsemi.system.*;
class SerialToEthernet extends Thread {
// Utilisation d'un buffer de 1024 pour le transfert de données
private static final int INPUT_BUF_LEN = 1024;
// Serial port and associated streams
private SerialPort sp;
private InputStream spin;
private OutputStream spout;
// Socket and associated streams
private Socket s;
private InputStream sin;
private OutputStream sout;
// commande à taper pour éxecuter le programme : java SerialToEthernet.tini 192.168.100.3 le port est déjà passé en argument dans le programeme
private SerialToEthernet(String server)
throws Exception {
// Creation et initialisation du port serie
sp = (SerialPort)
// Ouverture du port serie
CommPortIdentifier.getPortIdentifier("serial0").open(
"SerialToEthernet", 5000);
// RTS
TINIOS.setRTSCTSFlowControlEnable(0, false);
// 8 data bits, 1 stop bit, pas de parité
sp.setSerialPortParams(115200, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// Controle de flux
sp.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
SerialPort.FLOWCONTROL_RTSCTS_OUT);
// Initialisation des flux d'entrées et de sorties'
spin = sp.getInputStream();
spin = sp.getInputStream();
spout = sp.getOutputStream();
// Timeout de lecture de 100ms
sp.enableReceiveTimeout(100);
// Parametrage du seuil de reception et du buffer
sp.enableReceiveThreshold(INPUT_BUF_LEN);
// Connection au serveur TCP
s = new Socket(server, 4000);
sin = s.getInputStream();
sout = s.getOutputStream();
// Création et lancement d'un thread Serial To Ethernet'
(new Thread(new SerialReader(this, INPUT_BUF_LEN))).start();
// Lancement du thread de maintenance
super.start();
}
//l'utilisation de l'élément volatile s'explique ici par le fait, que la variable running peut être accessible par tout les threads.
private volatile boolean running = true;
private int serialTotal = 0;
private int networkTotal = 0;
public void run() {
while (running) {
try {
Thread.sleep(1000);
} catch (InterruptedException ie) {}
//System.out.println("Bytes received from serial:" + serialTotal);
//System.out.println("Bytes received from network:" + networkTotal);
}
}
private class SerialReader implements Runnable {
private byte[] serBuf;
private Thread maint;
private SerialReader(Thread maint, int size) {
serBuf = new byte[size];
this.maint = maint;
}
public void run() {
while (running) {
try {
System.out.println("Lecture des datas sur le port série");
int count = spin.read(serBuf, 0, serBuf.length);
//S'il existe encore des données à lire sur le port alors on continue la lecture'
if (count > 0) {
System.out.println("Ecriture des datas sur Ethernet");
sout.write(serBuf, 0, count);
serialTotal += count;
}
Thread.sleep(1000);
} catch (IOException ioe) {
// Problème de connection avec le serveur
System.out.println(ioe.getMessage());
ioe.printStackTrace();
running = false;
maint.interrupt();
break;
}
}
}
}
} |
Partager