资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

手机端ModbusTcp主站工具及程序-创新互联

Modbus协议在工业现场使用比较频繁,为方便手机调试,在android上做了个ModbusTcp主站功能。
在这里插入图片描述
1:使用说明
作为主站,需要连接从站,因此需要配置从站IP、端口以及协议中从站地址。
连接成功后这些配置就不能修改。
偏移是需要读取或者写入的起始地址。
长度读取或者写入的总长度。
后边的长度按键是,为写入准备输入数据区数据。一般步骤先设定需要写入的长度,当然根据实际情况可以同时设置偏移量;在点击长度,就会在写入区生成需要写入长度项目,然后设定需要写入的值。
在这里插入图片描述
读取按键,按设定的地址和长度以及功能读取器从站数据,读回来的数据,如果是是3、4功能号的,选择了十六进制,就按十六进制展示;否则按十进制展示。
写入按键,从偏移量作为基地址写入从站。
清理按键是清理写入区内输入的内容,清理不会写入到从站。
“----->”传递按键,在有些情况下,可以直接把读取的实际传递到写入区,然后修改部分数据后,直接写入到从站。这样就不需要逐条输入,方便操作。
2:程序说明
页面上在读取展示区和写入输入区是两个ListView组件。读区的item是两个TextView,写入输入区是一个TextView和一个EditText。
ModbusTcp功能直接使用的是Modbus4Android的jar包。核心功能代码如下

创新互联公司于2013年成立,是专业互联网技术服务公司,拥有项目网站设计、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元陇南做网站,已为上家服务,为陇南各地企业和个人服务,联系电话:18982081108
private void modbusInit(String ip, int port) {
        ModbusReq.getInstance().setParam(new ModbusParam()
                        .setHost(ip)
                        .setPort(port)
                        .setEncapsulated(false)
                        .setKeepAlive(true)
                        .setTimeout(2000)
                        .setRetries(0))
                .init(new OnRequestBack() {
                    @Override
                    public void onSuccess(String s) {
                        Log.d(TAG, "onSuccess " + s);
                        Intent i = new Intent("notifyText");
                        i.putExtra("act", "100");
                        i.putExtra("status", true);
                        i.putExtra("Message", "连接成功");
                        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d(TAG, "onFailed " + msg);
                        Intent i = new Intent("notifyText");
                        i.putExtra("act", "0");
                        i.putExtra("status", false);
                        i.putExtra("Message", "连接失败");
                        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
                    }
                });
    }

    public void readCoil(int slaveid, int start, int len) {
        ModbusReq.getInstance().readCoil(new OnRequestBack() {
            @Override
            public void onSuccess(boolean[] booleen) {
                Log.d(TAG, "readCoil onSuccess " + Arrays.toString(booleen));
                Intent i = new Intent("notifyText");
                i.putExtra("act", "1");
                i.putExtra("status", true);
                i.putExtra("Message", Arrays.toString(booleen));
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }

            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "readCoil onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "1");
                i.putExtra("status", false);
                i.putExtra("Message", "读取线圈失败");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, len);
    }

    public void readDiscreteInput(int slaveid, int start, int len) {

        ModbusReq.getInstance().readDiscreteInput(new OnRequestBack() {
            @Override
            public void onSuccess(boolean[] booleen) {
                Log.d(TAG, "readDiscreteInput onSuccess " + Arrays.toString(booleen));
                Intent i = new Intent("notifyText");
                i.putExtra("act", "2");
                i.putExtra("status", true);
                i.putExtra("Message", Arrays.toString(booleen));
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "readDiscreteInput onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "2");
                i.putExtra("status", false);
                i.putExtra("Message", "读取线圈失败");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, len);
    }

    public void readHoldingRegisters(int slaveid, int start, int len) {
        //readHoldingRegisters
        ModbusReq.getInstance().readHoldingRegisters(new OnRequestBack() {
            @Override
            public void onSuccess(short[] data) {
                Log.d(TAG, "readHoldingRegisters onSuccess " + Arrays.toString(data));
                Intent i = new Intent("notifyText");
                i.putExtra("act", "3");
                i.putExtra("status", true);
                i.putExtra("Message", Arrays.toString(data));
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }

            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "readHoldingRegisters onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "3");
                i.putExtra("status", false);
                i.putExtra("Message", "读取线圈失败");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, len);
    }

    public void readInputRegisters(int slaveid, int start, int len) {
        ModbusReq.getInstance().readInputRegisters(new OnRequestBack() {
            @Override
            public void onSuccess(short[] data) {
                Log.d(TAG, "readInputRegisters onSuccess " + Arrays.toString(data));
                Intent i = new Intent("notifyText");
                i.putExtra("act", "4");
                i.putExtra("status", true);
                i.putExtra("Message", Arrays.toString(data));
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "readInputRegisters onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "4");
                i.putExtra("status", false);
                i.putExtra("Message", "读取线圈失败");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, len);
    }

    public void writeCoil(int slaveid, int start, boolean value) {
        ModbusReq.getInstance().writeCoil(new OnRequestBack() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "writeCoil onSuccess " + s);
            }

            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "writeCoil onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "5");
                i.putExtra("status", false);
                i.putExtra("Message", "msg");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, value);
    }

    public void writeRegister(int slaveid, int start, int value) {
        ModbusReq.getInstance().writeRegister(new OnRequestBack() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "writeRegister onSuccess " + s);
            }

            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "writeRegister onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "6");
                i.putExtra("status", false);
                i.putExtra("Message", "msg");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, value);
    }

    public void writeRegisters(int slaveid, int start, short[] value) {
        ModbusReq.getInstance().writeRegisters(new OnRequestBack() {
            @Override
            public void onSuccess(String s) {
                Log.e(TAG, "writeRegisters onSuccess " + s);
            }

            @Override
            public void onFailed(String msg) {
                Log.e(TAG, "writeRegisters onFailed " + msg);
                Intent i = new Intent("notifyText");
                i.putExtra("act", "16");
                i.putExtra("status", false);
                i.putExtra("Message", "msg");
                LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
            }
        }, slaveid, start, value);
    }

读函数把成功和失败都用广播播放出去。
写只把失败广播出去。
3:测试效果
程序功能基本满足现场测试需要,方便随时联调。在测试过程中出现程序闪退,经跟踪,闪退异常原因“java.lang.IndexOutOfBoundsException: Index: 99, Size: 10”,是ListView的adapter在更新数据时,老数据操作未完成引起。经修改程序后,未出现闪退情况。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站标题:手机端ModbusTcp主站工具及程序-创新互联
地址分享:http://cdkjz.cn/article/cdeshd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220