lecture/ecriture sur le port
Bonjour,
J'ai quelques problèmes avec l'écriture sur le port com en java.
Je sais qu'il y a eu des discussions la dessus et je sais aussi que christophJ a déposé un totoriaux. Mais malgrés cela, je ne comprends pas ou ce trouve mon erreur.
Voici mon code:
Code:
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
| // Specification:
package ch.hearc.bilat.polhemus.evenementiel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import com.sun.comm.Win32Driver;
public class com_1 extends Thread implements SerialPortEventListener
{
/*------------------------------------------------------------------*\
|* Constructeurs *|
\*-----------------------------------------------------------------*/
public com_1(String port)
{
// initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
try
{
portId = CommPortIdentifier.getPortIdentifier(port);
serialPort = (SerialPort)portId.open("ecrit", 2000);
out = serialPort.getOutputStream();
fluxLecture = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (NoSuchPortException e)
{
e.printStackTrace();
}
catch (PortInUseException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (TooManyListenersException e)
{
}
catch (UnsupportedCommOperationException e)
{
e.printStackTrace();
}
System.out.println("port ouvert, attente de lecture");
ecriture("P");
System.out.println("La commande p est envoyé!");
}
/*------------------------------------------------------------------*\
|* Méthodes Public *|
\*-----------------------------------------------------------------*/
public void run()
{
running = true;
while(running)
{
attendre(2000);
}
// fermeture du flux et port
try
{
fluxLecture.close();
}
catch (IOException e)
{
e.printStackTrace();
}
serialPort.close();
}
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:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
String trame = new String();
try
{
// lecture du buffer et affichage
trame = (String)fluxLecture.readLine();
System.out.println("La commande recue est: " + trame);
}
catch (IOException e)
{
e.printStackTrace();
}
break;
}
}
public void stopThread()
{
running = false;
}
public void ecriture(String commande)
{
try
{
out.write(commande.getBytes());
}
catch (IOException e)
{
e.printStackTrace();
}
}
/*------------------------------------------------------------------*\
|* Méthodes Principal *|
\*-----------------------------------------------------------------*/
public static void main(String[] args)
{
String port = new String("COM1");
com_1 portCom1 = new com_1(port);
portCom1.start();
System.out.println("taper q pour quitter");
BufferedReader clavier = new BufferedReader(new InputStreamReader(System.in));
try
{
String lu = clavier.readLine();
while(!lu.equals("q"))
{
}
}
catch (IOException e)
{
}
portCom1.stopThread();
}
/*------------------------------------------------------------------*\
|* Méthodes Private *|
\*-----------------------------------------------------------------*/
private static void attendre(int temps)
{
try
{
Thread.sleep(temps);
}
catch (InterruptedException e)
{
}
}
/*------------------------------------------------------------------*\
|* Attributs Private *|
\*-----------------------------------------------------------------*/
private CommPortIdentifier portId;
private SerialPort serialPort;
private BufferedReader fluxLecture;
private boolean running;
private OutputStream out;
} |
Le problème est quand j'envoi ma donnée... C'est à dire la commande p!
Svpl aidez moi.
Bonne fin de journée