0

I had to control a Hardware made in C who receive unsigned bytes for control. in order to debug these commands i created some JavaFX interface.

I'm having trouble because the hardware is only responding to the fist command i send. To turn On for example i have to start the app, send de ON command and restart to send it again. If i don't restart the app the Equipment does not respond to the other commands.

package interfaceautomacaojavafx;

import java.lang.Integer;
import java.io.*;
import java.net.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import java.math.BigInteger;
import java.net.ConnectException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ResourceBundle;


public class FXMLDocumentController implements Initializable {

private Socket socket;            
private DataOutputStream dataOut;
private DataInputStream dataIn;
private String commandStr;
private char command[];
private String hostname;
private int portNumber;

@FXML
private Label lblIP, lblIPMask, lblGateway, lblPort, lblConnectionStatus,
        lblPingStatus;
@FXML
private Button btnPing, btnSend;

@FXML
private TextField txtIP0, txtIP1, txtIP2, txtIP3,
        txtIPMask0, txtIPMask1, txtIPMask2, txtIPMask3,
        txtGateway0, txtGateway1, txtGateway2, txtGateway3,
        txtPort, txtSendCommand ;

@FXML
private TextArea txaCommandHistory;

@FXML
private Circle circleConnection;



@Override
public void initialize(URL url, ResourceBundle rb) {
    System.out.println("App initialized");
}  

@FXML
private void buttonPingHalndler( ActionEvent event ){
    System.out.println("Button Ping clicked!");
    lblPingStatus.setText("Btn. Ping Clicked");
}

@FXML
private void buttonClearTxaHistoryHandler( ActionEvent event ){
    txaCommandHistory.clear();
}

@FXML
private void buttonSendCommandhandler( ActionEvent event ) {

    btnSend.setDisable(true);
    System.out.println("=== Button SendCommand clicked! ===");
    lblConnectionStatus.setText("Conexão iniciada...");
    circleConnection.setFill(Color.DODGERBLUE);

    hostname = txtIP0.getText();
    System.out.println("Hostname: " + hostname);

    portNumber = Integer.parseInt(txtPort.getText());
    System.out.println("Port: " + portNumber);


    byte buffer[]  = { 0x02, 0x0C, 0x06, 0x01, (0x81 - 256), 
        (0x81 - 256), (0x80 - 256), (0x80 - 256), (0x80 - 256), (0x80 -256), (0x80 - 256), (0x80 - 256) };

    System.out.println("buffer: " + Arrays.toString(buffer));
    lblConnectionStatus.setText("Tentanto conexão...");

    try {
        socket = new Socket( hostname, portNumber );            
        dataOut = new DataOutputStream(socket.getOutputStream());

        System.out.println("Objetos de conexão instanciados (Conexão aceita).");

        dataOut.write(buffer);

        txaCommandHistory.appendText( Arrays.toString( buffer ) );
        txaCommandHistory.appendText("\n");
        lblConnectionStatus.setText("Comando enviado.");
        circleConnection.setFill(Color.LIGHTGREEN);


        dataIn = new DataInputStream(socket.getInputStream());

        while( dataIn.available() > 0 ) {
            txaCommandHistory.appendText( dataIn.readUTF() );
            txaCommandHistory.appendText("\n");
         }

        txaCommandHistory.setVisible(true);
        txaCommandHistory.setDisable(false);

        dataIn.close();
        dataOut.close();
        socket.close();            

    } catch ( ConnectException ce ) {
        lblConnectionStatus.setText("Conexão rejeitada...");
        circleConnection.setFill(Color.LIGHTGRAY);
        ce.printStackTrace();

    } catch ( EOFException eofe ) {
        lblConnectionStatus.setText("Erro de conexão.");
        eofe.printStackTrace();
        System.out.println("EndOfFile Exception ocurred.");            

    } catch( UnknownHostException uhe ) {
        lblConnectionStatus.setText("Host desconhecido.");
        System.out.println("Host unknown: " + uhe.getMessage());

    } catch ( IOException ioe ) { 
        lblConnectionStatus.setText("Erro de conexão...");
        System.out.println("---- IOException ocurred. ----");
        ioe.printStackTrace();
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ioe);

    } finally { 
        System.out.println("--- Bloco Finally: ---");
        lblConnectionStatus.setText("Conexão Encerrada...");
        btnSend.setDisable(false);
        circleConnection.setFill(Color.LIGHTGRAY);
    }
}

