Ajouter un commentaire

testeurforme

bon ...

J'ai modifié mon programme afin de lire la reponse du modem a chaque fois que je lui envoie une commande....
Lorsque "j'ecoute" une hypothetique reponse, mon programme ne rend pas la main et attend "une reaction" de la part du modem. Si j'arret mon programme le mmodem est de nouveau dispo
Par contre l'envoie d'une commande au modem ne semble pas du tout fonctionner et meme fait plus ou moins planter le modem (obliger de l'eteindre pour que la commande 'interroger le modem' dans windows me donne une reponse.
Plusieurs questions restent donc en suspends :

1) Pourquoi la lecture [via inputStream = serialPort.getInputStream() ]est elle "bloquante" et pas l'ecriture [outputStream = serialPort.getOutputStream() ]

2) Faut il envoyer commencer le pilotage en enoyant une commande particuliere au modem ?

3) dois je faire un thread qui ecoute en permanence une reponse du modem ou puis je faire des sequences du style "envoie de commande puis lecture de la reponse"

4)comment savoir si ma commande est bien passée etant donné que lorsque le programme s'arrete, je dois eteindre rallumé le modem (lorsque j'ai passé une commande)

5) Dans l'api javacomm20-win32 il y a une classe de lecture et une classe d'ecriture (je me suis basé dessus pour ecrire mon simulacre de code lol)
J'aimerai bien tester la classe lecture mais comment faire etant donné que si je la lance, je ne peut pas utiliser un autre prog qui ecrit (genre hyperterminal de windows) etant donné que le port est deja ouvert (par la classe de lecture)

je remets ma classe que j'ai modifié en ajoutant la lecture d'une reponse
ainsi que les deux classes fournis dans l'aPI

ma classe qui ne fonctionne pas :

/**
* Created by IntelliJ IDEA.
* User: JSC
* Date: 4 déc. 2006
* Time: 13:51:07
* To change this template use File | Settings | File Templates.
* http://christophej.developpez.com/tutoriel/java/javacomm/
**/

import com.sun.comm.Win32Driver;

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
public Enumeration portList;
public CommPortIdentifier portId;
public SerialPort serialPort;
public OutputStream outputStream;
public InputStream inputStream;
private int delaiObtentionPort; //milliseconde
private int bauds;

public SimpleWrite() {
portList = CommPortIdentifier.getPortIdentifiers();
}

public void setDelaiObtentionPort(int delai) {
delaiObtentionPort = delai;
}

public void setBaud(int newBaud) {
bauds = newBaud;
}

public void AffichageListPort() {
CommPortIdentifier portCourant = null;
System.out.println("Affichage de la liste des ports ");
boolean fin = false;
while (!fin) {
try {
if (portList.hasMoreElements()) {
portCourant = (CommPortIdentifier) portList.nextElement();
System.out.println(portCourant.getName());
}
else {
fin = true;
}
}
catch (NoSuchElementException ex) {
System.out.println("Exception plus de port ");
}
}
}

public void initWin32Driver() {
System.out.println("initialisation du driver ");
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
}

public void libereSerialPort() {
serialPort.close();
}

