Code java pour envoi et récupération de flux

Fermé
Paul - Modifié le 10 juil. 2017 à 23:19
Bonsoir à tous , je veut réaliser un application java qui envoi des commendes à un modem et récupère la réponse du modem a travers une liaison serial (port COM) (en utilisant javax.comm) j'ai essayé avec ce code mais ça n'a pas marché !! quelqu'un peut m'aider ?? merci


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) {
    boolean        portFound = false;
    String        defaultPort = "COM1";

  if (args.length > 0) {
     defaultPort = args[0];
 } 
   
 portList = CommPortIdentifier.getPortIdentifiers();

 while (portList.hasMoreElements()) {
     portId = (CommPortIdentifier) portList.nextElement();
     if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  if (portId.getName().equals(defaultPort)) {
      System.out.println("Found port: "+defaultPort);
      portFound = true;
      SimpleRead reader = new SimpleRead();
  } 
     } 
 } 
 if (!portFound) {
     System.out.println("port " + defaultPort + " not found.");
 } 
  
    } 

   
    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("The Read Bytes from SerialPort are");
      System.out.write(readBuffer);
      System.out.println();
  } 

  System.out.print(new String(readBuffer));
     } catch (IOException e) {}

     break;
 }
    } 

}
A voir également: