import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import app.Com;
import app.Parameters;
import core.SerialPort;
public class Ventana extends JFrame implements ActionListener{
JLabel labelDato;
private JButton boton;
private Hilo hilo;
public Ventana() {
setLayout(null);
labelDato = new JLabel("...");
boton=new JButton("iniciar");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("...:::PUERTO:::SERIAL:::...");
labelDato.setFont(new Font("Arial", 0, 54));
labelDato.setLocation(10,10);
labelDato.setSize(280,280);
boton.setLocation(10,300);
boton.setSize(280,20);
boton.addActionListener(this);
add(labelDato);
add(boton);
setLocation(300,300);
setSize(300,350);
centrar(this);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent evento){
if(boton.getText().matches("iniciar")){
boton.setText("detener");
iniciar();
}else{
boton.setText("iniciar");
detener();
}
}
public void centrar(Component componente){
Toolkit tk=Toolkit.getDefaultToolkit ();
int lx=(int) tk.getScreenSize().getWidth()*1;
int ly=(int) tk.getScreenSize().getHeight()*1;
lx=lx/2-componente.getWidth()/2;
ly=ly/2-componente.getHeight()/2;
componente.setLocation(lx,ly);
}
private void iniciar(){
try {
if(hilo==null){
hilo = new Hilo(this);
hilo.start();
}else{
hilo.resume();
}
}catch (Exception ex) {
System.out.println (ex);
}
}
private void detener(){
try {
hilo.suspend();
labelDato.setText("...");
setTitle("...:::PUERTO:::SERIAL:::...");
}catch (Exception ex) {
System.out.println (ex);
}
}
}
class Hilo extends Thread{
private Ventana ventana;
public Hilo(Ventana ven){
ventana=ven;
}
public void run(){
try {
SerialPort libres = new SerialPort();
List lista = libres.getFreeSerialPort();
if(lista.size()<1){
throw new Exception("NO SE HAN ENCONTRADO PUERTOS DISPONIBLES");
}
String puerto=lista.get(0);
Parameters configuracion = new Parameters();
configuracion.setPort(puerto);
configuracion.setBaudRate("9600");
configuracion.setStopBits("2");
Com com = new Com(configuracion);
int dato=0;
int i=0;
System.out.println("LOG");
while(true){
dato = com.receiveSingleDataInt();
ventana.labelDato.setText( dato +"");
ventana.setTitle("...:::PUERTO:::SERIAL::("+puerto+"):::..."+dato);
System.out.println("lee "+dato);
sleep(100);
com.sendSingleData(i);
System.out.println("env "+i);
i++;
}
}catch (Exception ex) {
System.out.println (ex);
}
}
public void detener(){
ventana.dispose();
ventana=null;
}
}
class Ejecutable{
public static void main(String args[]) {
Ventana v=new Ventana();
}
}
y aqui les dejo los enlaces para descargar la libreria y el codigo:
http://www.4shared.com/file/102621757/a6780501/conexion_java_serial_db9.html
si ese no funciona prueba con este:
http://www.4shared.com/get/102621757/a6780501/conexion_java_serial_db9.html