Java实现欧姆龙FinsTCP协议(推荐用springboot)

依赖

用的是netty实现的,通过上位机开放出来的ip和端口采集欧姆龙PLC的数据,该案例只包括连接PLC加握手:

 <dependency>
       <groupId>io.netty</groupId>
       <artifactId>netty-all</artifactId>
       <version>4.1.20.Final</version>
 </dependency>

FinsTCP协议配置FinsClientConfig


import com.global.common.hex.HexStringUtil;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Fins协议配置
 *
 * @author Administrator
 * @since 1.0.0
 */
public class FinsClientConfig {
    public static ChannelFuture channelFuture;

    public static void finsStart(String ip, int port) throws Exception {
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
        try {
            //创建bootstrap对象,配置参数
            Bootstrap bootstrap = new Bootstrap();
            //设置线程组
            bootstrap.group(eventExecutors)
                    //设置客户端的通道实现类型
                    .channel(NioSocketChannel.class)
                    //使用匿名内部类初始化通道
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            //添加客户端通道的处理器
                            ch.pipeline().addLast(new MyClientHandler());
                        }
                    });
            System.out.println("客户端准备就绪,随时可以起飞~");
            //连接服务端
            channelFuture = bootstrap.connect(ip, port).sync();
            //对通道关闭进行监听
            channelFuture.channel().closeFuture().sync();
        } finally {
            //关闭线程组
            eventExecutors.shutdownGracefully();
        }
    }

    /**
     * 发送数据
     *
     * @param msg 数据
     */
    public static void send(String msg) {
        channelFuture.channel().writeAndFlush(Unpooled.wrappedBuffer(HexStringUtil.hexToByteArray(msg)));
    }
}

Handler类MyClientHandler


import cn.hutool.core.util.HexUtil;
import com.global.common.hex.HexStringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * 业务处理
 *
 * @author Administrator
 * @since 1.0.0
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {

    /**
     * 握手
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //握手
        ctx.writeAndFlush(Unpooled.wrappedBuffer(HexStringUtil.hexToByteArray("46494E530000000C00000000000000000000000D")));

    }

    /**
     * 消息处理
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //接收服务端发送过来的消息
        ByteBuf byteBuf = (ByteBuf) msg;
        byte[] bytes = ByteBufUtil.getBytes(byteBuf);
        System.err.println(HexUtil.encodeHexStr(bytes));

    }
}

下面这个方法可以自己去写一个,比如hutool的HexUtil工具类里就有我这边自己写

 	/**
     * 16进制字符串转化为byte数组
     *
     * @param inHex 要转16进制字节流数组的16进制字符
     * @return 16进制字节流
     */
    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            // 奇数
            hexlen++;
            result = new byte[(hexlen / 2)];
            inHex = "0" + inHex;
        } else {
            // 偶数
            result = new byte[(hexlen / 2)];
        }
        int j = 0;
        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            j++;
        }
        return result;
    }
    
    private static byte hexToByte(String inHex) {
       return (byte) Integer.parseInt(inHex, 16);
    }

总结:

FinsTCP协议走的还是网络通讯,那还是TCP/IP通讯,只是第一次发送必须是握手报文,然后发送读取数据报文
Java的网络通讯框架我这边比较常用的就是 t-io 以及netty

案例源码:

fins_demo

指令的说明介绍请看这个大佬的:

指令介绍

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
欧姆龙FINS/TCP协议是一种用于工业自动化设备之间进行通信的协议。它使用了TCP/IP网络通信协议,具有高可靠性和实时性的特点。在Qt开发中,我们可以使用Qt的网络模块来实现FINS/TCP协议的支持。 首先,我们需要在Qt项目中包含网络模块的头文件,并且链接相关的库文件。然后,我们可以通过使用QTcpSocket类来建立与欧姆龙设备的TCP连接。QTcpSocket类提供了处理TCP套接字的方法和信号。 接下来,我们需要了解FINS/TCP协议的通信规则和数据包格式。FINS/TCP协议使用特定的命令和数据来进行设备之间的通信。在Qt中,我们可以通过QTcpSocket类的write()方法将命令和数据发送给设备。我们还可以使用QTcpSocket类的read()方法来接收设备返回的响应数据。 在开发过程中,我们还可以使用Qt的信号和槽机制来实现与设备之间的实时通信。当设备发送数据到Qt应用程序时,我们可以通过QTcpSocket类的readyRead()信号来接收并处理数据。 此外,我们还可以使用Qt的界面开发工具,如Qt Designer,来设计和创建界面。我们可以在界面中添加按钮和文本框等控件,以便用户与设备进行交互。 综上所述,通过Qt的网络模块和相关功能,我们可以方便地实现欧姆龙FINS/TCP协议的支持。这使得开发人员能够轻松地与欧姆龙设备进行通信,并开发出功能强大的工业自动化应用程序。
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值