现在的位置: 首页 > 综合 > 正文

第一个下行指令测试通过:SET_TIME

2014年01月13日 ⁄ 综合 ⁄ 共 4414字 ⁄ 字号 评论关闭

输出如下:

01 53 45 54 5F 54 69 6D 65 0B 0C 12 10 02 12 00 D8
 cmd: 1 word: SET_Time data: 0B 0C 12 10 02 12 00 txt:

Sun Dec 18 16:02:18 CST 2011

package com.yc.pos32;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderAdapter;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * 下行协议 指令代码 控制字 数据 校验 回应: 成功:SET_OK 或 相应数据 失败:0
 * 
 * @author QinQouShui
 * 
 */
public class DownCodecFilter extends ProtocolCodecFilter {

	public static Hashtable ControlWords = new Hashtable();

	static {
		Properties p2 = new Properties();
		try {
			p2.load(new FileInputStream("./conf/conf.properties"));
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
		ControlWords.putAll(p2);

	}

	private static IoBuffer makePacket(byte command, String word,
			IoBuffer content) {
		IoBuffer buffer = IoBuffer.allocate(1 + word.length()
				+ (content == null ? 0 : content.remaining()) + 1);
		buffer.put(command).put(word.getBytes(Charset.forName("ascii")));
		if (content != null) {
			buffer.put(content);
			content.rewind();
		}
		return buffer.put(checksum(buffer, 0, buffer.position() - 1)).flip();
	}

	private static IoBuffer makePacket(DownCommand command) {
		return makePacket(command.getCmd(), command.getWord(),
				command.getContent());
	}

	// 求和校验
	private static byte checksum(IoBuffer buffer, int startPos, int endPos) {
		int sum = 0;
		for (int i = startPos; i <= endPos; i++)
			sum += buffer.get(i);
		return (byte) ((0x100 - sum & 0xff) & 0xff);
	}

	private static IoBuffer fromHexdump(String hex) {
		String[] arr = hex.split("\\s");
		IoBuffer buff = IoBuffer.allocate(arr.length);
		for (String a : arr) {
			buff.put((byte) Integer.parseInt(a, 16));
		}
		return buff.flip();
	}

	private static int getWordLength(byte key) {

		if (ControlWords.get("cw" + key) != null)
			return ControlWords.get("cw" + key).toString().length();
		else
			return 0;
	}

	// 解包
	private static DownCommand parsePacket(IoBuffer buff) throws Exception {

		int oldPos = 0;

		DownCommand command = null;
		byte cmdKey = buff.get();
		command = new DownCommand(cmdKey, buff.getString(getWordLength(cmdKey),
				Charset.forName("ascii").newDecoder()));

		int len = buff.limit() - 1 - buff.position();
		if (len > 0) {
			int oldLimit = buff.limit();
			buff.limit(buff.position() + len);
			command.setContent(buff.slice());
			buff.position(buff.limit());
			buff.limit(oldLimit);
		}
		if (checksum(buff, oldPos, buff.position() - 1) != buff.get()) {
			throw new Exception("校验错误");
		} else {
			command.setBad(false);
		}

		return command;
	}

	public DownCodecFilter() {
		super(new ProtocolCodecFactory() {

			public ProtocolEncoder getEncoder(IoSession session)
					throws Exception {
				return new ProtocolEncoderAdapter() {
					public void encode(IoSession session, Object message,
							ProtocolEncoderOutput out) throws Exception {
						IoBuffer buffer = makePacket((DownCommand) message);
						out.write(buffer);
					}
				};
			}

			public ProtocolDecoder getDecoder(IoSession session)
					throws Exception {
				return new ProtocolDecoderAdapter() {
					public void decode(IoSession session, IoBuffer in,
							ProtocolDecoderOutput out) throws Exception {
						IoBuffer buff = in;
						// 解包
						while (buff.hasRemaining()) {
							DownCommand cmd = parsePacket(buff);
							if (cmd != null) {
								out.write(cmd);
							} else {
								break;
							}
						}
						// if (buff.hasRemaining()) {
						// byte[] remain = new byte[in.remaining()];
						// in.get(remain);
						// session.setAttachment(remain);
						// }
					}
				};
			}

		});
	}

	public static void main(String[] args) {
		// 打包测试
		Calendar time = Calendar.getInstance();

		IoBuffer buffer = IoBuffer
				.allocate(200)
				.put((byte) (time.get(Calendar.YEAR) % 100))
				.put((byte) (time.get(Calendar.MONTH) + 1))
				.put((byte) time.get(Calendar.DAY_OF_MONTH))
				.put((byte) (time.get(Calendar.HOUR) + (time
						.get(Calendar.AM_PM) == Calendar.AM ? 0 : 12)))
				.put((byte) time.get(Calendar.MINUTE))
				.put((byte) time.get(Calendar.SECOND)).put((byte) 0).flip();
		IoBuffer outBuffer = makePacket((byte) 1, ControlWords.get("cw1")
				.toString(), buffer);

		System.out.println(outBuffer.getHexDump());

		// 解包测试
		try {
			DownCommand cmd = parsePacket(outBuffer);
			System.out.println(cmd.toString());
			Calendar c = Calendar.getInstance();
			c.set(cmd.getContent().get() + 2000, cmd.getContent().get() - 1,
					cmd.getContent().get(), cmd.getContent().get(), cmd
							.getContent().get(), cmd.getContent().get());
			System.out.println(c.getTime().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}


}

抱歉!评论已关闭.