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...