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
*/
public class JAVADUINO_Frame
extends javax.
swing.
JFrame {
/**
* Creates new form JAVADUINO_Frame
*/
private static final String L8on
= "Led_8_ON"; private static final String L8off
= "Led_8_OFF";
// Variables de conexión.
SerialPort serialPort;
private final String PUERTO
= "COM4";
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;
Enumeration puertoEnum
= CommPortIdentifier.
getPortIdentifiers();
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());
}
}
private void enviarDatos
(String datos
){ try{
output.write(datos.getBytes());
mostrarError("ERROR");
}
}
public void mostrarError
(String mensaje
){ }
/**
* 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() {
jButton1
= new javax.
swing.
JButton(); jButton2
= new javax.
swing.
JButton(); jLabel1
= new javax.
swing.
JLabel();
setTitle("JAVA y más Java");
jButton1.setText("ON");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton1ActionPerformed(evt);
}
});
jButton2.setText("OFF");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { 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)
.
addGap(0,
0,
Short.
MAX_VALUE))) .addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.
addGap(0,
0,
Short.
MAX_VALUE)) );
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)
.
addContainerGap(javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)) );
pack();
}// </editor-fold>
private void jButton1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { // TODO add your handling code here:
enviarDatos(L8on);
}
private void jButton2ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { // TODO add your handling code here:
enviarDatos(L8off);
}
/**
* @param args the command line arguments
*/
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 {
if ("Nimbus".equals(info.getName())) {
javax.
swing.
UIManager.
setLookAndFeel(info.
getClassName()); 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
private javax.
swing.
JButton jButton1
; private javax.
swing.
JButton jButton2
; private javax.
swing.
JLabel jLabel1
; // End of variables declaration
}