@FXML
private void buttonSendOnCommandHalndler( ActionEvent event ){

    btnSend.setDisable(true);
    System.out.println("=== Button SendOnCommand clicked! ===");
    lblConnectionStatus.setText("Conexão iniciada...");
    circleConnection.setFill(Color.DODGERBLUE);

    hostname = txtIP0.getText();
    System.out.println("Hostname: " + hostname);

    portNumber = Integer.parseInt(txtPort.getText());
    System.out.println("Port: " + portNumber);


    byte buffer[]  = { 0x02, 0x04, 0x01, 0x01 };        

    System.out.println("buffer: " + Arrays.toString(buffer));

    try {
        socket = new Socket( hostname, portNumber );            
        dataOut = new DataOutputStream(socket.getOutputStream());
        dataIn = new DataInputStream(socket.getInputStream());

        dataOut.flush();
        dataOut.write(buffer);
        dataOut.flush();

        lblConnectionStatus.setText("Comando enviado.");
        circleConnection.setFill(Color.LIGHTGREEN);
        String txaMessage = Arrays.toString(buffer);

        txaCommandHistory.setVisible(true);
        txaCommandHistory.setDisable(false);
        txaCommandHistory.appendText( txaMessage );
        txaCommandHistory.appendText("\n");

        dataIn.close();
        dataOut.close();
        socket.close();            

    } catch ( ConnectException ce ) {
        lblConnectionStatus.setText("Conexão rejeitada...");
        circleConnection.setFill(Color.LIGHTGRAY);
        ce.printStackTrace();

    } catch ( EOFException eofe ) {
        lblConnectionStatus.setText("Erro de conexão.");
        eofe.printStackTrace();
        System.out.println("EndOfFile Exception ocurred.");            

    } catch( UnknownHostException uhe ) {
        lblConnectionStatus.setText("Host desconhecido.");
        System.out.println("Host unknown: " + uhe.getMessage());

    } catch ( IOException ioe ) { 
        lblConnectionStatus.setText("Erro de conexão...");
        System.out.println("---- IOException ocurred. ----");
        ioe.printStackTrace();
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ioe);

    } finally { 
        System.out.println("--- Bloco Finally: ---");
        lblConnectionStatus.setText("Conexão Encerrada...");
        btnSend.setDisable(false);
        circleConnection.setFill(Color.LIGHTGRAY);
    }
}

@FXML
private void buttonSendOffCommandHalndler( ActionEvent event ){

    btnSend.setDisable(true);
    System.out.println("=== Button SendOffCommand clicked! ===");
    lblConnectionStatus.setText("Conexão iniciada...");
    circleConnection.setFill(Color.DODGERBLUE);

    hostname = txtIP0.getText();
    System.out.println("Hostname: " + hostname);

    portNumber = Integer.parseInt(txtPort.getText());
    System.out.println("Port: " + portNumber);


    byte buffer[]  = { 0x02, 0x04, 0x01, 0x00 };        

    System.out.println("buffer: " + Arrays.toString(buffer));

    try {
        socket = new Socket( hostname, portNumber );            
        dataOut = new DataOutputStream(socket.getOutputStream());
        dataIn = new DataInputStream(socket.getInputStream());

        dataOut.write(buffer);
        dataOut.flush();

        lblConnectionStatus.setText("Comando enviado.");
        circleConnection.setFill(Color.LIGHTGREEN);
        String txaMessage = Arrays.toString(buffer);

        txaCommandHistory.setVisible(true);
        txaCommandHistory.setDisable(false);
        txaCommandHistory.appendText( txaMessage );
        txaCommandHistory.appendText("\n");



    } catch ( ConnectException ce ) {
        lblConnectionStatus.setText("Conexão rejeitada...");
        circleConnection.setFill(Color.LIGHTGRAY);
        ce.printStackTrace();

    } catch ( EOFException eofe ) {
        lblConnectionStatus.setText("Erro de conexão.");
        eofe.printStackTrace();
        System.out.println("EndOfFile Exception ocurred.");            

    } catch( UnknownHostException uhe ) {
        lblConnectionStatus.setText("Host desconhecido.");
        System.out.println("Host unknown: " + uhe.getMessage());

    } catch ( IOException ioe ) { 
        lblConnectionStatus.setText("Erro de conexão...");
        System.out.println("---- IOException ocurred. ----");
        ioe.printStackTrace();
        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ioe);

    } finally { 

        try {
            dataIn.close();
            dataOut.close();
            socket.close(); 
        } catch ( IOException ioe){
            ioe.printStackTrace();
        }

        System.out.println("--- Bloco Finally: ---");
        lblConnectionStatus.setText("Conexão Encerrada...");
        btnSend.setDisable(false);
        circleConnection.setFill(Color.LIGHTGRAY);
    }
 }
}

