Hola:
Tengo un código medio hecho en el cual no se el motivo de los fallos. Se trata de encender y apagar un Led con Netbeans 8 en mi caso y Arduino.
Código Java:
Ver originalimport 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_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
) { }
}
Todavía no he programado los botones, hay que acabar con los fallos primero. Los mensajes que me dan son estos.
Cita: run:
java.lang.ClassFormatError: Duplicate field name&signature in class file JAVADUINO_JFrame
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java :760)
at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader .java:455)
at java.net.URLClassLoader.access$100(URLClassLoader. java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 367)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 24)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 57)
at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:495)
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
He segudio los tutoriales hasta el minuto 37':30" de este vídeo.
https://www.youtube.com/watch?v=4Hr_LZ62SdY
¿Alguna idea?
Saludos.