Jump to content
Ardivaba

[JAVA] Unable to get response from PLC

Recommended Posts

Hello.

I am unable to get any information from PLC using Java currently.

It does manage to connect to it though, as while the application is running and socket is connected, no other software can communicate with plc.

I have spent a better part of my day trying to get my Java application to communicate with PLC but without much.

I also haven't found any information regarding Java Unitronics driver so i guess there isn't one?

I would appreciate any kind of help.

Code below.

Cheers,

Ardi Vaba

import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;

public class asd {
   private static final String HOST = "192.168.1.74";
   private static final int PORT = 20256;

   public static void main(String[] args) throws IOException
   {
       Socket socket = new Socket(HOST, PORT);

       //I still don't underastand what [0] and [1] are for...
       byte[] tcpHeader = new byte[6];
       tcpHeader[0] = 1; //???
       tcpHeader[1] = 0; //???
       tcpHeader[2] = 101; //Ascii comm type
       tcpHeader[3] = 0;
       tcpHeader[4] = 14; ///01RE0020051F + CRC = 14
       tcpHeader[5] = 0;

       String message = "/01RE0020051F\n";

       byte[] finalMessage = new byte[message.getBytes("ASCII").length + tcpHeader.length];

       //Let's add messange and hader together...
       ByteBuffer buffer = ByteBuffer.wrap(finalMessage);
       buffer.put(tcpHeader);
       buffer.put(message.getBytes("ASCII"));

       //Let's wait until socket is connected...just to be sure
       while(!socket.isConnected()){}
       System.out.println("Sending ascii message...");
       //Send the message
       socket.getOutputStream().write(finalMessage);

       //Start waiting for response...
       BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
       System.out.println("Starting to wait for response...");
       String response = "";
       while((response = reader.readLine()) != null)
       {
           System.out.println(response);
       }
   }
}

Updated code to more readable format...

Share this post


Link to post
Share on other sites

Make sure that getBytes returns you the right bytes (meaning, not from unicode, but ascii, and that you have exactly the amount of bytes you expect).

The next thing I would check is that the message structure is correct:

* Make sure that the Network ID of the PLC is indeed 1

* Make sure that you are not missing any character in the message

* Make sure that the checksum is correct

You can also connect to the PLC using Serial Port, and then send the string message (without the tcp header) and see that you get response.

Share this post


Link to post
Share on other sites

Thank you for such a quick response.

That gave me confidence that i am not doing something extremely stupid.

I will try to get to the bottom of this.

My goal is to connect PLC with Android smartphones, hence the need to do it using Java.

Needless to say, i am software guy that is somewhat new to automatics world.

If i fail to get this communication to work i'll simply set up .NET server as a middleman and use .NET communications driver but this does add the requirement of a server which is not very good.

Share this post


Link to post
Share on other sites

Ok, now that I look at the documentations, I can help you a little better.

Q: "//I still don't underastand what [0] and [1] are for... "

A: The first 2 bytes are message ID, meaning that when you send a message to the PLC with a message ID 1, then the PLC will reply back with the same ID in the TCP header. It is suggested to change the message id for each message you send. See documentation:

http://www.unitronic...OM Protocol.pdf (Pages 27 and 28)

I see that the string of the message that you are using was taken from the documentation... However, the first problem I see is that your ETX is \n while it should be \r

(This might be the main reason why the PLC doesn't respond).

Also, make sure that the Network ID of the PLC is indeed 1...

Share this post


Link to post
Share on other sites

Hi!,

 

I am currently working on an application for android that effectively change the MB of a plc visio 350

 

I am trying to send tcp packets using very similar code in my android app, trought the pcom protocol to understand the plc, but so far only managed to effectively convey a single message, thereby changing the state of a MB .

 

After this is impossible to establish anew a connection to the plc through the tcp-ip over ethernet network, and must power cycle the plc to again achieve establish connection to it.

 

Even when Im using visilogic on the same network with a tcp(call)  connection mode  on socket 1, prior to sending  packet with the android application, I get a positive response from the plc, but once sent the package with the android application and turn on or off a MB I get no answer from the plc, not even the visilogic, after that I must reset plc to be able to establish connection again.

 

I tried even using the same code that has Ardivaba here with the same results. I think that could be the failure to close the socket in the plc, but not know how to do it correctly. 
 
Please anyone who can help, I really appreciate it
 
ricolinofeliz

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...