iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >java如何利用socket通信实现Modbus-RTU通信协议
  • 570
分享到

java如何利用socket通信实现Modbus-RTU通信协议

2023-06-14 13:06:18 570人浏览 泡泡鱼
摘要

小编给大家分享一下java如何利用Socket通信实现Modbus-RTU通信协议,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!ModbusModbus是一种串行

小编给大家分享一下java如何利用Socket通信实现Modbus-RTU通信协议,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

Modbus

Modbus是一种串行通信协议。Modbus 一个工业上常用的通讯协议、一种通讯约定。Modbus协议包括RTU、ASCII、tcp。其中MODBUS-RTU最常用,比较简单,在单片机上很容易实现。

简单分析Modbus-RTU报文

37 03 10 3F 80 00 00 00 00 00 00 3F 80 00 00 40 40 00 00 24 dd(十六进制)
37:从站地址 ,03:功能码,10:读取的字节数,24 dd:crc校验码。其它就是传送的数据。

4G DTU(ZHC4013)

ZHC4012是一款全网通七模4G DTU,支持2G/3G/4G信号透明传输。支持工业RS232/485等接口,直接连接设备传输。这个硬件是我项目中实践过的,该设备可以通过4G运营商网络与远程服务器进行数据通信。具体操作可以到官网联系客服。设备官网4G DTU(ZHC4013)

项目支持多个4G DTU设备数据上传,支持控制指定4G DTU设备。

附一个测试版本的源代码

用到的工具

