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
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
import com.sun.comm.Win32Driver;
public String getEtatRegistre(){
config=gestionnaire.getConfig();
w32Driver = new Win32Driver();
w32Driver.initialize();
try {
portId = CommPortIdentifier.getPortIdentifier(config.getPortCom());
} catch (NoSuchPortException nspe) {
nspe.printStackTrace();
}
try {
serialPort = (SerialPort) portId.open("FGEPesage", 2000);
} catch (PortInUseException piue) {
piue.printStackTrace();
}
try {
serialPort.setSerialPortParams(
config.getBauds(),
config.getDataBit(),
config.getBitStop(),
config.getParite());
} catch (UnsupportedCommOperationException ucoe) {
ucoe.printStackTrace();
}
try {
fluxLecture =new BufferedReader (new InputStreamReader(serialPort.getInputStream()));
fluxEcriture =new BufferedWriter(new OutputStreamWriter(serialPort.getOutputStream()));
String arret="W<201";
String demande="R1";
String demarrer="W<200";
char[] envoi;
envoi=arret.toCharArray();
for(int cpt=0;cpt<envoi.length;cpt++){
fluxEcriture.write(envoi[cpt]);
fluxEcriture.flush();
Thread.sleep(1000);
}
envoi=demande.toCharArray();
for(int cpt=0;cpt<envoi.length;cpt++){
fluxEcriture.write(envoi[cpt]);
fluxEcriture.flush();
Thread.sleep(1000);
}
etatRegistre=fluxLecture.readLine();
Thread.sleep(1000);
envoi=demarrer.toCharArray();
for(int cpt=0;cpt<envoi.length;cpt++){
fluxEcriture.write(envoi[cpt]);
fluxEcriture.flush();
Thread.sleep(1000);
}
if(etatRegistre.length()==149){
for(int cpt=0;cpt<etatRegistre.length()-7;cpt++){
checksum=checksum+etatRegistre.charAt(cpt);
}
checksum=65535-checksum;
anneemoisjour=etatRegistre.substring(2, 8);
heureminuteseconde=etatRegistre.substring(9, 15);
r0PGA1=etatRegistre.substring(16, 21);
r0PGA2=etatRegistre.substring(22, 27);
r1PGA1=etatRegistre.substring(28, 33);
r1PGA2=etatRegistre.substring(34, 39);
r2PGA1=etatRegistre.substring(40, 45);
r2PGA2=etatRegistre.substring(46, 51);
r3PGA1=etatRegistre.substring(52, 57);
r3PGA2=etatRegistre.substring(58, 63);
r4PGA1=etatRegistre.substring(64, 69);
r4PGA2=etatRegistre.substring(70, 75);
r5PGA1=etatRegistre.substring(76, 81);
r5PGA2=etatRegistre.substring(82, 87);
r6PGA1=etatRegistre.substring(88, 93);
r6PGA2=etatRegistre.substring(94, 99);
}else{
etatRegistre=null;
}
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fluxEcriture.close();
fluxLecture.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
serialPort.close();
}
return etatRegistre; |
Partager