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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| /**
* @var poids
* Le poids reçu de la balance
*/
private String poidsRecu = null;
/**
* @var poids
* Le poids reçu de la balance
*/
private float poids = 0.0f;
/**
* @var poidsPrecedent
* Le poids reçu de la balance juste avant le dernier reçu
*/
private float poidsPrecedent = 0.0f;
/**
* @var poidsFinal
* Le poids final après test de stabilité reçu de la balance
*/
public float poidsFinal = 0.0f;
/**
* @var threadPoids
* Le thread qui va communiquer avec la balance
*/
private Thread threadPoids;
/**
* @var portId
* l' Identifiant de notre port série
*/
private CommPortIdentifier portId;
/**
* @var reader
* le buffer qui va stocké les flux reçu de la balance
*/
private BufferedReader reader;
/**
* @var outStream
* Flux envoyer par la balance
*/
private OutputStream outStream;
/**
* Identificateur du port à utiliser pour la connexion
* @var o_port2
*/
private CommPortIdentifier o_port2;
/**
* Objet port identifié. Pour le traitement des flux I/O
* @var o_portId
*/
private SerialPort o_portId = null;
/**
* Constructeur de l' applet
* Initialise les variables de classe ainsi que l' IHM
* @param aucun
* @return void
*/
public void start()
{
///////////////////////////////////////////////////////////
// INITIALISATION DU THREAD
//////////////////////////////////////////////////////////
if (threadPoids == null) {
threadPoids = new Thread(this, "Poids");
threadPoids.start();
}
///////////////////////////////////////////////////////////
// INITIALISATION DU DRIVER Win32
//////////////////////////////////////////////////////////
Win32Driver driver = new Win32Driver();
try
{
driver.initialize();
}
catch (NullPointerException e) {/*System.out.println("coucoucoucoucouc\n");*/}
try
{
o_port2 = CommPortIdentifier.getPortIdentifier("COM1");
o_portId = (SerialPort) o_port2.open("test", 1000);
System.out.println("ok1");
o_portId.setSerialPortParams(1200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
System.out.println("ok2");
outStream = o_portId.getOutputStream();
reader = new BufferedReader(new InputStreamReader(o_portId.getInputStream()));
outStream = o_portId.getOutputStream();
reader = new BufferedReader(new InputStreamReader(o_portId.getInputStream()));
}
catch(NoSuchPortException e)
{
System.out.println("coucoucoucoucouc\n");
System.out.println("\nmessage: "+e.getMessage()+"\nCause: " +e.getCause());
}
catch (PortInUseException e)
{
// TODO Auto-generated catch block
System.out.println("coucoucoucoucouc\n");
System.out.println("\nmessage: "+e.getMessage()+"\nCause: " +e.getCause());
}
catch (UnsupportedCommOperationException e)
{
System.out.println("coucoucoucoucouc\n");
System.out.println("\nmessage: "+e.getMessage()+"\nCause: " +e.getCause());
}
catch (IOException e)
{
System.out.println("coucoucoucoucouc\n");
System.out.println("\nmessage: "+e.getMessage()+"\nCause: " +e.getCause());
}
}
/**
* Méthode qui lance notre thread
* @return void
*/
public void run() {
///////////////////////////////////////////////////////////
// INITIALISATION DU PORT
//////////////////////////////////////////////////////////
JOptionPane j = new JOptionPane();
j.showMessageDialog(null, "poidsRecu: "+poidsRecu, "test", JOptionPane.INFORMATION_MESSAGE);
while (threadPoids != null) {
try {
threadPoids.sleep(1000);
poidsRecu = reader.readLine();
if (poidsRecu.contains("+") || poidsRecu.contains("-"))
{
poidsRecu = poidsRecu.replaceAll("[^0-9.-]", "");
poids = Float.parseFloat(poidsRecu);
if(isStable(poids, poidsPrecedent))
{
poidsFinal = poids;
}
}
} catch (InterruptedException e){
System.out.println("coucoucoucoucouc\n");
}
catch (IOException e) {
System.out.println("coucoucoucoucouc\n");
// TODO Auto-generated catch block
//System.out.println("\nmessage: "+e.getMessage()+"\nCause: " +e.getCause());
}
repaint();
}
}
/**
* méthode qui vérifie la stabilité du poids reçu par la balance
* @return booléan
* @param poid1 float
* @param poid2 float
*/
private boolean isStable(float poid1, float poid2)
{
float poidTemoin = poid1 - poid2;
boolean bOk = false;
if (poidTemoin < 1 || poidTemoin > -1)
{
bOk = true;
}
return bOk;
}
public float retourne_poids()
{
return poidsFinal;
}
public void paint(Graphics g) {
String label = new String("Poids reçu: "+poids+"\n");
label += " poids stabilisé :" + poidsFinal;
g.drawString(label, 5, 20);
}
/**
* Méthode qui arrête notre thread
* @return void
*/
public void stop() {
threadPoids.stop();
threadPoids = null;
} |
Partager