I am using jSSC library and I need to implements the RTS signal because I need to conenct to some RS-485 clients so I use this signal for DE/RE of a SN75176. The problem is when I active or deactive the RTS signal, I have a LED between the MAX232 and the SN75176 to indicate if RTS is active or not and it doesn´t seem to work as I need. This is the code:
package jssc_checkclientsandcables;
import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener;
import jssc.SerialPortException;
/**
*
* @author DeLeon
*/
public class JSSC_CheckClientsAndCables {
/**
* @param args the command line arguments
*/
private static int[] vcClients, vcRele;
private static int continuarProg;
private static SerialPort serialPort;
private static SerialPortEvent event;
private static byte[] buffer;
//private static int contadorcin;
private static final int CANT_CLIENTS = 3;
private static final int STREAM_LONG = 4;
public static void main(String[] args) throws InterruptedException {
// TODO code application logic here
serialPort = new SerialPort("COM1");
try {
System.out.println("Port name: " + serialPort.getPortName());
System.out.println("Port opened: " + serialPort.openPort());
System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0));
//System.out.println("Set Flow Control Mode setted: " + serialPort.setFlowControlMode(1 | 2));
//System.out.println("Params setted: " + serialPort.setParams(9600, 8, 1, 0, false, false));
//Preparamos la máscara
//int mask = SerialPort.MASK_RXCHAR;<-<-<-<-< Estos dos estaban ON
//Seteamos el Evento - No me queda muy claro como funciona este método
//serialPort.setEventsMask(mask); -<-<-<-<
System.out.println("Set Flow Control Mode setted: " + serialPort.setFlowControlMode(1 | 2));
System.out.println("Get Flow Control Mode: " + serialPort.getFlowControlMode());
//
//serialPort.addEventListener(new SerialPortReader());
}
catch (SerialPortException ex){
System.out.println(ex);
}
vcClients = new int[CANT_CLIENTS];
vcRele = new int[CANT_CLIENTS];
buffer = new byte[STREAM_LONG];
System.out.println("Primer RTS tiene que ser False");
try{
//Desactivmaos la señal RTS
System.out.println("RTS signal: " + serialPort.setRTS(false));
System.out.println("CTS signal: " + serialPort.isCTS());
//System.out.println("RTS signal: " + serialPort.setRTS(true));
}
catch (SerialPortException ex){
System.out.println("Problemas al desactivar RTS Signal");
}
do{
for(int i = 0; i < CANT_CLIENTS; i++){
System.out.print("\n\nIngrese numero de cliente: ");
vcClients[i] = Consola.readInt();
System.out.print("\n\nIngrese rele que desea activar (A = 1, B = 2, M = 3, N = 4, OFF = 6): ");
vcRele[i] = Consola.readInt();
}
for (int j = 0; j < CANT_CLIENTS; j++){
System.out.println("\n\nEnviando datos al Cliente numero " + vcClients[j]);
System.out.println("Segundo RTS tiene que ser True");
try{
//Ponemos en alto la señal RTS para que el SN75176 active el driver de transmisión
// System.out.println("RTS signal active: " + serialPort.setRTS(true));
// serialPort.setRTS(true);
// serialPort.setRTS(true);
// System.out.println("RTS signal active: " + serialPort.isRTS(true));
// //System.out.println("RTS signal: " + serialPort.setRTS(false));
try{
//Desactivmaos la señal RTS
System.out.println("RTS signal: " + serialPort.setRTS(true));
System.out.println("CTS signal: " + serialPort.isCTS());
//System.out.println("RTS signal: " + serialPort.setRTS(true));
}
catch (SerialPortException ex){
System.out.println("Problemas al desactivar RTS Signal");
}
// Controlar si no se necesita un retardo entre envios al serial <- <- <- <- <-
System.out.println("Paso el segundo RTS (True) y esta por mandar");
serialPort.writeInt(0xa5);//Byte de START
serialPort.writeInt(vcClients[j]);
switch (vcRele[j]){
case 1:{
serialPort.writeInt(0xf1);
break;
}
case 2:{
serialPort.writeInt(0xf2);
break;
}
case 3:{
serialPort.writeInt(0xf3);
break;
}
case 4:{
serialPort.writeInt(0xf4);
break;
}
case 6:{
serialPort.writeInt(0xf6); // Desactivar todos los Clientes
break;
}
default:{
System.out.println("No se enviaron datos validos al cliente " + vcClients[j] + " - Switch-Case = default");
break;
}
}
serialPort.writeInt(vcClients[j] + vcRele[j] + 0xf0);
System.out.println("Termino de mandar");
}
catch (SerialPortException ex){
System.out.println("No se pudo enviar informacion al puerto COM1");
}
System.out.println("\nStream enviado al cliente " + vcClients[j]);
Thread.sleep(800);//Wait 0.8 seconds
// try{
// //Desactivmaos la señal RTS
// System.out.println("RTS signal: " + serialPort.setRTS(false));
// }
// catch (SerialPortException ex){
// System.out.println("Problemas al desactivar RTS Signal");
// }
System.out.println("Esperando respuesta del cliente " + vcClients[j]);
//Esperamos del cliente el valor entero que representa una confirmación, 0xac
do{
//if(event.isRXCHAR()){
//if(event.getEventValue() == 2){
/* MODIFICAR ESTO: Tenemos que recibir 3 caracteres, START+DATA+CHECKSUM */
try{
//Desactivmaos la señal RTS
System.out.println("Tercer RTS tiene que ser False");
//serialPort.setRTS(false);
System.out.println("RTS signal: " + serialPort.setRTS(false));
System.out.println("Paso el tercer RTS false");
}
catch (SerialPortException ex){
System.out.println("Problemas al desactivar RTS Signal");
}
System.out.println("Antes de esperar los datos al buffer de recep");
try {
byte buffer[] = serialPort.readBytes(STREAM_LONG);
}
catch (SerialPortException ex) {
System.out.println(ex);
}
System.out.println("Despues de esperar los datos al buffer de recep");
System.out.println("Primer dato recibido: " + buffer[0]);
System.out.println("Segundo dato recibido: " + buffer[1]);
System.out.println("Tercer dato recibido: " + buffer[2]);
System.out.println("Cuarto dato recibido: " + buffer[3]);
}while( ((buffer[3]) >= 0xf6) && ((buffer[3]) <= 0xf8));
System.out.println("\n\nEl valor del 1º dato recibio es: " + buffer[0]);
System.out.println("El valor del 2º dato recibio es: " + buffer[1]);
System.out.println("El valor del 3º dato recibio es: " + buffer[2]);
System.out.println("El valor del 4º dato recibio es: " + buffer[3]);
//if( (buffer[0]+buffer[1]) == 0x151)
//System.out.println("Comunicación con el cliente correcta" );
System.out.println("Desea continuar? [Si = 1 / No = 2]");
continuarProg = Consola.readInt();
}
}while(continuarProg != 2);
continuarProg = 0;
try{
//Cerramos el puerto COM1
System.out.println("Port closed: " + serialPort.closePort());
}
catch (SerialPortException ex){
System.out.println("Problemas cerrando el COM o desactivando RTS Signal");
}
And this is the output:
run: Port name: COM1 Port opened: true Params setted: true Set Flow Control Mode setted: true Get Flow Control Mode: 3 Primer RTS tiene que ser False RTS signal: false
Ingrese numero de cliente: 1
Ingrese rele que desea activar (A = 1, B = 2, M = 3, N = 4, OFF = 6): 1
Ingrese numero de cliente: 2
Ingrese rele que desea activar (A = 1, B = 2, M = 3, N = 4, OFF = 6): 2
Ingrese numero de cliente: 3
Ingrese rele que desea activar (A = 1, B = 2, M = 3, N = 4, OFF = 6): 3
Enviando datos al Cliente numero 1 Segundo RTS tiene que ser True RTS signal: false Paso el segundo RTS (True) y esta por mandar
BUILD STOPPED (total time: 57 minutes 38 seconds)
What am I doing wrong?
Feel free to ask if some information are missing or sth. is unclear.
Kind regards and thanks in advance.