Boa tarde, estou fazendo um projeto de leitura de um sensor com arduino, via porta serial porém estou tendo problemas ao mostrar os dados recebidos na interface.
A parte de leitura está totalmente funcional, e é feita através do código abaixo. Porém quando tento recuperar os dados lidos e adicionar em um JTextArea não consigo.
[code]public void lerDados() {
if (habilitar == false) {
try {
entrada = port.getInputStream();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ERRO de Stream", "ERRO", JOptionPane.ERROR_MESSAGE);
System.out.println("ERRO de Stream" + e);
}
try {
port.addEventListener(this);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ERRO de Listener", "ERRO", JOptionPane.ERROR_MESSAGE);
System.out.println("ERRO de Listener: " + e);
}
port.notifyOnDataAvailable(true);
try {
threadLeitura = new Thread(this);
threadLeitura.start();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ERRO de Thread", "ERRO", JOptionPane.ERROR_MESSAGE);
System.out.println("ERRO de Thread" + e);
}
}
}
public void run() {
try {
Thread.sleep(50);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "ERRO de Thread!", "ERRO", JOptionPane.ERROR_MESSAGE);
System.out.println("ERRO de Thread!" + e);
}
}
public String getDados() {
try {
Thread.sleep(50);
} catch (InterruptedException ex) {
Logger.getLogger(SerialComInOut.class.getName()).log(Level.SEVERE, null, ex);
}
return dados;
}
@Override
public void serialEvent(SerialPortEvent spe) {
StringBuffer bufferleitura = new StringBuffer();
int novodado = 0;
if (spe.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
while ((novodado = entrada.read()) != -1) {
if ('\r' == (char) novodado) {
bufferleitura.append('\n');
} else {
bufferleitura.append((char) novodado);
}
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "ERRO de Leitura Serial", "ERRO", JOptionPane.ERROR_MESSAGE);
System.out.println("ERRO de leitura serial." + ex);
}
dados = (new String(bufferleitura));
System.out.println("Dados lidos: " + dados);
}
}[/code]
Código da interface estou utilizando cardLayout.
[code]mprincipal = new JMenuItem(“Principal”);
mprincipal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mostracard(“pprincipal”);
portaOK = scio.statusPorta();
JPanel ppcetral = new JPanel(new GridLayout(2, 1, 10, 10));
ppcetral.add(new JLabel("Dados recebidos:"));
dadosrecebidos = new JTextArea("Dados recebidos da porta serial: ", 5, 10);
sp = new JScrollPane(dadosrecebidos);
ppcetral.add(sp);
pprincipal.add(ppcetral, BorderLayout.CENTER);
if (portaOK == false) {
JOptionPane.showMessageDialog(null, " Nenhuma porta serial aberta!\n Você será direcionado a página de configuração", "ERRO", JOptionPane.ERROR_MESSAGE);
mostracard("configurar");
} else {
scio.habilitarLeitura();
scio.lerDados();
String aux = scio.getDados();
if (aux != null) {
dadosrecebidos.append(aux);
System.out.println("DADOS" + aux);
}
}
}
});[/code]
Creio que o problema esteja em que quando chamo o método lerDados() o método utiliza um Listener e só roda quando há algo a ser lido.