probleme avec la methode readline
j'ai écris le programme suivant pour communiquer avec le port serie d'un pc
Mais quand je l'execute, le programme se bloque.
Il ya aussi un Warning sur la ligne :rep= dis.realine();
je vous donne le code ; Aider moi !
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
| import javax.comm.*;
import com.sun.comm.Win32Driver;
import java.io.*;
import java.util.*;
public class Port
{
public CommPortIdentifier cpi;
public CommPort cp;
static InputStream is;
static OutputStream os;
static DataOutputStream dos;
static DataInputStream dis;
static PrintStream ps;
static String portname;
static SerialPort sp;
public Port(String pn)
{
portname=pn;
}
public void open() throws IOException, NoSuchPortException, PortInUseException,
UnsupportedCommOperationException
{
try
{
//initialisation du driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
cpi=CommPortIdentifier.getPortIdentifier(portname);
System.out.println("Port"+portname );
cp=cpi.open("sms",2000);
sp=(SerialPort)cp;
System.out.println("Port ouvert ");
sp.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
os=cp.getOutputStream();
ps =new PrintStream(os);
is =cp.getInputStream();
dis=new DataInputStream(is);
}
catch(Exception ed)
{
System.out.println("on ne pe pas trouver le port");
}
}
public void close()
{
cp.close();
}
public void send(String commande)
{
ps.print(commande);
ps.print("\r\n");
System.out.println("La commande envoyé est :"+commande);
}
public String reponse()
{
String rep=null;
try
{
rep=dis.readLine();
}
catch(IOException ert)
{
System.out.println("Impossible de lire sur le port");
}
return rep;
}
public static void main (String[] Args) throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException
{
Port po=new Port("COM1");
String r1;
po.open();
po.send("AT+CMEE=1");
r1=po.reponse();
System.out.println(r1);
po.close();
}
} |