java modbus tcp 通讯 androidapp远程访问plc

根据modbus通讯协议我把底层程序已经写好,直接配置就可以读出你想要的数据,可以是short,int,float等

实现代码如下

#####https://item.taobao.com/item.htm?spm=a230r.7195193.1997079397.32.4eed49d05YNS1z&id=541946979235&abbucket=19

package com.tminfo.wang.unit;


import java.io.IOException;
import java.util.Vector;


import com.tminfo.plcscaner.modbus.ModbusException;
import com.tminfo.plcscaner.modbus.facade.ModbusTCPMaster;
import com.tminfo.plcscaner.modbus.procimg.Register;


public class BaseUnit implements Runnable {


/**
* @param args
*/
ModbusTCPMaster ModbusTCPMaster1=null;
// static Register Registers[]=null;
static Register Registersw[]=new Register[10];
// Register Registersw[]=null;
static Register Registers[]=null;
static Register Registersw[]=new Register[10];
static  Vector valueint=new Vector() ;
public static void setValueint(Vector valueint) {
BaseUnit.valueint = valueint;
}
static BaseUnit BaseUnit=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
BaseUnit=new BaseUnit();
Thread t=new Thread(BaseUnit);
t.start();

}
@Override
public void run() {
// TODO Auto-generated method stub
while (true)
{
this.readdata();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public void readdata()
{
ModbusTCPMaster1=new ModbusTCPMaster("127.0.0.1",502,1);
try {
ModbusTCPMaster1.connect();
try {
valueint.clear();
//读取0到20个字。。读20个int
Registers=ModbusTCPMaster1.readMultipleRegisters(100,Registersw.length,1);
for (int i=0;i {
valueint.add(Registers[i].getValue());
// System.out.println(valueint.get(i));
}

} catch (ModbusException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
ModbusTCPMaster1.disconnect();
}
}

public static Register[] getRegistersw() {
return Registersw;
}
public static void setRegistersw(Register[] registersw) {
Registersw = registersw;
}
public static Vector getValueint() {
return valueint;
}


}

 

你可能感兴趣的:(java)