EDIT : j'ai trouvé : il suffit de copier write dans read, car write est plus simple que read et utilise à peu près pareil. il faut ensuite changer quelques variables en static et c'est bon. (je n'ai pas trouvé comment supprimer mon topic, toutefois, ce code pourrait servir à d'autres)

voilà !


Bonjour,

je suis débutant en java et je cherche à trouver un petit programme qui permet à la fois de lire et d'écrire sur un port COM. J'ai cherché dans des dizaines de pages web, y compris ici (http://christophej.developpez.com/tu...java/javacomm/, j'arrive à lancer ses programmes, mais j'ai beau envoyer 'q' sur le port série, ça ne fait rien), et j'ai trouvé deux fichiers sources qui marchent bien et me permettent d'écrire et de lire séparément. Or, j'ai essayé tout un tas de méthodes pour essayer d'unir les deux et je n'y arrive pas. Pourriez-vous m'aider s'il-vous-plaît ? Voici les deux classes :



Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
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("COM8")) {
        // if (portId.getName().equals("/dev/term/a")) {
          SimpleRead reader = new SimpleRead();
        }
      }
    }
  }
 
  public SimpleRead() {
    try {
      serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
    } catch (PortInUseException e) {System.out.println(e);}
    try {
      inputStream = serialPort.getInputStream();
    } catch (IOException e) {System.out.println(e);}
    try {
      serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {System.out.println(e);}
    serialPort.notifyOnDataAvailable(true);
    try {
      serialPort.setSerialPortParams(9600,
        SerialPort.DATABITS_8,
        SerialPort.STOPBITS_1,
        SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {System.out.println(e);}
    readThread = new Thread(this);
    readThread.start();
  }
 
  public void run() {
    try {
      Thread.sleep(20000);
    } catch (InterruptedException e) {System.out.println(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[2];
 
        try {
          while (inputStream.available() > 0) {
            int numBytes = inputStream.read(readBuffer);
          }
          System.out.print(new String(readBuffer));
        } catch (IOException e) {System.out.println(e);}
        break;
      }
    }
}




Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
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("COM8")) {
        //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) {}
        }
      }
    }
  }
}