import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.swing.ImageIcon;
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
*/
public class GUI_conexion_JAVARDUINO
extends javax.
swing.
JFrame {
/**
* Creates new form GUI_conexion_JAVARDUINO
*/
private static final String YELLOW_LED_OFF
= "0"; private static final String YELLOW_LED_ON
= "1"; private static final String RED_LED_OFF
= "1"; private static final String RED_LED_ON
= "3";
// IDs de conexion
// OutputStream es una clase abstracta de la que no pueden crearse objetos. No obstante, todos los objetos que se creen de clases derivadas dispondrán de algunas operaciones interesantes:
// que respectivamente escriben un byte, un array de bytes, o una zona de un vector de bytes.
private OutputStream output
= null; // requiere: import java.io.OutputStream; SerialPort serialPort; // requiere: import java.io.OutputStream;
private final String PUERTO
= "COM4"; // Tiempo de espera (ms) mientras se abre el puerto serie
private static final int TIMEOUT = 2000;
private static final int DATA_RATE = 9600;
public GUI_conexion_JAVARDUINO() {
initComponents();
inicializarConexion();
jLabel1.
setIcon(new ImageIcon("src/RedLED_OFF_icon.png")); jLabel2.
setIcon(new ImageIcon("src/YellowLED_OFF_icon.png")); }
public void inicializarConexion() {
CommPortIdentifier puertoID = null; // requiere: import gnu.io.CommPortIdentifier;
// lista de puertos encontrados
Enumeration puertoEnum
= CommPortIdentifier.
getPortIdentifiers(); // requiere: import java.util.Enumeration; // Mientras tenga mas elementos
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" + PUERTO);
}
try {
serialPort = (SerialPort) puertoID.open(this.getClass().getName(), TIMEOUT);
//parametros puerto serie
serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
// Flujo de datos
output = serialPort.getOutputStream();
mostrarError(e.getMessage());
}
}
private void enviarDatos
(String datos
) { try {
output.write(datos.getBytes());
mostrarError("ERROR ENVIAR");
}
}
private 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() {
btnLED_ON
= new javax.
swing.
JButton(); btnLED_OFF
= new javax.
swing.
JButton(); jLabel1
= new javax.
swing.
JLabel(); jLabel2
= new javax.
swing.
JLabel();
btnLED_ON.setText("LED OFF");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { btnLED_ONActionPerformed(evt);
}
});
btnLED_OFF.setText("LED ON");
jLabel1.setText("jLabel1");
jLabel2.setText("jLabel1");
radBtnRed.setText("LED ROJO");
radBtnYellow.setText("LED AMARILLO");
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(radBtnYellow)
.
addGap(0,
0,
Short.
MAX_VALUE)) .addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnLED_ON)
.addComponent(jLabel1))
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED,
173,
Short.
MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnLED_OFF)
.addComponent(jLabel2))
.addGap(75, 75, 75))
.addGroup(layout.createSequentialGroup()
.addComponent(radBtnRed)
.
addContainerGap(javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)))) );
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(radBtnRed)
.addGap(18, 18, 18)
.addComponent(radBtnYellow)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED,
129,
Short.
MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnLED_ON)
.addComponent(btnLED_OFF))
.addGap(45, 45, 45))
);
pack();
}// </editor-fold>
private void btnLED_ONActionPerformed
(java.
awt.
event.
ActionEvent evt
) { // TODO add your handling code here:
}
/**
* @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(GUI_conexion_JAVARDUINO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(GUI_conexion_JAVARDUINO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(GUI_conexion_JAVARDUINO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(GUI_conexion_JAVARDUINO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new GUI_conexion_JAVARDUINO().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JButton btnLED_OFF
; private javax.
swing.
JButton btnLED_ON
; private javax.
swing.
JLabel jLabel1
; private javax.
swing.
JLabel jLabel2
; // End of variables declaration
}