Salut, Depuis hier, j'essaye de simuler une communication en série avec Arduino à l'aide de logiciel Proteus. Voilà e ce que j'ai fait : 1) Préparer le port série virtuel :
Terminal #1 :
1 2 3 4 | ~$ socat -d -d pty,raw,echo=0 pty,raw,echo=0 2016/02/20 11:14:09 socat[16985] N PTY is /dev/pts/12 2016/02/20 11:14:09 socat[16985] N PTY is /dev/pts/25 2016/02/20 11:14:09 socat[16985] N starting data transfer loop with FDs [3,3] and [5,5] |
Terminal #2 :
1 | ~$ cat < /dev/pts/12
|
Terminal #3 :
1 2 | ~$ sudo ln -s /dev/pts/25 /dev/ttyUSB25 ~$ echo "test" > /dev/ttyUSB25 |
Dans le terminal #2 j'obtient :
1 2 | ~$ cat < /dev/pts/12 test |
2) Programme Arduino (Source Eskimon):
3) Programme Java : netbeans (source : http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port) :
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | //Envoie "h" pour allumer une LED et "l" pour l'éteindre. import gnu.io.CommPort; import gnu.io.CommPortIdentifier; import gnu.io.NoSuchPortException; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.UnsupportedCommOperationException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.logging.Level; import java.util.logging.Logger; public class recieve extends javax.swing.JFrame { static InputStream in; static SerialPort serialPort; Enumeration<CommPortIdentifier> portsEnum; CommPortIdentifier portId; ArrayList<CommPortIdentifier> l; public recieve() { l = new ArrayList(); initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { searchAvailablePortsBtn = new javax.swing.JButton(); portsList = new javax.swing.JComboBox<>(); jScrollPane1 = new javax.swing.JScrollPane(); portsListDisplayArea = new javax.swing.JTextArea(); disconnectBtn = new javax.swing.JButton(); connectBtn = new javax.swing.JButton(); ledOnBtn = new javax.swing.JButton(); ledOfBtn = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); searchAvailablePortsBtn.setText("Search availble ports"); searchAvailablePortsBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { searchAvailablePortsBtnActionPerformed(evt); } }); portsList.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "" })); portsListDisplayArea.setColumns(20); portsListDisplayArea.setRows(5); jScrollPane1.setViewportView(portsListDisplayArea); disconnectBtn.setText("Disconnect"); disconnectBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { disconnectBtnActionPerformed(evt); } }); connectBtn.setText("Connect"); connectBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { connectBtnActionPerformed(evt); } }); ledOnBtn.setText("LED ON"); ledOnBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ledOnBtnActionPerformed(evt); } }); ledOfBtn.setText("LED OFF"); ledOfBtn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ledOfBtnActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(62, 62, 62) .addComponent(connectBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(disconnectBtn) .addGap(86, 86, 86)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(searchAvailablePortsBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(portsList, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(48, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGap(71, 71, 71) .addComponent(ledOnBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(ledOfBtn) .addGap(57, 57, 57)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(34, 34, 34) .addComponent(searchAvailablePortsBtn) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(portsList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(disconnectBtn) .addComponent(connectBtn)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ledOnBtn) .addComponent(ledOfBtn)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold> private void searchAvailablePortsBtnActionPerformed(java.awt.event.ActionEvent evt) { portsList.removeAllItems(); portsEnum = CommPortIdentifier.getPortIdentifiers(); while(portsEnum.hasMoreElements()) { portId = portsEnum.nextElement(); portsList.addItem(portId.getName()); l.add(portId); System.out.println(portId.getName()); } } private void disconnectBtnActionPerformed(java.awt.event.ActionEvent evt) { serialPort.close(); portsListDisplayArea.append(portsList.getSelectedItem().toString()+" disconnected.\n"); } private void connectBtnActionPerformed(java.awt.event.ActionEvent evt) { try { CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portsList.getSelectedItem().toString()); if ( portIdentifier.isCurrentlyOwned() ) portsListDisplayArea.append("Error: Port is currently in use"); else { CommPort commPort = portIdentifier.open(this.getClass().getName(),2000); if ( commPort instanceof SerialPort ) { serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); portsListDisplayArea.append("Connected to " + portsList.getSelectedItem().toString()+"\n"); } else portsListDisplayArea.append("Error: Only serial ports are handled by this example.\n"); } } catch (NoSuchPortException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } catch (PortInUseException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedCommOperationException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } } private void ledOnBtnActionPerformed(java.awt.event.ActionEvent evt) { try { OutputStream out = serialPort.getOutputStream(); (new Thread(new SerialWriter(out,1))).start(); } catch (IOException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } } private void ledOfBtnActionPerformed(java.awt.event.ActionEvent evt) { try { OutputStream out = serialPort.getOutputStream(); (new Thread(new SerialWriter(out,0))).start(); } catch (IOException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } } /** * @param args the command line arguments */ /** */ public static class SerialReader implements Runnable { InputStream in; public SerialReader ( InputStream in ) { this.in = in; } public void run () { byte[] buffer = new byte[1024]; int len = -1; try { while ( ( len = this.in.read(buffer)) > -1 ) { portsListDisplayArea.append(new String(buffer,0,len)); } } catch ( IOException e ) { e.printStackTrace(); } } } /** */ public static class SerialWriter implements Runnable { OutputStream out; int flag; public SerialWriter ( OutputStream out , int flag) { this.out = out; this.flag = flag; } public void run () { try { if (flag == 1) out.write('h'); else out.write('l'); } catch (IOException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } } } public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(recieve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(recieve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(recieve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(recieve.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new recieve().setVisible(true); } }); try { in = serialPort.getInputStream(); (new Thread(new SerialReader(in))).start(); } catch (IOException ex) { Logger.getLogger(recieve.class.getName()).log(Level.SEVERE, null, ex); } } // Variables declaration - do not modify private javax.swing.JButton connectBtn; private javax.swing.JButton disconnectBtn; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JButton ledOfBtn; private javax.swing.JButton ledOnBtn; private javax.swing.JComboBox<String> portsList; private static javax.swing.JTextArea portsListDisplayArea; private javax.swing.JButton searchAvailablePortsBtn; // End of variables declaration } |
4) Montage proteus :
5) Exécution :
Comme vous voyez, le port reçoit "h", mais la LED ne s'allume pas. Aidez-moi s'il vous plaît. Merci
+0
-0