Home Page Forums Community International Develop OpenWebNet Java-Listener doesnt work

OpenWebNet
Java-Listener doesnt work

This topic contains 6 replies, has 2 voices, and was last updated by  RoStief 06/02/2019 at 17:37.

3
6
371
  • Verified

    Hi,

    I am trying to write a small Java-app which is just listening to the BUS (Ethernet Connection @MH200N) and shows the current situation.

    Sending commands to the system is done without any problem, but to permanently get a current Status – I failed (as always sending a status update command will slow down the whole system with response times for >5sec.)

    Here is my Java Code and I hope anyone can help me:

     
    <pre style=”background-color: #ffffff; color: #000000; font-family: ‘Courier New’; font-size: 9,0pt;”><span style=”color: #000080; font-weight: bold;”>package </span>com.example.adamantos.homeautomatisation;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.io.BufferedReader;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.io.BufferedWriter;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.io.InputStreamReader;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.io.OutputStreamWriter;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.io.PrintWriter;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.net.Socket;
    <span style=”color: #000080; font-weight: bold;”>import </span>java.util.concurrent.Callable;

    <span style=”color: #000080; font-weight: bold;”>public class </span>Connect_StatusAbfrage <span style=”color: #000080; font-weight: bold;”>implements </span>Callable<String> {
    String <span style=”color: #660e7a; font-weight: bold;”>inMsg</span>=<span style=”color: #000080; font-weight: bold;”>new </span>String(<span style=”color: #008000; font-weight: bold;”>”ABC”</span>);
    <span style=”color: #000080; font-weight: bold;”>private static </span>Socket <span style=”color: #660e7a; font-style: italic;”>clientSocket</span>;
    <span style=”color: #000080; font-weight: bold;”>private static </span>BufferedReader <span style=”color: #660e7a; font-style: italic;”>bufferedReader</span>;
    <span style=”color: #000080; font-weight: bold;”>private static </span>String <span style=”color: #660e7a; font-style: italic;”>inputLine</span>;
    <span style=”color: #000080; font-weight: bold;”>private static </span>PrintWriter <span style=”color: #660e7a; font-style: italic;”>out2</span>;
    BufferedReader <span style=”color: #660e7a; font-weight: bold;”>input</span>;

    <span style=”color: #808000;”>@Override
    </span> <span style=”color: #000080; font-weight: bold;”>public </span>String call() <span style=”color: #000080; font-weight: bold;”>throws </span>Exception{
    <span style=”color: #808080; font-style: italic;”>//protected Void doInBackground(Integer… params) {
    </span> <span style=”color: #000080; font-weight: bold;”>try </span>{
    <span style=”color: #808080; font-style: italic;”>//System.out.println(“Statusabfrage gestartet”);
    </span> Thread.<span style=”font-style: italic;”>sleep</span>(<span style=”color: #0000ff;”>5000</span>);
    Socket s = <span style=”color: #000080; font-weight: bold;”>new </span>Socket(<span style=”color: #008000; font-weight: bold;”>”192.168.0.15″</span>, <span style=”color: #0000ff;”>20000</span>);
    s.setKeepAlive(<span style=”color: #000080; font-weight: bold;”>true</span>);
    s.setTcpNoDelay(<span style=”color: #000080; font-weight: bold;”>true</span>);
    BufferedReader in = <span style=”color: #000080; font-weight: bold;”>new </span>BufferedReader(<span style=”color: #000080; font-weight: bold;”>new </span>InputStreamReader(s.getInputStream()));
    BufferedWriter out = <span style=”color: #000080; font-weight: bold;”>new </span>BufferedWriter(<span style=”color: #000080; font-weight: bold;”>new </span>OutputStreamWriter(s.getOutputStream()));
    String outfirst = <span style=”color: #008000; font-weight: bold;”>”*99*1##” </span>+ System.<span style=”font-style: italic;”>getProperty</span>(<span style=”color: #008000; font-weight: bold;”>”line.separator”</span>);
    System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #008000; font-weight: bold;”>”Received First :”</span>+ in.readLine() + System.<span style=”font-style: italic;”>getProperty</span>(<span style=”color: #008000; font-weight: bold;”>”line.separator”</span>));
    System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #008000; font-weight: bold;”>”Write: ” </span>+ outfirst);
    out.write(outfirst);
    out.flush();
    System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #008000; font-weight: bold;”>”Received Confirmation : ” </span>+ in.readLine()+ System.<span style=”font-style: italic;”>getProperty</span>(<span style=”color: #008000; font-weight: bold;”>”line.separator”</span>) );

    <span style=”color: #000080; font-weight: bold;”>while </span>((<span style=”color: #660e7a; font-weight: bold;”>inMsg </span>= in.readLine()) != <span style=”color: #000080; font-weight: bold;”>null</span>)
    System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #660e7a; font-weight: bold;”>inMsg</span>); <span style=”color: #808080; font-style: italic;”>// read from server and print it.
    </span> System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #008000; font-weight: bold;”>”2:” </span>+ <span style=”color: #660e7a; font-weight: bold;”>inMsg</span>);
    out.close();
    in.close();
    s.close();
    }
    <span style=”color: #000080; font-weight: bold;”>catch</span>(Exception b)
    {
    System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #008000; font-weight: bold;”>”Exception in Status Abfrage: ” </span>+ b.toString());
    }
    System.<span style=”color: #660e7a; font-weight: bold; font-style: italic;”>out</span>.println(<span style=”color: #008000; font-weight: bold;”>”ENDE”</span>);
    <span style=”color: #000080; font-weight: bold;”>return null</span>;
    }
    }

    ….
    the first ack is send by the system (for correct tcp/ip)
    but after sending the <span style=”color: #008000; font-weight: bold;”>*99*1## i dont get any answer expect null (-1)
    </span>
    2019-01-17 21:20:20.208 I: Received First :*#*1##
    2019-01-17 21:20:20.208 I: Write: *99*1##
    2019-01-17 21:20:20.209 I: Received Confirmation : null
    2019-01-17 21:20:20.209 I: 2:null
    2019-01-17 21:20:20.210 I: ENDE

    Hope anybody can help me.
    And another Question: is it possible to connect 1 session (socket) with a listening session and one with a command session? or do I have to implement one class which handles both and changes from one session to the other?

    Many thanks for any answer,
    Michael

     




    User
    quaxi16
    Role
    Other
    4 posts
    Verified

    Once again the code to be readable:

    package com.example.adamantos.homeautomatisation;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.concurrent.Callable;

    public class Connect_StatusAbfrage implements Callable<String> {
    String inMsg=new String(“ABC”);
    private static Socket clientSocket;
    private static BufferedReader bufferedReader;
    private static String inputLine;
    private static PrintWriter out2;
    BufferedReader input;

    @override
    public String call() throws Exception{
    //protected Void doInBackground(Integer… params) {
    try {
    //System.out.println(“Statusabfrage gestartet”);
    Thread.sleep(5000);
    Socket s = new Socket(“192.168.0.15”, 20000);
    s.setKeepAlive(true);
    s.setTcpNoDelay(true);
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
    String outfirst = “*99*1##” + System.getProperty(“line.separator”);
    System.out.println(“Received First :”+ in.readLine() + System.getProperty(“line.separator”));
    System.out.println(“Write: ” + outfirst);
    out.write(outfirst);
    out.flush();
    System.out.println(“Received Confirmation : ” + in.readLine()+ System.getProperty(“line.separator”) );

    while ((inMsg = in.readLine()) != null)
    System.out.println(inMsg); // read from server and print it.
    System.out.println(“2:” + inMsg);
    out.close();
    in.close();
    s.close();
    }
    catch(Exception b)
    {
    System.out.println(“Exception in Status Abfrage: ” + b.toString());
    }
    System.out.println(“ENDE”);
    return null;
    }
    }




    User
    quaxi16
    Role
    Other
    4 posts
    Verified

    You are using “<span style=”color: #48595f; font-family: DinPro, sans-serif;”>readLine()” when reading from the socket after the connection. I wouldn’t be so sure that you are receiving a carriage return after a message, so you should just read the buffer, and check for the termination sign “##”</span>

    Also it shouldn’t be a problem with parallel having a command and an event connection ( i did that in c++ and it didn’t cause any problems)

    regards

    Roman




    User
    RoStief
    Role
    Software Developer
    11 posts
    Verified

    Hello Roman,

    i tried your solution with a bit of success, but he is still not listening 🙁

    I get confirmation of the tcp-connection as well the confirmation as sending the command.
    then he does Nothing anymore..

    package com.example.adamantos.homeautomatisation;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.util.concurrent.Callable;

    public class Connect_StatusAbfrage implements Callable<String> {
    String inMsg=new String(“ABC”);
    private static Socket clientSocket;
    private static BufferedReader bufferedReader;
    private static String inputLine;
    private static PrintWriter out2;
    BufferedReader input;

    @override
    public String call() throws Exception{
    //protected Void doInBackground(Integer… params) {
    try {
    //System.out.println(“Statusabfrage gestartet”);
    Thread.sleep(5000);
    Socket s = new Socket(“192.168.0.15”, 20000);
    //s.setKeepAlive(true);
    //s.setTcpNoDelay(true);
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

    boolean a = true;
    boolean b = false;
    int character;
    StringBuilder data = new StringBuilder();

    while ((character = in.read()) != -5000000)
    {
    data.append((char) character);

    if (data.toString().contains(“##”))
    {
    System.out.println(data);
    data.delete(0,data.length());
    System.out.println(“Data cleared: ” + data);
    b = true;
    }

    if (a&&b)
    {
    String outfirst = “*99*1##” + System.getProperty(“line.separator”);
    out.write(outfirst);
    out.flush();
    a = false;
    System.out.println(“Write: ” + outfirst);
    }
    Thread.sleep(10);
    }

    System.out.println(“END of data : “+ data);

    }
    catch(Exception b)
    {
    System.out.println(“Exception in Status Abfrage: ” + b.toString());
    }
    System.out.println(“ENDE”);
    return null;
    }
    }

    No exception, no “Ende”, no “end of data”… just Nothing …
    Output:
    2019-01-18 20:25:10.573 I: *#*1##
    2019-01-18 20:25:10.573 I: Data cleared:
    2019-01-18 20:25:10.575 I: Write: *99*1##
    2019-01-18 20:25:10.637 I: *#*1##
    2019-01-18 20:25:10.637 I: Data cleared:

    Thanks a lot !!!
    Michael




    User
    quaxi16
    Role
    Other
    4 posts
    Verified

    Hello,

    my guess would be that the BufferedReader is the problem ( because the data read doesn’t exceed the buffer size, and the stream remains open until the socket is closed), therefore i would recommend changing “<span style=”color: #48595f; font-family: DinPro, sans-serif;”>BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));”</span>

    to

    <span style=”color: #48595f; font-family: DinPro, sans-serif;”>InputStream in = s.getInputStream();</span>

    and also check in the loop: <span style=”color: #48595f; font-family: DinPro, sans-serif;”>while ((character = in.read()) != -1)</span>

    regards

    Roman




    User
    RoStief
    Role
    Software Developer
    11 posts
    Verified

    Hi,

    sigh, doesnt work either 🙁
    Seems it receives always -1 or some undefined Chars whole the time (if you delete the !-1 and let him write every you get rectangles in Output).
    So Output msg works, recevied of ack works, sending monitor-Signal works, receiving ack, then instantly -1.

    2019-01-26 11:10:43.586 I: *#*1##
    2019-01-26 11:10:43.586 I: Data cleared:
    2019-01-26 11:10:43.588 I: Write: *99*1##
    2019-01-26 11:10:43.650 I: *#*1##
    2019-01-26 11:10:43.650 I: Data cleared:
    2019-01-26 11:10:43.660 I: END of data :
    2019-01-26 11:10:43.660 I: ENDE

    @override
    public String call() {

    Einmal_Abfrage();

    try {
    Thread.sleep(10000);
    Socket s = new Socket(“192.168.0.15”, 20000);
    s.setKeepAlive(true);
    s.setTcpNoDelay(true);

    InputStream in = s.getInputStream();
    OutputStream out = s.getOutputStream();

    boolean a = true;
    boolean b = false;
    int character;
    StringBuilder data = new StringBuilder();

    while ((character = in.read()) != -1)
    {
    data.append((char) character);

    if (data.toString().contains(“##”))
    {
    System.out.println(data);
    data.delete(0,data.length());
    System.out.println(“Data cleared: ” + data);
    b = true;

    }

    if (a&&b)
    {
    //Listener Mode
    String outfirst = “*99*1##” + System.getProperty(“line.separator”);
    out.write(outfirst.getBytes());
    out.flush();
    s.shutdownOutput();
    a = false;
    System.out.println(“Write: ” + outfirst);
    }
    Thread.sleep(10);
    }

    System.out.println(“END of data : “+ data);

    }
    catch(Exception b)
    {
    System.out.println(“Exception in Status Abfrage: ” + b.toString());
    }
    System.out.println(“ENDE”);
    return null;
    }




    User
    quaxi16
    Role
    Other
    4 posts
    Verified

    Hello, i just had again a look at your code, and the problem could be that you are sending the wrong command to the Gateway.

    You are sending “*99*1##”, but to create an Event connection for listening, you need to send “<span style=”color: #48595f; font-family: DinPro, sans-serif;”>*99*9##”</span>

    regards

    Roman




    User
    RoStief
    Role
    Software Developer
    11 posts
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.