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
| import java.io.*;
public class COM implements Runnable{
static int Vlu = 0;/**Tension d'entrée en Volt*/
static int R = 1800;/**Résistance du pont diviseur en amont de la thermistance */
static java.io.RandomAccessFile port;
static Thread myThread=null;
static String q = null;
public COM(){
myThread = new Thread(this);/**initialisation*/
}
public void start(){
try {
port=new java.io.RandomAccessFile("COM3","rwd");/**Choix du port et ouverture du fichier en mode écriture/lectue aléatoire*/
port.writeBytes("\r\n");/**écrit sur le port*/
port.writeBytes("c,1,0,0,1\r\n");
port.writeBytes("T,1000,1\r\n");
}
catch (Exception e) {
System.out.println("start "+e.toString());
}
myThread.start();/**lancement du thread*/
}
public void run() {
System.out.println("lecture COM...");
for(;;){
String st = null;
try {
st=port.readLine();
q = st.substring(2,st.length());/**découpage du string d'entrée*/
Convtemp();/**méthode conversion de la température */
} catch (IOException e) {System.out.println(e.getMessage());}
System.out.println(st);
}
}
public void Convtemp(){
try {
Vlu = Integer.parseInt(q);/**Cast du string en int */
/*************Calcul de la température ambiante *******/
/**/ float Vacq = (float)(Vlu*5)/1024; /**/
/**/ float Rth = (Vacq*R)/(5-Vacq); /**/
/**/ double temp = -25.40*Math.log(Rth)+ 199.04;/**/
/****************************************************/
System.out.println("vacq ="+Vacq);
System.out.println("Rth ="+Rth);
System.out.println("T° ="+temp);
} catch (Exception e) {System.out.println("temp conv:"+e.getMessage());}
}
public static void main(String[] args) {
COM UBW= new COM();
UBW.start();
}
} |