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
|
import java.io.*;
import java.util.*;
import javax.comm.*;
public class ModemDialog implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread readThread;
PrintWriter sortie;
BufferedReader bufRead;
BufferedReader entree;
public static void main(String[] args) {
try {
portId = CommPortIdentifier.getPortIdentifier("LPT1");
ModemDialog instance = new ModemDialog();
} catch (NoSuchPortException e) {
e.printStackTrace();
}
}
public ModemDialog() {
System.out.println("Connexion sur le port COM");
try {
serialPort = (SerialPort) portId.open("modem", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
bufRead = new BufferedReader(new InputStreamReader(inputStream));
sortie = new PrintWriter(outputStream, true);
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
String messageString = "AT";
entree = new BufferedReader(new InputStreamReader(System.in));
while(!messageString.equals("stop"))
{
System.out.println("Entrer une commande AT...");
try {
messageString = entree.readLine();
} catch( IOException e ) {
e.printStackTrace();
}
if(messageString.equals("CtrlZ"))
{
//System.out.println("on détecte le car 26"+(char)26);
sortie.println((char)26);
}
else
{
System.out.println("Envoi de "+messageString);
sortie.println(messageString);
}
}
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEvent event) {
//System.out.println("Event de récéption");
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:
//System.out.println("Reception...");
int messageLu = 0;
//String messageLuStr = null;
try {
while (inputStream.available() > 0) {
//messageLuStr = bufRead.readLine().trim();
//System.out.println(messageLuStr);
messageLu = bufRead.read();
System.out.print((char)messageLu);
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}
et voici le message d'erreur. |
Partager