java 如何发送接收Omron Fins协议
目前GITHUB 我找到了发送FIns协议 但是我如果用传统的Socket接收 解析有误
Omron FINS协议是一种基于TCP/IP的协议,它用于与Omron PLC进行通信。在Java中,可以使用Java Socket API来实现与Omron PLC的通信。
发送FINS协议可以通过以下代码实现:
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class SendFins {
public static void main(String[] args) throws IOException {
// 连接PLC
Socket socket = new Socket("10.10.10.10", 9600);
OutputStream outputStream = socket.getOutputStream();
// FINS头
byte[] finsHeader = new byte[] { (byte) 0x80, 0x00, 0x02, 0x00, 0x00, 0x0C };
// FINS命令
byte[] finsCommand = new byte[] { 0x01, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00 };
// 发送FINS命令
outputStream.write(finsHeader);
outputStream.write(finsCommand);
// 关闭连接
outputStream.close();
socket.close();
}
}
其中,需要将10.10.10.10替换为实际的PLC的IP地址,9600替换为实际的端口号。
接收FINS协议需要对接收到的字节数组进行解析,可以使用Omron的FINS协议库来实现。以下是一个使用Omron的FINS协议库来接收FINS协议的示例代码:
import omron.fins.*; // 导入Omron的FINS协议库
import java.net.ServerSocket;
import java.net.Socket;
public class ReceiveFins {
public static void main(String[] args) throws Exception {
// 创建Socket服务端
ServerSocket serverSocket = new ServerSocket(9600);
Socket socket = serverSocket.accept();
// 创建FINS协议对象
Fins fins = new Fins();
// 读取FINS头
byte[] finsHeader = new byte[8];
socket.getInputStream().read(finsHeader);
// 解析FINS头
FinsHeader header = fins.parseFinsHeader(finsHeader);
// 读取FINS命令
byte[] finsCommand = new byte[header.getLength()];
socket.getInputStream().read(finsCommand);
// 解析FINS命令
FinsIoMemoryAreaReadResponse response = (FinsIoMemoryAreaReadResponse) fins.parseCommand(finsCommand);
// 输出读取到的数据
System.out.println(response.getData()[0]);
// 关闭连接
socket.close();
serverSocket.close();
}
}
在运行接收FINS协议的代码之前,需要先运行发送FINS协议的代码,以向PLC发送数据。在运行接收FINS协议的代码时,需要将9600替换为实际的端口号。
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class OmronFinsClient {
private String host;
private int port;
private int localNetId;
private int localNodeId;
private int remoteNetId;
private int remoteNodeId;
public OmronFinsClient(String host, int port, int localNetId, int localNodeId, int remoteNetId, int remoteNodeId) {
this.host = host;
this.port = port;
this.localNetId = localNetId;
this.localNodeId = localNodeId;
this.remoteNetId = remoteNetId;
this.remoteNodeId = remoteNodeId;
}
public byte[] readMemory(int address, int count) throws IOException {
byte[] request = createReadMemoryRequest(address, count);
byte[] response = sendRequest(request);
if (response != null) {
return extractMemoryData(response);
} else {
return null;
}
}
public void writeMemory(int address, byte[] data) throws IOException {
byte[] request = createWriteMemoryRequest(address, data);
sendRequest(request);
}
private byte[] createReadMemoryRequest(int address, int count) {
byte[] request = new byte[12];
request[0] = (byte) localNetId;
request[1] = (byte) localNodeId;
request[2] = (byte) remoteNetId;
request[3] = (byte) remoteNodeId;
request[8] = 0x01;
request[9] = (byte) ((address >> 8) & 0xFF);
request[10] = (byte) (address & 0xFF);
request[11] = (byte) count;
return request;
}
private byte[] createWriteMemoryRequest(int address, byte[] data) {
byte[] request = new byte[12 + data.length];
request[0] = (byte) localNetId;
request[1] = (byte) localNodeId;
request[2] = (byte) remoteNetId;
request[3] = (byte) remoteNodeId;
request[8] = 0x02;
request[9] = (byte) ((address >> 8) & 0xFF);
request[10] = (byte) (address & 0xFF);
request[11] = (byte) data.length;
System.arraycopy(data, 0, request, 12, data.length);
return request;
}
private byte[] sendRequest(byte[] request) throws IOException {
Socket socket = null;
InputStream in = null;
OutputStream out = null;
byte[] response = null;
try {
socket = new Socket(host, port);
in = socket.getInputStream();
out = socket.getOutputStream();
out.write(request);
out.flush();
response = new byte[1024];
int len = in.read(response);
response = Arrays.copyOf(response, len);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (socket != null) {
socket.close();
}
}
return response;
}
private byte[] extractMemoryData(byte[] response) {
if (response[8] == 0x01 && response[9] == 0x00 && response[10] == 0x00) {
int count = response[11];
byte[] data = new byte[count];
System.arraycopy(response, 12, data, 0, count);
return data;
} else {
return null;
}
}
}
博主试一下这一段代码,是通过Socket编程实现的