ttyacm0 - Puerto serie Java Problema
serial port permissions linux (2)
Estoy tratando de encender / apagar el relé, pero hasta ahora no podía tener éxito. Probé el programa Coolterm para ver si los controladores estaban instalados correctamente y sí funcionó y pude activarlo / desactivarlo a través de GUI. Sin embargo, tengo un problema para enviar el comando a través de java para encender el relé.
Parámetros de comunicación: 8 datos, 1 parada, sin paridad Velocidad en baudios: 9600
Comandos: comando OFF: FF 01 00 (HEX) o 255 1 0 (DEC)
Comando ENCENDIDO: FF 01 01 (HEX) o 255 1 1 (DEC)
Mi código está abajo:
public class Application {
InputStream in;
OutputStream out;
String dataHex = "FF 01 01";
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
System.out.println(portIdentifier);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
System.out.println(commPort);
SerialPort serialPort = (SerialPort) commPort;
System.out.println(serialPort);
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
this.in = serialPort.getInputStream();
this.out = serialPort.getOutputStream();
System.out.println(dataHex.getBytes());
out.write(dataHex.getBytes());
System.out.println("end");
}
InputStream getIn() {
return this.in;
}
OutputStream getOut() {
return this.out;
}
public static void main(String args[]) throws QTException, FileNotFoundException, IOException {
Application app = new Application();
try {
app.connect("/dev/tty.usbserial-A400953X");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Gracias por adelantado...
Intenta seguir el código:
byte[] array = { -1, 1, 1 };
out.write(array);
en lugar de
out.write(dataHex.getBytes());
Su programa está actualizando el String "FF 01 01" que tiene 8 bytes de longitud en una codificación compatible con ASCII. Esto parece bastante inusual, supongo que su gadget realmente espera 3 bytes como en el siguiente código:
byte[] data = new byte[] {(byte)0xFF, (byte)0x01, (byte)0x01};
out.write(data);