public void paramSerialPort() {
try {
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.setSerialPortParams(bauds, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException ex) {
System.out.println("Erreur Paramatrage du port '" + serialPort.getName() + "' " + ex.toString());
ex.printStackTrace();
}

serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnCarrierDetect(true);
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnBreakInterrupt(true);
serialPort.notifyOnCTS(true);
serialPort.notifyOnDSR(true);
serialPort.notifyOnFramingError(true);
serialPort.notifyOnOutputEmpty(true);
serialPort.notifyOnOverrunError(true);
serialPort.notifyOnParityError(true);
serialPort.notifyOnRingIndicator(true);

}

public void lanceCommande() {
String m0 = "AT,\n";
String m1 = "ATE1";
String m2 = "ATI";
String m3 = "AT&Z0=T12345678";
String m4 = "ATD0140127674\n";
String m5 = "ATDS=0";
int numBytes;

try {
outputStream = serialPort.getOutputStream();
System.out.println("Recuperation du outPutStream : OK ");
}
catch (IOException e) {
System.out.println("Erreur recuperation outPutStream " + e.toString());
e.printStackTrace();
}
try {
inputStream = serialPort.getInputStream();
System.out.println("InputStream créé");
}
catch (IOException e) {
System.out.println("Erreur recuperation inPutStream " + e.toString());
e.printStackTrace();
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("Parametrage du modem : OK");
}
catch (UnsupportedCommOperationException e) {
System.out.println("Impossible de parametrer " + e.toString());
e.printStackTrace();
}
try {

outputStream.write(m0.getBytes());
System.out.println("ecriture de m1 (" + m0 + ") OK");
System.out.print("Tentative de lecture");
byte[] readBuffer = new byte[20];
numBytes = inputStream.read(readBuffer);
System.out.print("readbuffer = " + new String(readBuffer));
System.out.print("numBytes = " + numBytes);

outputStream.write(m1.getBytes());
System.out.println("ecriture de m2 (" + m1 + ") OK");

outputStream.write(m2.getBytes());
System.out.println("ecriture de m0 (" + m2 + ") OK");

outputStream.write(m3.getBytes());
System.out.println("ecriture de m3 (" + m3 + ") OK");

outputStream.write(m4.getBytes());
System.out.println("ecriture de m4 (" + m4 + ") OK");

outputStream.write(m5.getBytes());
System.out.println("ecriture de m5 (" + m5 + ") OK");

outputStream.close();

}
catch (IOException e) {
System.out.println("Impossible d'ecrire " + e.toString());
e.printStackTrace();
}
}

public void getPort(String nomPort) {
CommPortIdentifier portId = null;
try {
portId = CommPortIdentifier.getPortIdentifier(nomPort);
}
catch (NoSuchPortException ex) {
System.out.println("erreur Port '" + nomPort + "' " + ex.toString());
ex.printStackTrace();
}
try {
serialPort = (SerialPort) portId.open("Mon_Appli", delaiObtentionPort);
System.out.println("Obtention du port '" + serialPort.getName() + "' en ecriture : OK");
}
catch (PortInUseException ex) {
System.out.println("erreur Port '" + nomPort + "' deja ouvert : " + ex.toString());
ex.printStackTrace();
}
}
}

le lanceur (main)

import javax.comm.CommPortIdentifier;

/**
* Created by IntelliJ IDEA.
* User: JSC
* Date: 4 déc. 2006
* Time: 14:11:32
* To change this template use File | Settings | File Templates.
*/
public class TestSimpleWrite {

public TestSimpleWrite(){

}
public static void main(String arg[]){

//SimpleRead reader = new SimpleRead("COM1");

SimpleWrite testeurPort = new SimpleWrite();
testeurPort.setDelaiObtentionPort(10000);
testeurPort.initWin32Driver();
testeurPort.AffichageListPort();
testeurPort.getPort("COM1");
testeurPort.lanceCommande();
testeurPort.libereSerialPort();
}
}

et les classe de l'aPI

/*
* @(#)SimpleRead.java 1.12 98/06/25 SMI
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license
* to use, modify and redistribute this software in source and binary
* code form, provided that i) this copyright notice and license appear
* on all copies of the software; and ii) Licensee does not utilize the
* software in a manner which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
* ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
* SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
* BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
* OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control
* of aircraft, air traffic, aircraft navigation or aircraft
* communications; or in the design, construction, operation or
* maintenance of any nuclear facility. Licensee represents and
* warrants that it will not use or redistribute the Software for such
* purposes.
*/

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} 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();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent event) {
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:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}

/*
* @(#)SimpleWrite.java 1.12 98/06/25 SMI
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license
* to use, modify and redistribute this software in source and binary
* code form, provided that i) this copyright notice and license appear
* on all copies of the software; and ii) Licensee does not utilize the
* software in a manner which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
* ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
* SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
* BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
* OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control
* of aircraft, air traffic, aircraft navigation or aircraft
* communications; or in the design, construction, operation or
* maintenance of any nuclear facility. Licensee represents and
* warrants that it will not use or redistribute the Software for such
* purposes.
*/

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
// if (portId.getName().equals("COM1")) {
if (portId.getName().equals("/dev/term/a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteApp", 2000);
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}
}
}
}
}
}

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 FFFF  Y   Y  FFFF  III  H  H 
F Y Y F I H H
FFF Y FFF I HHHH
F Y F I H H
F Y F III H H