Uso Windows 7 de 64 bits con Java de 32 para que me funcione el famoso RXTX.
He hecho una aplicación sencilla que se puede encender un Led y apagarlo con NetBeans 8 como puedes ver en la imagen de arriba.
1) Cuando el Form o formulario o ventana está abierta o ejecuto Java, me aparece arriba a la izquierda. ¿Cómo se pone en el centro cuando ejecutes a apliación?
2) Ahora quiero hacer un cuadro de texto para que muestre los textos que te entran en el puerto serie.
¿Alguna idea?
Su código para encender y apagar un Led es este.
Código
import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.OutputStream; import java.util.Enumeration; import javax.swing.JOptionPane; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Meta */ /** * Creates new form JAVADUINO_Frame */ // Variables de conexión. SerialPort serialPort; private static final int TIMEOUT = 2000; // 2 segundos. private static final int DATA_RATE = 115200; // Baudios. public JAVADUINO_Frame() { initComponents(); inicializarConexion(); } public void inicializarConexion(){ CommPortIdentifier puertoID = null; while (puertoEnum.hasMoreElements()){ CommPortIdentifier actualPuertoID = (CommPortIdentifier) puertoEnum.nextElement(); if (PUERTO.equals(actualPuertoID.getName())){ puertoID = actualPuertoID; break; } } if (puertoID == null){ mostrarError("No se puede conectar al puerto"); } try{ serialPort = (SerialPort) puertoID.open(this.getClass().getName(), TIMEOUT); // Parámatros puerto serie. serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_2, SerialPort.PARITY_NONE); output = serialPort.getOutputStream(); mostrarError(e.getMessage()); } } try{ output.write(datos.getBytes()); mostrarError("ERROR"); } } } /** * 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() { setTitle("JAVA y más Java"); jButton1.setText("ON"); jButton1ActionPerformed(evt); } }); jButton2.setText("OFF"); jButton2ActionPerformed(evt); } }); jLabel1.setText("Mensaje desde el puerto serie:"); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jButton2)) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2)) .addGap(31, 31, 31) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) ); pack(); }// </editor-fold> // TODO add your handling code here: enviarDatos(L8on); } // TODO add your handling code here: enviarDatos(L8off); } /** * @param args the command line arguments */ /* 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())) { break; } } java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); java.util.logging.Logger.getLogger(JAVADUINO_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ public void run() { new JAVADUINO_Frame().setVisible(true); } }); } // Variables declaration - do not modify // End of variables declaration }
Saludos.