public class FactoryBuilder {
static ModbusMaster master;
public FactoryBuilder(String type) {
String ip=" ";
int port=502;
int refreshTime = 10000;
String comm = " ";
int baud = 0;
int dataBits = 8;
int stopBits = 1;
int parity =0;
try {
/* Load Properties from file */
System.out.println("Loading communication settings...");
Properties props = new Properties();
props.load(new FileInputStream("communication.properties"));
ip=props.getProperty("IP");
port=Integer.parseInt(props.getProperty("PORT"));
comm=props.getProperty("COMM");
baud=Integer.parseInt(props.getProperty(" "));
dataBits=Integer.parseInt(props.getProperty("DATABITS"));
stopBits=Integer.parseInt(props.getProperty("STOPBITS"));
parity=Integer.parseInt(props.getProperty("PARITY"));
}catch(IOException ex){
System.out.println("Problem opening communication.properties file");
}
if(type.equals("ip")){
System.out.println("Connecting over IP to ("+ip+":"+port+")");
ModbusFactory factory = new ModbusFactory();
IpParameters params = new IpParameters();
params.setHost(ip);
params.setPort(port);
//params.setEncapsulated(true);
master = factory.createTcpMaster(params, true);
}else{
System.out.println("Connecting over serial port ("+comm+")");
ModbusFactory factory = new ModbusFactory();
SerialParameters params = new SerialParameters();
params.setCommPortId(comm);
params.setBaudRate(baud);
params.setDataBits(dataBits);
params.setStopBits(stopBits);
params.setParity(parity);
master = factory.createRtuMaster(params);
}
}//FactoryBuilder()
public ModbusMaster getMaster(){
return this.master;
}//getMaster()
}//Class FactoryBuilder
package com.client;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.code.RegisterRange;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.ModbusRequest;
import com.serotonin.modbus4j.msg.ReadInputRegistersRequest;
import com.serotonin.modbus4j.msg.ReadInputRegistersResponse;
import com.serotonin.modbus4j.msg.WriteCoilRequest;
import com.serotonin.modbus4j.msg.WriteRegisterRequest;
public class TestMyServlet {
static ModbusMaster master;
static int node;
static boolean color;
static boolean modsenseRunning = false;
public TestMyServlet() {
String version = "3.51";
System.out.println("ModSense v" + version);
}
public static void main(String[] args) throws Exception {
TestMyServlet clientRequest = new TestMyServlet();
clientRequest.machine();
Thread th = new Thread();
do {
System.out.print("\nChecking Process Status...");
System.out.println(modsenseRunning);
if (!modsenseRunning) {
clientRequest.machine();
}
th.sleep(100000);
} while (true);
}
public void machine() throws Exception {
// TestMyServlet clientRequest = new TestMyServlet();
modsenseRunning = true;
/* Values to be set by properties file */
int refreshTime = 100000;
String dbAddress = "";
String dbUser = "";
String dbPassword = "";
String dbName = "";
String connection = "";
int offsetBegin = 0;
int offsetLength = 100;
node = 1;
color = true;
String serial = "";
String pollOnly = "";
Thread th = new Thread();
try {
System.out.println("Loading modsense settings...");
Properties props = new Properties();
props.load(new FileInputStream("modsense.properties"));
refreshTime = Integer.parseInt(props.getProperty("REFRESH"));
connection = props.getProperty("CONNECTION");
color = Boolean.parseBoolean(props.getProperty("COLOR"));
node = Integer.parseInt(props.getProperty("UNITID"));
dbName = props.getProperty("DBNAME");
dbAddress = props.getProperty("DBADDRESS");
dbUser = props.getProperty("DBUSER");
dbPassword = props.getProperty("DBPASSWORD");
serial = props.getProperty("SERIAL");
pollOnly = props.getProperty("POLLONLY");
offsetBegin = Integer.parseInt(props.getProperty("OFFSETBEGIN"));
offsetLength = Integer.parseInt(props.getProperty("OFFSETLENGTH"));
System.out.println("Establishing connection to device...");
FactoryBuilder fb = new FactoryBuilder(connection);
master = fb.getMaster();
master.setTimeout(1000);
master.setRetries(0);
master.init();
} catch (FileNotFoundException e) {
System.out.println("ERROR: Could not find properties file...");
modsenseRunning = false;
master.destroy();
e.printStackTrace();
} catch (IOException e) {
modsenseRunning = false;
master.destroy();
e.printStackTrace();
} catch (ModbusInitException e) {
System.out
.println("ERROR: Could not establish connection to device...");
modsenseRunning = false;
master.destroy();
e.printStackTrace();
}
do {
System.out.println("Beginning process...");
List queryAllData = new ArrayList();
List updatedData = new ArrayList();
query_update(queryAllData, updatedData, dbAddress, dbName, dbUser,
dbPassword, serial);
response_alldata(queryAllData, updatedData, dbAddress, dbName,
dbUser, dbPassword, serial);
th.sleep(refreshTime);
} while (modsenseRunning);
}
public void query_update(List queryAllData, List updatedData,
String dbAddress, String dbName, String dbUser, String dbPassword,
String serial) {
Document document = DocumentHelper.createDocument();
Element rootElement = document.addElement("request");
/*
* Element typeElement = rootElement.addElement("type");
* typeElement.setText("query");
*/
// 以下部分从附件取得
Element dbname = rootElement.addElement("dbname");
dbname.setText(dbName);
Element dbuser = rootElement.addElement("dbuser");
dbuser.setText(dbUser);
Element dbpassword = rootElement.addElement("dbpassword");
dbpassword.setText(dbPassword);
Element serialElement = rootElement.addElement("serial");
serialElement.setText(serial);
HttpURLConnection http;
System.out
.println("----------------发送查询请求的plc/sending query update request----------------");
System.out.println(document.asXML());
try {
URL urls = new URL("http://" + dbAddress + "/customplatform/test");
http = (HttpURLConnection) urls.openConnection();
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
DataOutputStream out = new DataOutputStream(http.getOutputStream());
XMLWriter writer = new XMLWriter(out);
writer.write(document);
writer.close();
out.flush();
System.out
.println("----------------接收服务器反馈的更新数据和所有数据地址/receiving updating data and all address----------------");
InputStream resp_in = http.getInputStream();
SAXReader saxReader = new SAXReader();
InputStreamReader resp_strInStream = new InputStreamReader(resp_in,
"UTF-8");
Document resp_document = saxReader.read(resp_strInStream);
Element resp_root = resp_document.getRootElement();
Iterator resp_lv = resp_root.elementIterator("update-item");
Element resp_el = null;
while (resp_lv.hasNext()) {// 循环获得需要更新的address和value:
resp_el = (Element) resp_lv.next();
System.out.println(resp_el.elementText("address") + ":"
+ resp_el.elementText("value"));
// if mb if mi if ml, 不同类型的address 在这里分类
if ((resp_el.elementText("address")).substring(0, 2).equals(
"MB")) {
if (writeCoilToAddress(resp_el.elementText("address"),
resp_el.elementText("value"))) {
updatedData.add(resp_el.elementText("address")); // 如果修改成功,把数据放入修改成功的数组里面
}
} else if ((resp_el.elementText("address")).substring(0, 2)
.equals("MI")) {
if (writeHoldingToAddress(resp_el.elementText("address"),
resp_el.elementText("value"))) {
updatedData.add(resp_el.elementText("address"));
}
} else if ((resp_el.elementText("address")).substring(0, 2)
.equals("ML")) {
if (writeRegisterToAddress(resp_el.elementText("address"),
resp_el.elementText("value"))) {
updatedData.add(resp_el.elementText("address"));
}
}
}
Iterator resp_lv2 = resp_root.elementIterator("query-item");
Element resp_el2 = null;
while (resp_lv2.hasNext()) {
resp_el2 = (Element) resp_lv2.next();
queryAllData.add(resp_el2.elementText("address"));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void response_alldata(List queryAllData, List updatedData,
String dbAddress, String dbName, String dbUser, String dbPassword,
String serial) {
Document document = DocumentHelper.createDocument();
Element rootElement = document.addElement("feedback");
Element dbname = rootElement.addElement("dbname");
dbname.setText(dbName);
Element dbuser = rootElement.addElement("dbuser");
dbuser.setText(dbUser);
Element dbpassword = rootElement.addElement("dbpassword");
dbpassword.setText(dbPassword);
Element serialElement = rootElement.addElement("serial");
serialElement.setText(serial);
Map resp_alldata = new HashMap();
for (int i = 0; i < queryAllData.size(); i++) {
// String value=getAddressValue(queryAllData[k]);
// String value = "x";// 从Plc读出来的
String value = getAddressValue((String) queryAllData.get(i));
resp_alldata.put(queryAllData.get(i), value);
}
Iterator iter = resp_alldata.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String address = (String) entry.getKey();
String val = (String) entry.getValue();
Element element = rootElement.addElement("response-item");
Element addressElement = element.addElement("address");
Element valueElement = element.addElement("value");
addressElement.setText(address);
valueElement.setText(val);
}
for (int i = 0; i < updatedData.size(); i++) {
Element element = rootElement.addElement("updated-item");
Element addressElement = element.addElement("address");
addressElement.setText((String) updatedData.get(i));
}
System.out
.println("--------------发送所有plc数据的xml/sending all address value--------------");
System.out.println(document.asXML());
HttpURLConnection http;
try {
URL urls = new URL("http://" + dbAddress + "/customplatform/test");
http = (HttpURLConnection) urls.openConnection();
http.setDoOutput(true);
http.setDoInput(true);
http.setRequestMethod("POST");
DataOutputStream out = new DataOutputStream(http.getOutputStream());
XMLWriter writer = new XMLWriter(out);
writer.write(document);
writer.close();
out.flush();
String resp = "";
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(http.getInputStream(), "UTF-8"));
String str = breader.readLine();
while (str != null) {
resp += str;
str = breader.readLine();
}
System.out.println("resp=" + resp);
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean writeCoilToAddress(String address, String value) {
int addressOffset = Integer.parseInt(address.substring(2));
boolean boolValue = Boolean.parseBoolean(value);
return writeMI(addressOffset, boolValue);
}
public static boolean writeMI(int offset, boolean value) {
try {
ModbusRequest req = new WriteCoilRequest(node, offset, value);
master.send(req);
return true;
} catch (ModbusTransportException ex) {
return false;
}
}
public static boolean writeHoldingToAddress(String address, String value) {
int addressOffset = Integer.parseInt(address.substring(2));
int intValue = Integer.parseInt(value);
return writeHolding(addressOffset, intValue);
}
public static boolean writeRegisterToAddress(String address, String value) {
int addressOffset = Integer.parseInt(address.substring(2));
int intValue = Integer.parseInt(value);
return writeRegister(addressOffset, intValue);
}
public static boolean writeHolding(int offset, int value) {
try {
ModbusRequest req = new WriteRegisterRequest(node, offset, value);
master.send(req);
return true;
} catch (ModbusTransportException ex) {
return false;
}
}
public static boolean writeRegister(int offset, int value) {
if (color) {
offset = offset + 0x7000;
} else {
offset = 5100 + (offset * 2);
}
try {
ModbusRequest req = new WriteRegisterRequest(node, offset, value);
master.send(req);
return true;
} catch (ModbusTransportException ex) {
return false;
}
}
public static String getAddressValue(String address) {
int addressOffset = Integer.parseInt(address.substring(2));
if (address.substring(0, 2).equals("MB")) {
return readCoil(addressOffset);
} else if (address.substring(0, 2).equals("MI")) {
return readHoldingRequest(addressOffset);
} else if (address.substring(0, 2).equals("ML")) {
return readRegisterRequest(addressOffset);
} else {
return null;
}
}
public static String readCoil(int offset) {
try {
Object ob = master.getValue(node, RegisterRange.COIL_STATUS,
offset, 1);
return ob.toString();
} catch (ModbusTransportException ex) {
System.out.println("Problem reading " + offset);
return "coilerror";
} catch (ErrorResponseException ex) {
System.out.println("Error response in " + offset);
return "coilerror";
}
}
public static String readHoldingRequest(int offset) {
try {
Object ob = master.getValue(node, RegisterRange.HOLDING_REGISTER,
offset, DataType.TWO_BYTE_INT_SIGNED);
return ob.toString();
} catch (ModbusTransportException ex) {
System.out.println("Problem reading " + offset);
return "holdingerror";
} catch (ErrorResponseException ex) {
System.out.println("Error response in " + offset);
return "holdingerror";
}
}
public static String readRegisterRequest(int offset) {
if (color) {
offset = offset + 0x7000;
} else {
offset = 5100 + (offset * 2);
}
try {
ModbusRequest req = new ReadInputRegistersRequest(node, offset, 2);
ReadInputRegistersResponse rep = (ReadInputRegistersResponse) master
.send(req);
return Short.toString(rep.getShortData()[0]);// +Short.toString(rep.getShortData()[1]);
} catch (ModbusTransportException ex) {
System.out.println("Problem reading long...(" + offset + ")");
return "error";
}
}
}