I'm having problems sending data to the arduino Java application, because I'm trying to light a lamp only to send the card making only flashes and is not connected, it's like sending the wrong message, and the message is sent by the IDE arduino works normally, then this code:
package br.com.comunicacaoserial;
import gnu.io.CommPortIdentifier;
import java.util.Enumeration;
public class SerialCom {
protected String[] portas;
protected Enumeration listaDePortas;
public SerialCom(){
listaDePortas = CommPortIdentifier.getPortIdentifiers();
}
public String[] ObterPortas(){
return portas;
}
public void ListarPortas(){
int i = 0;
while(listaDePortas.hasMoreElements()){
CommPortIdentifier ips = (CommPortIdentifier) listaDePortas.nextElement();
portas[i] = ips.getName();
i++;
}
}
public boolean PortaExiste(String COMp){
String temp;
boolean e = false;
while(listaDePortas.hasMoreElements()){
CommPortIdentifier ips = (CommPortIdentifier) listaDePortas.nextElement();
temp = ips.getName();
if(temp.equals(COMp) == true){
e = true;
}
}
return e;
}
}
package br.com.comunicacaoserial;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import sun.awt.windows.ThemeReader;
public class SerialComLeitura implements Runnable, SerialPortEventListener {
String DadosLidos;
int nodeBytes;
private int baudrate;
private int timeout;
private CommPortIdentifier cp;
private SerialPort porta;
private OutputStream saida;
private InputStream entrada;
private Thread threadLeitura;
private boolean IDPortaOK;
private boolean PortaOK;
private boolean Leitura;
private boolean Escrita;
private String Porta;
protected String peso;
public SerialComLeitura(String p, int b, int i){
this.Porta = p;
this.baudrate = b;
this.timeout = i;
}
public void setPeso(String peso){
this.peso = peso;
}
public String getPeso(){
return peso;
}
public void HabilitarEscrita(){
Escrita = true;
Leitura = false;
}
public void HabilitarLeitura(){
Leitura = true;
Escrita = false;
}
public void ObterIdDaPorta(){
try{
cp = CommPortIdentifier.getPortIdentifier(Porta);
if(cp == null){
System.out.println("Erro da porta");
IDPortaOK = false;
System.exit(1);
}
IDPortaOK = true;
}catch(Exception e){
System.out.println("Erro obtendo ID da porta: "+e);
IDPortaOK = false;
System.exit(1);
}
}
public void AbrirPorta(){
try{
porta = (SerialPort) cp.open("SerialCommLeitura", timeout);
PortaOK = true;
//configura paramêtros
porta.setSerialPortParams(baudrate, porta.DATABITS_8, porta.STOPBITS_1, porta.PARITY_NONE);
porta.setFlowControlMode(porta.FLOWCONTROL_NONE);
}catch(Exception e){
PortaOK = false;
System.out.println("Erro abrindo a comunicacão: "+e);
System.exit(1);
}
}
public void LerDados(){
if(Leitura == false){
try{
entrada = porta.getInputStream();
}catch(Exception e){
System.out.println("Erro de stream: "+e);
System.exit(1);
}
try{
System.out.println(entrada);
porta.addEventListener(this);
}catch(Exception e){
System.out.println("Erro na listener: "+e);
System.exit(1);
}
porta.notifyOnDataAvailable(true);
try{
threadLeitura = new Thread();
threadLeitura.start();
run();
}catch(Exception e){
System.out.println("Erro de Thead: "+e);
}
}
}
public void EnviaUmaString(char msg){
if(Escrita == true){
try{
saida = porta.getOutputStream();
System.out.println("Fluxo OK");
}catch(Exception e){
System.out.println("Erro Status: "+e);
}
try{
System.out.println("Enviando um byte para "+Porta);
System.out.println("Enviando: "+msg);
saida.write(Byte.valueOf(String.valueOf(msg)));
Thread.sleep(100);
saida.flush();
}catch(Exception e){
System.out.println("Houve um erro durante o envio");
System.out.println("Status: "+e);
System.exit(1);
}
}else{
System.exit(1);
}
}
@Override
public void run() {
try{
Thread.sleep(5);
}catch(Exception e){
System.out.println("Erro na thread: "+e);
}
}
@Override
public void serialEvent(SerialPortEvent spe) {
StringBuffer bufferLeitura = new StringBuffer();
int novoDado = 0;
switch(spe.getEventType()){
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
//Novo algoritmo de leitura
while(novoDado != -1){
try{
novoDado = entrada.read();
if(novoDado == -1){
break;
}
if('\r' == (char)novoDado){
bufferLeitura.append("\n");
}else{
bufferLeitura.append((char)novoDado);
}
}catch(IOException e){
System.out.println("Erro na leitura serial: "+e);
}
}
setPeso(new String(bufferLeitura));
System.out.println(getPeso());
break;
}
}
public void FecharCommm(){
try{
porta.close();
}catch(Exception e){
System.out.println("Erro ao fechar a comunicação: "+e);
System.exit(0);
}
}
public String ObterPortA(){
return Porta;
}
public int ObterBaudrate(){
return baudrate;
}
}
package serial;
import br.com.comunicacaoserial.SerialCom;
import br.com.comunicacaoserial.SerialComLeitura;
public class Appserial extends SerialCom {
public static void main(String[] args){
//Iniciando a leitura
SerialComLeitura leitura = new SerialComLeitura("COM23", 9600, 0);
leitura.HabilitarEscrita();
leitura.ObterIdDaPorta();
leitura.AbrirPorta();
leitura.EnviaUmaString('1');
// Controle da leitura aberta na serial
try{
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Erro na Thread: " + e);
}
leitura.FecharCommm();
}
}
arduino:
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
byte c = Serial.read();
if(c == '1') digitalWrite(13, 1);
if(c == 'A') digitalWrite(13, 0);
}