Tenho um codigo que faz a leitura da Porta serial atravez de uma thread, mais quando tento atualizar uma label eu não consigo. Implementei uma classe SerialRead Runnable, e nessa classe eu executo um loop para ler a porta serial. Até ai tudo bem, consigo ler a porta serial e imprimir ela no console. Mais quando eu tento atualizar a Label jLtempoatual, nada acontece na interface grafica. Ja testei se esta rodando em EDT e esta rodando tudo em EDT. Porem a jLtempoatual não esta atualizando em tempo de execução no jFrame.
[code]import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.awt.EventQueue;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
-
@author bruno
*/
public class Controle extends javax.swing.JFrame {/**
-
Creates new form Controle
*/
public Controle() {initComponents();
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}private void jLtempatualMouseMoved(java.awt.event.MouseEvent evt) {
}
public void Transporte(final String comando) {
-
jLtempatual.setText(comando);
}
public void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName); // portName é o nome da porta que vai ser aberta para comunicação
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Porta está atualmente em uso");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); //Parametros de comunicação, o primeiro numero é a velocidade de comunicação.
InputStream in = serialPort.getInputStream();
//OutputStream out = serialPort.getOutputStream();
Thread Leitura = new Thread(new SerialReader(in), "Leitura thread");
Leitura.setDaemon(true);
Leitura.start();
// (new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Somente as portas seriais são suportadas.");
}
}
}
public class SerialReader implements Runnable
{
InputStream in;
String teste;
public SerialReader ( InputStream in )
{
this.in = in;
}
//EventQueue.invokeLater(new Runnable() {
public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 ) //Ler a serial e coloca na variavel buffer
{
teste=new String(buffer,0,len);
EventQueue.invokeLater(new Runnable() {
public void run() {
System.out.println(teste); //Imprime o resulatado no console
System.out.println("Esta Thread está Rodando em EDT: " +
EventQueue.isDispatchThread());
jLtempatual.setText(teste);
jTprompt.append(teste);
Transporte(teste);
}
});
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
String nome="";
nome=jButton5.getText();
if(nome.equals(“FAN OFF”)) {
jButton5.setText(“FAN ON”);
} else {
jButton5.setText(“FAN OFF”);
}
}
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter ( OutputStream out )
{
this.out = out;
}
public void run ()
{
try
{
int c = 0;
while ( ( c = System.in.read()) > -1 )
{
this.out.write(c); //Envia 0 pela serial??
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
public static void main ( String[] args )
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try
{
(new Controle()).connect("/dev/ttyUSB1"); //Passa o nome da porta como parametro para classe connect
final Controle frame = new Controle();
frame.setVisible(true);
}
catch ( Exception e )
{
// TODO Auto-generated catch block
System.out.print(“Não foi possivel conectar: “+ e+” Dispositivo desconectado.”);
}
}});
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JCheckBox jCheckBox1;
private javax.swing.JCheckBox jCheckBox2;
private javax.swing.JCheckBox jCheckBox3;
private javax.swing.JCheckBox jCheckBox4;
private javax.swing.JCheckBox jCheckBox5;
private javax.swing.JLabel jLTempo;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel19;
private javax.swing.JLabel jLabel20;
private javax.swing.JLabel jLabel21;
private javax.swing.JLabel jLabel22;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JLabel jLbase;
private javax.swing.JLabel jLrampa;
public javax.swing.JLabel jLtempatual;
private javax.swing.JLabel jLtempmax;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField10;
private javax.swing.JTextField jTextField11;
private javax.swing.JTextField jTextField13;
private javax.swing.JTextField jTextField14;
private javax.swing.JTextField jTextField15;
private javax.swing.JTextField jTextField17;
private javax.swing.JTextField jTextField18;
private javax.swing.JTextField jTextField19;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField22;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField jTextField9;
public javax.swing.JTextArea jTprompt;
// End of variables declaration
}[/code]