import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
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_JFrame
extends javax.
swing.
JFrame {
/**
* Creates new form JAVADUINO_JFrame
*/
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;
private static final int DATA_RATE = 115200;
public JAVADUINO_JFrame() {
initComponents();
inicializarConexion();
}
public final 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ámetros 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();
jButton1.setText("ON");
jButton2.setText("OFF");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jButton1)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED,
101,
Short.
MAX_VALUE) .addComponent(jButton2)
.addGap(45, 45, 45))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.
addContainerGap(250,
Short.
MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(27, 27, 27))
);
pack();
}// </editor-fold>
/**
* @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_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(JAVADUINO_JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new JAVADUINO_JFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JButton jButton1
; private javax.
swing.
JButton jButton2
; // End of variables declaration
private void mostrarError
(String no_se_puede_conectar_al_puerto
) { }
}