Ver Mensaje Individual
  #5 (permalink)  
Antiguo 06/03/2015, 12:50
REHome
 
Fecha de Ingreso: mayo-2007
Ubicación: PIC-16F84A
Mensajes: 727
Antigüedad: 17 años
Puntos: 8
Respuesta: Netbeans 8 y encontrar fallos.

He empezado de nuevo.

El código ya me enciende los Led y los apaga. Este es su código por si alguien le pueda interesar y este si funciona.

Código Java:
Ver original
  1. import gnu.io.CommPortIdentifier;
  2. import gnu.io.SerialPort;
  3. import java.io.OutputStream;
  4. import java.util.Enumeration;
  5. import javax.swing.JOptionPane;
  6.  
  7. /*
  8.  * To change this license header, choose License Headers in Project Properties.
  9.  * To change this template file, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12.  
  13. /**
  14.  *
  15.  * @author Meta
  16.  */
  17. public class JAVADUINO_Frame extends javax.swing.JFrame {
  18.  
  19.     /**
  20.      * Creates new form JAVADUINO_Frame
  21.      */
  22.    
  23.     private static final String L8on = "Led_8_ON";
  24.     private static final String L8off = "Led_8_OFF";
  25.    
  26.     // Variables de conexión.
  27.     private OutputStream output = null;
  28.     SerialPort serialPort;
  29.     private final String PUERTO = "COM4";
  30.    
  31.     private static final int TIMEOUT = 2000; // 2 segundos.
  32.    
  33.     private static final int DATA_RATE = 115200; // Baudios.
  34.    
  35.     public JAVADUINO_Frame() {
  36.         initComponents();
  37.         inicializarConexion();
  38.     }
  39.    
  40.     public void inicializarConexion(){
  41.         CommPortIdentifier puertoID = null;
  42.         Enumeration puertoEnum = CommPortIdentifier.getPortIdentifiers();
  43.        
  44.         while (puertoEnum.hasMoreElements()){
  45.             CommPortIdentifier actualPuertoID = (CommPortIdentifier) puertoEnum.nextElement();
  46.             if (PUERTO.equals(actualPuertoID.getName())){
  47.                 puertoID = actualPuertoID;
  48.                 break;
  49.             }
  50.         }
  51.        
  52.         if (puertoID == null){
  53.             mostrarError("No se puede conectar al puerto");
  54.             System.exit(ERROR);
  55.         }
  56.        
  57.         try{
  58.             serialPort = (SerialPort) puertoID.open(this.getClass().getName(), TIMEOUT);
  59.             // Parámatros puerto serie.
  60.            
  61.             serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE);
  62.            
  63.             output = serialPort.getOutputStream();
  64.         } catch (Exception e){
  65.             mostrarError(e.getMessage());
  66.             System.exit(ERROR);
  67.         }
  68.     }
  69.    
  70.     private void enviarDatos(String datos){
  71.         try{
  72.             output.write(datos.getBytes());
  73.         } catch (Exception e){
  74.             mostrarError("ERROR");
  75.             System.exit(ERROR);
  76.         }
  77.     }
  78.    
  79.     public void mostrarError(String mensaje){
  80.         JOptionPane.showMessageDialog(this, mensaje, "ERROR", JOptionPane.ERROR_MESSAGE);    
  81.     }
  82.     /**
  83.      * This method is called from within the constructor to initialize the form.
  84.      * WARNING: Do NOT modify this code. The content of this method is always
  85.      * regenerated by the Form Editor.
  86.      */
  87.     @SuppressWarnings("unchecked")
  88.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  89.     private void initComponents() {
  90.  
  91.         jButton1 = new javax.swing.JButton();
  92.         jButton2 = new javax.swing.JButton();
  93.  
  94.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  95.         setTitle("JAVADUINO by Meta");
  96.  
  97.         jButton1.setText("ON");
  98.         jButton1.addActionListener(new java.awt.event.ActionListener() {
  99.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  100.                 jButton1ActionPerformed(evt);
  101.             }
  102.         });
  103.  
  104.         jButton2.setText("OFF");
  105.         jButton2.addActionListener(new java.awt.event.ActionListener() {
  106.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  107.                 jButton2ActionPerformed(evt);
  108.             }
  109.         });
  110.  
  111.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  112.         getContentPane().setLayout(layout);
  113.         layout.setHorizontalGroup(
  114.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  115.             .addGroup(layout.createSequentialGroup()
  116.                 .addContainerGap()
  117.                 .addComponent(jButton1)
  118.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
  119.                 .addComponent(jButton2)
  120.                 .addContainerGap())
  121.         );
  122.         layout.setVerticalGroup(
  123.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  124.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  125.                 .addContainerGap(92, Short.MAX_VALUE)
  126.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  127.                     .addComponent(jButton1)
  128.                     .addComponent(jButton2))
  129.                 .addGap(26, 26, 26))
  130.         );
  131.  
  132.         pack();
  133.     }// </editor-fold>                        
  134.  
  135.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  136.         // TODO add your handling code here:
  137.         enviarDatos(L8on);
  138.     }                                        
  139.  
  140.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  141.         // TODO add your handling code here:
  142.         enviarDatos(L8off);
  143.     }                                        
  144.  
  145.     /**
  146.      * @param args the command line arguments
  147.      */
  148.     public static void main(String args[]) {
  149.         /* Set the Nimbus look and feel */
  150.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  151.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  152.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  153.          */
  154.         try {
  155.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  156.                 if ("Nimbus".equals(info.getName())) {
  157.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  158.                     break;
  159.                 }
  160.             }
  161.         } catch (ClassNotFoundException ex) {
  162.             java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  163.         } catch (InstantiationException ex) {
  164.             java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  165.         } catch (IllegalAccessException ex) {
  166.             java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  167.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  168.             java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  169.         }
  170.         //</editor-fold>
  171.  
  172.         /* Create and display the form */
  173.         java.awt.EventQueue.invokeLater(new Runnable() {
  174.             public void run() {
  175.                 new JAVADUINO_Frame().setVisible(true);
  176.             }
  177.         });
  178.     }
  179.  
  180.     // Variables declaration - do not modify                    
  181.     private javax.swing.JButton jButton1;
  182.     private javax.swing.JButton jButton2;
  183.     // End of variables declaration                  
  184. }

https://www.youtube.com/watch?v=JAwCa4Uk6tA

He instalado todo otra vez JAVA 32 bits en Windows 7 de 64 bits.

Ahora me toca saber como se lee los datos y los muestra en un richTextBox.


Saludos y gracias por todo.
__________________
Meta Shell, VERSIÓN 1.2.2
Descargar