Boa tarde, estou com um problema ao abrir porta serial ela retorna “Port Busy” como porta ocupada. uso o Postman para enviar os parametros. so preciso que retorne um valor de peso como uma string.
package com.alvorada.weight.api;
import java.util.HashMap;
// import com.fazecast.jSerialComm.SerialPort;
// import java.io.InputStream;
// import java.io.OutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
// import com.fazecast.jSerialComm.*;
import jssc.SerialPort;
import jssc.SerialPortException;
@RestController
@RequestMapping("/readweight")
public class SerialPortController {
@GetMapping()
public HashMap<String, String> listar(@RequestParam("typebal") String typeBal, @RequestParam("dtr") Boolean dtr,
@RequestParam("rts") Boolean rts, @RequestParam("readbuffer") Integer readBuffer,
@RequestParam("receivedbytes") Integer receivedBytes, @RequestParam("port") String port,
@RequestParam("baudrate") Integer baudRate, @RequestParam("databits") Integer dataBits,
@RequestParam("parity") Integer parity, @RequestParam("stopbits") Integer stopBits,
@RequestParam("stringlength") Integer stringLenght) {
String result = "";
// result = "typebal = " + typeBal + " dtr=" + dtr.toString() + " rts=" + rts.toString() + " readbuffer="
// + readBuffer.toString() + " receivedbytes=" + receivedBytes.toString() + " port=" + port
// + " baudrate=" + baudRate + " databits=" + dataBits + " parity=" + parity + " stopbits=" + stopBits;
result = connectPort(dtr, rts, readBuffer, receivedBytes, port, baudRate, dataBits, parity, stopBits,
stringLenght);
//HashMap<String, String>
//@test
//result = “8&)p’1234567891011”;
HashMap<String, String> mapRet = new HashMap<>();
mapRet.put(“peso”, result);
return mapRet;
}
private String connectPort(Boolean dtr, Boolean rts, Integer readBuffer, Integer receivedBytes, String port,
Integer baudRate, Integer dataBits, Integer parity, Integer stopBits, Integer stringLenght) {
SerialPort serialPort = new SerialPort(port);
String saida = "";
if (stringLenght == null) {
stringLenght = 20;
}
try {
// if (serialPort.isOpened ()) {
// serialPort.removeEventListener();
// serialPort.closePort ();
// }
if (serialPort != null && serialPort.isOpened ()) {
// serialPort.purgePort (1);
// serialPort.purgePort (2);
serialPort.closePort ();
}
serialPort.openPort();
serialPort.setParams(baudRate, dataBits, stopBits, parity);
serialPort.setDTR(dtr);
serialPort.setRTS(rts);
// while (closePort <= 1) {
// String informacao = serialPort.readString(stringLenght);
// closePort++;
// saida = saida + informacao;
// }
String informacao = serialPort.readString();
saida = informacao.substring(0, stringLenght.intValue());
serialPort.closePort();
} catch (SerialPortException e) {
// System.out.println(e);
// e.printStackTrace();
return e.getMessage();
}
return saida;
}
}