Newer
Older
pgdsc / src / com / szpg / plc / message / response / ReadMemoryCommandResponse.java
package com.szpg.plc.message.response;

import org.apache.log4j.Logger;

import com.szpg.db.dao.PgHjsbblDao;
import com.szpg.db.dao.impl.PgHjsbblDaoImpl;
import com.szpg.db.data.PgHjsbbl;
import com.szpg.plc.message.CommandResponse;
import com.szpg.plc.util.ByteUtil;
import com.szpg.plc.util.Bytes;

public abstract class ReadMemoryCommandResponse extends CommandResponse {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6832502645108837925L;
	
	private PgHjsbblDao blDao = new PgHjsbblDaoImpl();
	
	private final Logger logger = Logger.getLogger(this.getClass().getName());
	
	/**
	 * 解析温度值
	 * @param zcbh
	 * @param start
	 * @param messageData
	 * @return
	 */
	public float getValueFromData(String zcbh, int start, byte[] messageData, String blbh) {
		float value = 0.0f;
		
		PgHjsbbl blObj = blDao.findBlByBh(blbh);
		if (null == blObj) {
			logger.warn("没有在数据库中找到变量[" + blbh + "]");
			return value;
		}
		
		int kszdz = blObj.getKszdz(); //开始字地址
		int jszdz = blObj.getJszdz(); //结束字地址
		int n = jszdz - (kszdz - 1); //字数
		int offset = kszdz - start; //与开始字的偏移量
		
		Bytes valueBytes = new Bytes();
		for (int j = n; j > 0; j--) {
			valueBytes.append(new byte[] {messageData[(offset + j - 1) * 2], messageData[(offset + j - 1) * 2 + 1]});
		}
		
		try {
			value = Float.intBitsToFloat(Integer.parseInt(ByteUtil.binToHexString(valueBytes.toBytes()), 16)); //变量值
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("解析变量值[" + blbh + "]异常" + ex);
		}
		
		logger.debug("解析变量值[" + blbh + "]" + value);
		return value;
	}
	
	/**
	 * 二期PLC实时值解析策略
	 */
	public float getValueFromDataSecond(String zcbh, int start, byte[] messageData, String blbh) {
		float value = 0.0f;
		
		PgHjsbbl blObj = blDao.findBlByBh(blbh);
		if (null == blObj) {
			logger.warn("没有在数据库中找到变量[" + blbh + "]");
			return value;
		}
		
		int kszdz = blObj.getKszdz(); //开始字地址
		int jszdz = blObj.getJszdz(); //结束字地址
		int n = jszdz - (kszdz - 1); //字数
		int offset = kszdz - start; //与开始字的偏移量
		
		Bytes bytes = new Bytes(); //需要计算的字节数组
		for (int j = n; j > 0; j--) {
			bytes.append(new byte[] {messageData[(offset + j - 1) * 2], messageData[(offset + j - 1) * 2 + 1]});
		}
		
		Bytes valueBytes = new Bytes(); //实时值字节数字
		Bytes pointBytes = new Bytes(); //小数位字节数组
		valueBytes.append(new byte[] {messageData[(offset + 1 - 1) * 2], messageData[(offset + 1 - 1) * 2 + 1]});
		pointBytes.append(new byte[] {messageData[(offset + 2 - 1) * 2], messageData[(offset + 2 - 1) * 2 + 1]});
		
		// 变量值通过实时数据及数据小数点位来得到
		try {
			int point = ByteUtil.binToInt(pointBytes.toBytes());
			if (point > 0) {
				value = (float) (ByteUtil.binToInt(valueBytes.toBytes()) / Math.pow(10, point));
			} else if (point == 0) {
				value = ByteUtil.binToInt(valueBytes.toBytes());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("解析变量值[" + blbh + "]异常" + ex);
		}
		
		logger.debug("解析变量值[" + blbh + "]" + value);
		return value;
	}

}