I am testing a class of data receipts via COM ports in Windows. I get this data from an embedded dsPIC-based system.
When sending data from the microcontroller to the computer via USB (COM3), the data is received normally, but when using bluetooth (COM4) my program does not receive anything.
ErrorconnectingtoCOMportandreceivingdataviabluetoothinJava:
Totestthereceiptofdataviabluetoothonthecomputer,IusedtheHerculesSoftware,andconfirmedthatthereceptionisnormallydone,onlybyJavaIcannotreceive.
DatareceivedviaBluetoothonHercules:
IhavetestedthisprogramonWindows10x64,Windows7x86,Windows7x64,withtwonativebluetoothnotebookadaptersandaUSBbluetoothadapter,butwithoutsuccessinJava.
IusedthelibraryandDLLavailable in this link .
I need to receive this data via Bluetooth because it is the only wireless communication available on the embedded system.
Follow my code.
Class TestSerial (main):
package testeserial;
public class TesteSerial {
public static void main(String[] args) {
Serial s = new Serial();
if (s.iniciaSerial()) {
while (true) {
}
}
}
}
Serial Class:
package testeserial;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Enumeration;
public class Serial implements SerialPortEventListener {
boolean status = false;
SerialPort serialPort = null;
private String appName; //nome da aplicação
private BufferedReader input; //obj para leitura na serial
private OutputStream output; //obj que coordena na a saida de dados
private static final int TIME_OUT = 5000;//tempo de espera
private static int DATA_RATE = 9600;//velocidade de comunicação
private String serialPortName = "COM4"; //numero da COM
String aux[];
private String tipoDado;
private String latitude;
private String longitude;
private String proxPonto;
private String detectaPonto;
private String graus;
private String leituraComando;
//gera o log para o grafico
private void log(String caminho, String conteudo) throws IOException {
Path arquivo;
arquivo = Paths.get(caminho);
Files.write(arquivo, conteudo.getBytes(), StandardOpenOption.APPEND);
}
public boolean iniciaSerial() {
try {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
System.out.println("entrou no try");
while (portId == null && portEnum.hasMoreElements()) {
CommPortIdentifier currentPortId = (CommPortIdentifier) portEnum.nextElement();
System.out.println("entrou no while");
if (currentPortId.getName().equals(serialPortName) || currentPortId.getName().startsWith(serialPortName)) {
System.out.println("entrou no if");
serialPort = (SerialPort) currentPortId.open(appName, TIME_OUT);
portId = currentPortId;
System.out.println("Conecetado em " + currentPortId.getName());
break;
}
}
if (portId == null || serialPort == null) {
System.out.println("Deu ruim");
return false;
}
serialPort.setSerialPortParams(DATA_RATE,
serialPort.DATABITS_8,
serialPort.STOPBITS_1,
serialPort.PARITY_NONE);
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
status = true;
try {
Thread.sleep(TIME_OUT);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
status = false;
}
return status;
}
//envia dados
public void sendData(String data) {
try {
String teste = data;
output = serialPort.getOutputStream();
output.write(teste.getBytes());
output.flush();
} catch (Exception e) {
System.err.println(e.toString());
}
}
//fecha a porta
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
//Metodo que lida com a entrada de dados
@Override
public void serialEvent(SerialPortEvent spe) {
try {
switch (spe.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
if (input == null) {
input = new BufferedReader(
new InputStreamReader(
serialPort.getInputStream()));
}
System.out.println("Entrada: " + getLeituraComando());
setLeituraComando(input.readLine());
aux = leituraComando.split(",");
if (aux.length == 6) {
setTipoDado(aux[0]);
setLatitude(aux[1]);
setLongitude(aux[2]);
setProxPonto(aux[3]);
setDetectaPonto(aux[4]);
setGraus(aux[5]);
}
break;
default:
break;
}
} catch (Exception e) {
//e.printStackTrace();
}
}
public String getSerialPortName() {
return serialPortName;
}
public void setSerialPortName(String serialPortName) {
this.serialPortName = serialPortName;
}
public static int getDATA_RATE() {
return DATA_RATE;
}
public static void setDATA_RATE(int DATA_RATE) {
Serial.DATA_RATE = DATA_RATE;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public SerialPort getSerialPort() {
return serialPort;
}
public void setSerialPort(SerialPort serialPort) {
this.serialPort = serialPort;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public BufferedReader getInput() {
return input;
}
public void setInput(BufferedReader input) {
this.input = input;
}
public OutputStream getOutput() {
return output;
}
public void setOutput(OutputStream output) {
this.output = output;
}
public String getTipoDado() {
return tipoDado;
}
public void setTipoDado(String tipoDado) {
this.tipoDado = tipoDado;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getProxPonto() {
return proxPonto;
}
public void setProxPonto(String proxPonto) {
this.proxPonto = proxPonto;
}
public String getDetectaPonto() {
return detectaPonto;
}
public void setDetectaPonto(String detectaPonto) {
this.detectaPonto = detectaPonto;
}
public String getGraus() {
return graus;
}
public void setGraus(String graus) {
this.graus = graus;
}
public String getLeituraComando() {
return leituraComando;
}
public void setLeituraComando(String leituraComando) {
this.leituraComando = leituraComando;
}
public String[] getAux() {
return aux;
}
public void setAux(String[] aux) {
this.aux = aux;
}
}