Interface: Interface JavaFX

Additional info:

-When i click the buttons i see the communication flowing in Wireshark.

-I have three buttons so i've added the .flush() method in one of them but it makes no diference.

-I changed the .close() methods to finally block in one of them but it also makes no diference.

Wireshark command: Wireshark command

Henrique
  • 35
  • 2
  • 8
  • It's possible the bits are flipped as you go from JAVA to C, maybe you'll need to account for this. – ZeldaZach Jun 21 '17 at 14:49
  • But in the image is possible to se that the commands are the same always ( if i'm seeing right ). Why that could happen afer the first comand? The buffer sended is local, so they are destroied after the action. – Henrique Jun 21 '17 at 15:04
  • 2
    You said that you have looked at the network traffic using Wireshark. What was the difference between the second "ON" command and the "ON" command after restart? Have you tries to execute your app twice and execute "ON" in both of them (just to see if the problem is really on JAVA side). – Robert Jun 21 '17 at 15:19
  • In general your question is unanswerable without a proper specification of the device protocol. However you should not be calling `readUTF()`, and it is very difficult to believe you aren't getting an error from it. The only thing that `readUTF()` can read is a string written by `DataOutputStream.writeUTF()`, and no device is going to give you that. And `available()` is not a test for end of stream. – user207421 Jun 21 '17 at 17:37
  • 1
    And why aren't you reading responses for all commands? And why the different ways of handling the close? They should all be in the finally block. And, finally, why are you using a new socket per command at all? Instead if keeping the same connection open? – user207421 Jun 21 '17 at 17:50
  • @Robert The sended bits are the same for all comands, so it not seems to be a Java problem right?!. What is making me sad is it exists a Android app that send this commands but this problem not occurs in the Android app. I don't saw the commands before in Wiresharik because i'm getting used to it, and i just found how to see the data =O. Thanks Robert. – Henrique Jun 22 '17 at 13:46
  • @EJP Everything i did is for test. I don't know how the eqp receives the command, i only had the commands and what they do. So for example, i know that the 0x02 0x04 0x01 0x01 has to turn the eqp on, i don't know yet if the eqp sends response for commands so for that i put the DataInputStream.readUTF() willing that this method converts evething that should sended back as strings. Resuming, i have to use .read() instead? – Henrique Jun 22 '17 at 14:11
  • @EJP I made a command per conection because it is made to test if the the eqp is receiveing corectly them, so the sofware is made to send only one command per conection. So only recaptuling, the question is if something that i did or didn't could make the software change the commands after the first command? That's because i'm learnig how to use flow of data interchange. Thanks a lot for the advices. – Henrique Jun 22 '17 at 14:12

1 Answers1

0

After analyze the Wireshark data i will close this question because seems that the Java application is sending the commands correctly.

Henrique
  • 35
  • 2
  • 8