package hdo.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class ModBusUtils {    public static byte[] readInputStream(InputStream inputStream) throws IOException {        byte[] buffer = new byte[1024];        int len = 0;        ByteArrayOutputStream bos = new ByteArrayOutputStream();        if((len = inputStream.read(buffer)) != -1) {            bos.write(buffer, 0, len);        }        bos.close();        return bos.toByteArray();    }        public static char byteToASCLL(byte b){        return (char) b;    }           public static String bytes2HexString(byte[] b) {        String r = "";        for (int i = 0; i < b.length; i++) {            String hex = Integer.toHexString(b[i] & 0xFF);            if (hex.length() == 1) {                hex = '0' + hex;            }            r += hex.toUpperCase()+" ";        }        return r;    }  public static String getCRC3(byte[] data) {        byte[] crc16_h = {                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41,                (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x01, (byte) 0xC0, (byte) 0x80, (byte) 0x41, (byte) 0x00, (byte) 0xC1, (byte) 0x81, (byte) 0x40        };        byte[] crc16_l = {                (byte) 0x00, (byte) 0xC0, (byte) 0xC1, (byte) 0x01, (byte) 0xC3, (byte) 0x03, (byte) 0x02, (byte) 0xC2, (byte) 0xC6, (byte) 0x06, (byte) 0x07, (byte) 0xC7, (byte) 0x05, (byte) 0xC5, (byte) 0xC4, (byte) 0x04,                (byte) 0xCC, (byte) 0x0C, (byte) 0x0D, (byte) 0xCD, (byte) 0x0F, (byte) 0xCF, (byte) 0xCE, (byte) 0x0E, (byte) 0x0A, (byte) 0xCA, (byte) 0xCB, (byte) 0x0B, (byte) 0xC9, (byte) 0x09, (byte) 0x08, (byte) 0xC8,                (byte) 0xD8, (byte) 0x18, (byte) 0x19, (byte) 0xD9, (byte) 0x1B, (byte) 0xDB, (byte) 0xDA, (byte) 0x1A, (byte) 0x1E, (byte) 0xDE, (byte) 0xDF, (byte) 0x1F, (byte) 0xDD, (byte) 0x1D, (byte) 0x1C, (byte) 0xDC,                (byte) 0x14, (byte) 0xD4, (byte) 0xD5, (byte) 0x15, (byte) 0xD7, (byte) 0x17, (byte) 0x16, (byte) 0xD6, (byte) 0xD2, (byte) 0x12, (byte) 0x13, (byte) 0xD3, (byte) 0x11, (byte) 0xD1, (byte) 0xD0, (byte) 0x10,                (byte) 0xF0, (byte) 0x30, (byte) 0x31, (byte) 0xF1, (byte) 0x33, (byte) 0xF3, (byte) 0xF2, (byte) 0x32, (byte) 0x36, (byte) 0xF6, (byte) 0xF7, (byte) 0x37, (byte) 0xF5, (byte) 0x35, (byte) 0x34, (byte) 0xF4,                (byte) 0x3C, (byte) 0xFC, (byte) 0xFD, (byte) 0x3D, (byte) 0xFF, (byte) 0x3F, (byte) 0x3E, (byte) 0xFE, (byte) 0xFA, (byte) 0x3A, (byte) 0x3B, (byte) 0xFB, (byte) 0x39, (byte) 0xF9, (byte) 0xF8, (byte) 0x38,                (byte) 0x28, (byte) 0xE8, (byte) 0xE9, (byte) 0x29, (byte) 0xEB, (byte) 0x2B, (byte) 0x2A, (byte) 0xEA, (byte) 0xEE, (byte) 0x2E, (byte) 0x2F, (byte) 0xEF, (byte) 0x2D, (byte) 0xED, (byte) 0xEC, (byte) 0x2C,                (byte) 0xE4, (byte) 0x24, (byte) 0x25, (byte) 0xE5, (byte) 0x27, (byte) 0xE7, (byte) 0xE6, (byte) 0x26, (byte) 0x22, (byte) 0xE2, (byte) 0xE3, (byte) 0x23, (byte) 0xE1, (byte) 0x21, (byte) 0x20, (byte) 0xE0,                (byte) 0xA0, (byte) 0x60, (byte) 0x61, (byte) 0xA1, (byte) 0x63, (byte) 0xA3, (byte) 0xA2, (byte) 0x62, (byte) 0x66, (byte) 0xA6, (byte) 0xA7, (byte) 0x67, (byte) 0xA5, (byte) 0x65, (byte) 0x64, (byte) 0xA4,                (byte) 0x6C, (byte) 0xAC, (byte) 0xAD, (byte) 0x6D, (byte) 0xAF, (byte) 0x6F, (byte) 0x6E, (byte) 0xAE, (byte) 0xAA, (byte) 0x6A, (byte) 0x6B, (byte) 0xAB, (byte) 0x69, (byte) 0xA9, (byte) 0xA8, (byte) 0x68,                (byte) 0x78, (byte) 0xB8, (byte) 0xB9, (byte) 0x79, (byte) 0xBB, (byte) 0x7B, (byte) 0x7A, (byte) 0xBA, (byte) 0xBE, (byte) 0x7E, (byte) 0x7F, (byte) 0xBF, (byte) 0x7D, (byte) 0xBD, (byte) 0xBC, (byte) 0x7C,                (byte) 0xB4, (byte) 0x74, (byte) 0x75, (byte) 0xB5, (byte) 0x77, (byte) 0xB7, (byte) 0xB6, (byte) 0x76, (byte) 0x72, (byte) 0xB2, (byte) 0xB3, (byte) 0x73, (byte) 0xB1, (byte) 0x71, (byte) 0x70, (byte) 0xB0,                (byte) 0x50, (byte) 0x90, (byte) 0x91, (byte) 0x51, (byte) 0x93, (byte) 0x53, (byte) 0x52, (byte) 0x92, (byte) 0x96, (byte) 0x56, (byte) 0x57, (byte) 0x97, (byte) 0x55, (byte) 0x95, (byte) 0x94, (byte) 0x54,                (byte) 0x9C, (byte) 0x5C, (byte) 0x5D, (byte) 0x9D, (byte) 0x5F, (byte) 0x9F, (byte) 0x9E, (byte) 0x5E, (byte) 0x5A, (byte) 0x9A, (byte) 0x9B, (byte) 0x5B, (byte) 0x99, (byte) 0x59, (byte) 0x58, (byte) 0x98,                (byte) 0x88, (byte) 0x48, (byte) 0x49, (byte) 0x89, (byte) 0x4B, (byte) 0x8B, (byte) 0x8A, (byte) 0x4A, (byte) 0x4E, (byte) 0x8E, (byte) 0x8F, (byte) 0x4F, (byte) 0x8D, (byte) 0x4D, (byte) 0x4C, (byte) 0x8C,                (byte) 0x44, (byte) 0x84, (byte) 0x85, (byte) 0x45, (byte) 0x87, (byte) 0x47, (byte) 0x46, (byte) 0x86, (byte) 0x82, (byte) 0x42, (byte) 0x43, (byte) 0x83, (byte) 0x41, (byte) 0x81, (byte) 0x80, (byte) 0x40        };        int crc = 0x0000ffff;        int ucCRCHi = 0x00ff;        int ucCRCLo = 0x00ff;        int iIndex;        for (int i = 0; i < data.length; ++i) {            iIndex = (ucCRCLo ^ data[i]) & 0x00ff;            ucCRCLo = ucCRCHi ^ crc16_h[iIndex];            ucCRCHi = crc16_l[iIndex];        }        crc = ((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff) & 0xffff;        //高低位互换,输出符合相关工具对Modbus CRC16的运算        crc = ((crc & 0xFF00) >> 8) | ((crc & 0x00FF) << 8);        return String.fORMat("%04X", crc);    }       public static byte[] hexStringToByteArray(String hexString) {        hexString = hexString.replaceAll(" ", "");        int len = hexString.length();        byte[] bytes = new byte[len / 2];        for (int i = 0; i < len; i += 2) {            // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character                    .digit(hexString.charAt(i + 1), 16));        }        return bytes;    }}

服务端代码

package hdo.utils.thread;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;import java.util.Map;import hdo.utils.ModBusUtils;public class SocketServer extends Thread { public Map<String,Socket> clientMap = new HashMap<>();  public void run() {        try {            //创建socket服务端            @SuppressWarnings("resource")   ServerSocket server = new ServerSocket(9666);            while (true){                System.out.println("建立监听");                Socket client = server.accept();                System.out.println("建立连接");                //获取注册包                InputStream is = client.getInputStream();                byte[] bytes = ModBusUtils.readInputStream(is);                String str = "";                for(int i=0;i<bytes.length;i++) {                    if(i==0){                        System.out.print("十进制:"+bytes[i]+" ");                    }else {                        System.out.print(bytes[i]+" ");                    }                    str=str+ModBusUtils.byteToASCLL(bytes[i]);                }                System.out.println("十六进制:"+ModBusUtils.bytes2HexString(bytes));                System.out.println("注册包:"+str);                //添加客户端                clientMap.put(str,client);                Service service = new Service(clientMap);                service.start();            }  } catch (Exception e) {   e.printStackTrace();  } } }
package com.ruoyi.test;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.util.*;public class Service extends Thread {    private Map<String, Socket> clientMap = new HashMap<>();    public Service(){    }    public Service(Map<String,Socket> clientMap){        this.clientMap=clientMap;    }    public void run() {        while(true) {            try {                for(Map.Entry<String, Socket> entry : clientMap.entrySet()) {                    //注册包 节点编号                    String reGIStrationPacket = entry.geTKEy();                    Socket socket = entry.getValue();                    assert socket != null;                    InputStream is = socket.getInputStream();                    OutputStream os = socket.getOutputStream();                    if (is.available() != 0) {                        byte[] bytes = ModBusUtils.readInputStream(is);                        String str = ModBusUtils.bytes2HexString(bytes);                        System.out.print("十六进制:" + str);                        //截取对应的数据                        List<Float> floatList = new ArrayList<>();                        String[] arr = str.split(" ");                        if (arr.length > 20) {                            for (int i = 7; i < arr.length - 2; i += 4) {                                String a = arr[i] + arr[i + 1] + arr[i + 2] + arr[i + 3];                                Float f = Float.intBitsToFloat(Integer.valueOf(a, 16));                                System.out.print("浮点数:" + f);                                floatList.add(f);                            }                        }                        System.out.println("节点编号:" + registrationPacket);                        byte[] by = new byte[19];                        by[0] = 55;                        by[1] = 3;                        by[2] = 16;                        by[3] = 63;                        by[4] = -128;                        by[5] = 0;                        by[6] = 0;                        by[7] = 0;                        by[8] = 0;                        by[9] = 0;                        by[10] = 0;                        by[11] = 0;                        by[12] = 0;                        by[13] = 0;                        by[14] = 0;                        String hexStr = Integer.toHexString(Float.floatToIntBits(Float.parseFloat("2.0")));                        byte[] b = ModBusUtils.hexStringToByteArray(hexStr);                        by[15] = b[0];                        by[16] = b[1];                        by[17] = b[2];                        by[18] = b[3];                        //crc3校验                        String crc = ModBusUtils.getCRC3(by);                        byte[] crcByte = ModBusUtils.hexStringToByteArray(crc);                        byte[] data = new byte[21];                        data[19] = crcByte[0];                        data[20] = crcByte[1];                        for (int i = 0; i < by.length; i++) {                            data[i] = by[i];                        }                        os.write(data);                        System.out.println(registrationPacket + ":写入数据:" + Arrays.toString(data));                    } else {                        continue;                    }                }            } catch (Exception e) {                e.printStackTrace();            }            try {                Thread.sleep(5000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}

以上是“java如何利用socket通信实现Modbus-RTU通信协议”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: java如何利用socket通信实现Modbus-RTU通信协议

本文链接: https://www.lsjlt.com/news/271151.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-14
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-14
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-14
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-14
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-14
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-14
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-14
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-14
    golang 数据库备份 mysql git 标准库
  • 如何在 Golang 中优雅地处理错误?
    在 go 中,优雅处理错误包括:使用 error 类型;使用 errors 包函数和类型;自定义错误类型;遵循错误处理模式,包括关闭资源、检查错误、打印错误信息和处理或返回错误。 在 ...
    99+
    2024-05-14
    golang 错误处理
  • 如何构建 Golang RESTful API,并使用中间件进行身份验证?
    本文介绍了如何构建 golang restful api。首先,通过导入必要的库、定义数据模型和创建路由来构建 restful api。其次,使用 go-chi/chigot 和 go-...
    99+
    2024-05-14
    golang git
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作