saudações…
algumas vezes não consigo escrever na serial, Me dá a notificação OUTPUT_BUFFER_EMPTY. O que isso significa? Alguém sabe como resolver esse problema?
Porque só consigo enviar novamente se reiniciar a aplicação.
saudações…
algumas vezes não consigo escrever na serial, Me dá a notificação OUTPUT_BUFFER_EMPTY. O que isso significa? Alguém sabe como resolver esse problema?
Porque só consigo enviar novamente se reiniciar a aplicação.
Se você utilizar adaptadores usb<->serial utilize o seguinte método:
portaSerial.notifyOutputEmpty(true)
Teria como mostrar o código para melhor visualização?
Sim, eu estou usando um conversor usb-serial.
segue abaixo o código:
public void abrirPorta(String porta, Integer baudrate, Integer timeout) throws NoSuchPortException,
PortInUseException,
UnsupportedCommOperationException,
IOException,
TooManyListenersException{
this.porta = porta;
CommPortIdentifier cp = CommPortIdentifier.getPortIdentifier(porta);
portaSerial = (SerialPort)cp.open(this.getClass().getName(), timeout);
portaSerial.setSerialPortParams(baudrate,
portaSerial.DATABITS_8,
portaSerial.STOPBITS_1,
portaSerial.PARITY_NONE);
LerDados();
}
public void enviarUmaString(String msg) throws IOException,
InterruptedException{
saida = portaSerial.getOutputStream();
saida.write(msg.getBytes());
// Thread.sleep(100);
saida.flush();
// saida.close();
}
public void LerDados() throws IOException,
TooManyListenersException{
entrada = portaSerial.getInputStream();
portaSerial.addEventListener(this);
portaSerial.notifyOnDataAvailable(true);
threadLeitura = new Thread(this);
threadLeitura.start();
}
public void serialEvent(SerialPortEvent spe) {
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.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
msgRecebida = "";
byte[] bufferLeitura = new byte[193];
try {
Thread.sleep(500);
while (entrada.available() > 0) {
//node bytes fornece a qtde de bytes recebidos
nodeBytes = entrada.read(bufferLeitura);
msgRecebida = msgRecebida.concat(new String(bufferLeitura).substring(0, nodeBytes));
}
observer.HasDataAvailable();
} catch (Exception ex) {
System.out.println("Erro durante a leitura: " + ex);
}
break;
}
}
Sim, eu estou usando um conversor usb-serial.segue abaixo o código:
public void abrirPorta(String porta, Integer baudrate, Integer timeout) throws NoSuchPortException, PortInUseException, UnsupportedCommOperationException, IOException, TooManyListenersException{ this.porta = porta; CommPortIdentifier cp = CommPortIdentifier.getPortIdentifier(porta); portaSerial = (SerialPort)cp.open(this.getClass().getName(), timeout); portaSerial.setSerialPortParams(baudrate, portaSerial.DATABITS_8, portaSerial.STOPBITS_1, portaSerial.PARITY_NONE); LerDados(); } public void enviarUmaString(String msg) throws IOException, InterruptedException{ saida = portaSerial.getOutputStream(); saida.write(msg.getBytes()); // Thread.sleep(100); saida.flush(); // saida.close(); } public void LerDados() throws IOException, TooManyListenersException{ entrada = portaSerial.getInputStream(); portaSerial.addEventListener(this); portaSerial.notifyOnDataAvailable(true); threadLeitura = new Thread(this); threadLeitura.start(); } public void serialEvent(SerialPortEvent spe) { 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.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; case SerialPortEvent.DATA_AVAILABLE: msgRecebida = ""; byte[] bufferLeitura = new byte[193]; try { Thread.sleep(500); while (entrada.available() > 0) { //node bytes fornece a qtde de bytes recebidos nodeBytes = entrada.read(bufferLeitura); msgRecebida = msgRecebida.concat(new String(bufferLeitura).substring(0, nodeBytes)); } observer.HasDataAvailable(); } catch (Exception ex) { System.out.println("Erro durante a leitura: " + ex); } break; } }
Coloca então aquele código que te falei.