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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
| public class ModeEvenement extends Thread implements SerialPortEventListener {
/* Un 'baud rate' de 921600 correspond à environ 92'160 caractères par seconde.
On prévoit alors un tampon d'entrée assez grand pour pouvoir soutenir 1s
de transmission permanebte(transmission san contrôel de flux)
*/
private static final int SPEED_PORT = 921600;
private static final int INPUT_BUFFER_SIZE = 90 * 1024;
private static final int BUFFER_SIZE = INPUT_BUFFER_SIZE;
private CommPortIdentifier portId;
private SerialPort serialPort;
private OutputStream outStream; //flux d'écriture du port
//private BufferedReader fluxLecture;
private BufferedInputStream fluxLecture;
private byte[] byte_buffer;
private FileWriter mon_fichier = null;
private BufferedWriter tampon = null;
private int r=0;
private int t=0;
private int position=0;
private ParamMesure para_mes;
/**
* Constructeur qui récupère l'identifiant du port, l'objet ParamMesure, et lance l'ouverture.
* En cas d'erreur, on affiche un message et on devrait libérer toutes les
* ressources.
*/
public ModeEvenement(String port, ParamMesure pm) {
para_mes=pm;
byte_buffer=new byte[BUFFER_SIZE];
//initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
//récupération de l'identifiant du port
try {
portId = CommPortIdentifier.getPortIdentifier(port);
} catch (NoSuchPortException e) {
e.printStackTrace(System.err);
return;
}
//ouverture du port
try {
serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
} catch (PortInUseException e) {
e.printStackTrace(System.err);
return;
}
//récupération du flux
try {
fluxLecture = new BufferedInputStream(serialPort.getInputStream(), INPUT_BUFFER_SIZE);
// new BufferedReader(
// new InputStreamReader(serialPort.getInputStream()));
} catch (IOException e) {
e.printStackTrace(System.err);
return;
}
//ajout du listener
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {
e.printStackTrace(System.err);
return;
}
//paramétrage du port
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.setSerialPortParams(
SPEED_PORT,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {
e.printStackTrace(System.err);
return;
}
try {
outStream = serialPort.getOutputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace(System.err);
return;
}
System.out.println("port ouvert");
//ouverture du fichier
try {
mon_fichier = new FileWriter(para_mes.get_folder_path()+
'\\'+para_mes.get_nom_fichier(),true);
tampon = new BufferedWriter(mon_fichier);
System.out.println("fichier cree");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace(System.err);
return;
}
// lancement de l'appli
this.start();
}
public void run() {
//running = true;
// lancement du prog dans FPGA
send_byte((byte)0);
System.out.println("reception lancée");
while (para_mes.get_running()) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
//fermeture du flux et port
try {
fluxLecture.close();
outStream.flush();// a revoir
outStream.close();
tampon.newLine();
tampon.write("FIN MESURES ");
tampon.newLine();
tampon.flush();
tampon.close();
mon_fichier.close();
System.out.println("Fermeture OK. POSITION: "+position);
} catch (IOException e) {
}
serialPort.close();
}
/**
* Méthode de gestion des événements.
*/
public void serialEvent(SerialPortEvent event) {
//gestion des événements sur le port :
//on ne fait rien sauf quand les données sont disponibles
switch (event.getEventType()) {
case SerialPortEvent.BI :System.out.println("event2");
case SerialPortEvent.OE :System.out.println("event2");
case SerialPortEvent.FE :System.out.println("event2");
case SerialPortEvent.PE :System.out.println("event2");
case SerialPortEvent.CD :System.out.println("event2");
case SerialPortEvent.CTS :System.out.println("event2");
case SerialPortEvent.DSR :System.out.println("event2");
case SerialPortEvent.RI :System.out.println("event3");
case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
break;
case SerialPortEvent.DATA_AVAILABLE :
try {
System.out.println("event " + fluxLecture.available());
int read; = fluxLecture.read(byte_buffer,0,BUFFER_SIZE);
while ((read = fluxLecture.read(byte_buffer,0,BUFFER_SIZE) != -1) {
for (int i=0; i<read; ++i) {
//System.out.println(byte_buffer[i]);
//mesure = byte_buffer[i];
/* On évite de travailler avec des strings */
tampon.write(byte_buffer[i]);
tampon.write(' ');
position++;
}
}
} catch (IOException e) {
System.out.println("pb dans ecriture");
}
break;
}
} |
Partager