J'ai fait un programme qui récupère des données d'un appareil par liaison série.
J'ai modifié mon code pour qu'on puisse choisir le port COM et maintenant ça me fait une erreur parce qu'il n'y a pas de liaison.
Quand je remplace choixPort par "COM2" ça marche.
Mes codes :
connection :
interface :
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
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 public class connection { CommPortIdentifier portId; static SerialPort port; static InputStream in; static BufferedReader br; String donnees; String s; char[] c; private String s2; String choixPort = ""; public connection() throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException { //initialisation du driver Win32Driver w32Driver= new Win32Driver(); w32Driver.initialize(); //récupération du port try{ if (choixPort.equals("")) { portConfig portC = new portConfig(); choixPort = portC.getPort(); } portId=CommPortIdentifier.getPortIdentifier(choixPort); //ouvrir le port port=(SerialPort)portId.open("Mon_Appli", 20000); //paramétrer le port port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); port.setSerialPortParams (9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); //gérer le flux d'entrée in = port.getInputStream(); //lire les données br = new BufferedReader(new InputStreamReader(in)); s = br.readLine(); // while((donnees = br.readLine()) != "") // { // s += " " + donnees; // } for(int i=0; i<2003; i++) { s += " " + br.readLine(); } this.s2 = s; //écrire les données // fichier("c:\\test.txt",s); }finally { close(); } } //écrire les données dans un fichier public void fichier(String monFichier, String mesDonnees) { FileWriter fw; try { fw = new FileWriter(monFichier, true); BufferedWriter bw = new BufferedWriter(fw); bw.write(mesDonnees); bw.newLine(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } public static void close() { try { in.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } port.close(); } public static void main(String[] args) throws IOException, NoSuchPortException, PortInUseException, UnsupportedCommOperationException { new Interface(); } public String getS2() { return this.s2; } public void setS2(String d) { this.s2 = s; } }
configuration port :
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
81
82
83
84
85
86
87
88
89
90
91
92 public class Interface extends JFrame{ private static final long serialVersionUID = 1L; //objets graphiques private JFrame fenetre; private JPanel panel2; private JPanel panel3; private JButton donnees; private JButton quitter; private JLabel label; private JLabel label2; private JScrollPane scrollF; public Interface() { //composants this.fenetre = new JFrame("Test"); this.panel2 = new JPanel(new FlowLayout()); this.panel2 = new JPanel(new FlowLayout()); this.label = new JLabel(); this.panel3 = new JPanel(new FlowLayout()); this.donnees = new JButton("Données"); this.quitter = new JButton("Quitter"); //positionnement fenetre.setSize(300,400); fenetre.setLocationRelativeTo(null); panel2.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder (), "Données "))); panel3.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0), BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder (), "Autre "))); panel2.setPreferredSize(new Dimension(250, 280)); label.setPreferredSize(new Dimension(180,230)); // label2.setPreferredSize(new Dimension(250,75)); panel3.setPreferredSize(new Dimension(250, 75)); donnees.setPreferredSize(new Dimension(100, 30)); quitter.setPreferredSize(new Dimension(100, 30)); //fermeture par défaut this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //listener donnees.addActionListener(new actiondonnees()); quitter.addActionListener(new GestionQuitter()); //gestionnaire de disposition Container conteneur = fenetre.getContentPane(); FlowLayout disposition = new FlowLayout(); conteneur.setLayout(disposition); conteneur.add(this.panel2); conteneur.add(this.panel3); panel2.add(this.label); panel3.add(this.donnees); panel3.add(this.quitter); scrollF = new JScrollPane(label,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); panel2.add(this.scrollF); label.setLabelFor(scrollF); scrollF.setVisible(true); //rendre la fenetre visible this.fenetre.setVisible(true); } //gestion du bouton quitter public class GestionQuitter implements ActionListener { public void actionPerformed(ActionEvent contexte) { System.exit(0); } } //gestion du bouton données public class actiondonnees implements ActionListener { public void actionPerformed(ActionEvent e2) { try { conversion conv = new conversion(); label.setText(conv.getDonnees()); } catch (IOException e1) {e1.printStackTrace();} catch (NoSuchPortException e1) {e1.printStackTrace();} catch (PortInUseException e1) {e1.printStackTrace();} catch (UnsupportedCommOperationException e1) {e1.printStackTrace();} } } }
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
81
82
83
84
85
86
87
88
89
90
91
92
93 public class portConfig { CommPortIdentifier portId; static SerialPort port; static InputStream in; static Enumeration portList; private String chosenPort = ""; private JLabel label; private JFrame config; private JComboBox box; private JPanel panel4; private JPanel panel5; private JButton valider; private JButton annuler; public portConfig() { this.config = new JFrame("Configuration port"); this.panel4 = new JPanel(new FlowLayout()); this.label = new JLabel(); this.box = new JComboBox(); this.panel5 = new JPanel(new FlowLayout()); this.valider = new JButton("Valider"); this.annuler = new JButton("Annuler"); config.setSize(300, 120); config.setLocationRelativeTo(null); Container conteneur2 = config.getContentPane(); FlowLayout disposition2 = new FlowLayout(); conteneur2.setLayout(disposition2); conteneur2.add(this.panel4); conteneur2.add(this.panel5); label.setHorizontalTextPosition(JLabel.CENTER); box.setPreferredSize(new Dimension(150, 20)); label.setLabelFor(box); panel4.add(label); panel4.add(box); valider.setPreferredSize(new Dimension(100, 30)); annuler.setPreferredSize(new Dimension(100, 30)); panel5.add(valider); panel5.add(annuler); ports(); valider.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent evt) { valid(); } } ); annuler.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent evt) { cancel(); } } ); this.config.setVisible(true); } protected void valid() { this.chosenPort = box.getSelectedItem().toString(); config.dispose(); } protected void cancel() { config.dispose(); } public String getPort () { return this.chosenPort; } public void ports() { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); box.addItem(portId.getName()); } } }
Partager