最近项目要用到PDA与服务器通信,准备序列化传输model对象来解决命令问题。
但是WinCE精简的Framework不支持序列化及XML的方法。
没有办法,只能自己实现序列化。下面是一个类的实现。
这里仅提供一种解决方案,可能比较笨,欢迎大家讨论。
测试通过,但是代码格式没调整。。。
using System;
using System.Collections.Generic;
using System.Text;

namespace SocketModule

...{
public class TagReadInfo

...{

public TagReadInfo() ...{ }

private string clientIP;
private string gasStationID;
private byte[] tagReadBytes;
private byte clientFlag;


/**//// <summary>
/// 客户端的IP 即PDA的IP
/// </summary>
public string ClientIP

...{

get ...{ return clientIP; }

set ...{ clientIP = value; }
}


/**//// <summary>
/// 加气站编号 37010201
/// </summary>
public string GasStationID

...{

get ...{ return gasStationID; }

set ...{ gasStationID = value; }
}


/**//// <summary>
/// 客户端发送的字节数组 里面包括了时间 电子标签号 识读信息 车牌号码 加气枪号
/// </summary>
public byte[] TagReadBytes

...{

get ...{ return tagReadBytes; }

set ...{ tagReadBytes = value; }
}


/**//// <summary>
/// 是否是缓存数据 是:1 不是:0
/// </summary>
public byte ClientFlag

...{

get ...{ return clientFlag; }

set ...{ clientFlag = value; }
}

public byte[] ToByte()

...{
string str = clientIP + "," + gasStationID;
byte[] mess = System.Text.Encoding.UTF8.GetBytes(str.ToCharArray());

byte[] send = new byte[mess.Length + 33];
TagReadBytes.CopyTo(send, 0);
send[32] = ClientFlag;
mess.CopyTo(send, 33);
byte length = (byte)send.Length;

byte[] message = new byte[send.Length + 1];
message[0] = length;
send.CopyTo(message, 1);
return message;

}
public static TagReadInfo GetModel(byte[] Sendmess)

...{
byte length = Sendmess[0];
byte[] mess = new byte[length];
Buffer.BlockCopy(Sendmess, 1, mess, 0, length);

TagReadInfo info = new TagReadInfo();
byte[] mb = new byte[32];
Buffer.BlockCopy(mess, 0, mb, 0, 32);
info.TagReadBytes = mb;
info.ClientFlag = mess[32];

byte[] message = new byte[mess.Length - 33];
Buffer.BlockCopy(mess, 33, message, 0, mess.Length - 33);
string[] infos = System.Text.Encoding.UTF8.GetString(message).Split(',');
info.ClientIP = infos[0];
info.GasStationID = infos[1];
return info;
}

public static TagReadInfo GetModelForPDA(byte[] Sendmess)

...{
byte length = Sendmess[0];
byte[] mess = new byte[length];
Buffer.BlockCopy(Sendmess, 1, mess, 0, length);

TagReadInfo info = new TagReadInfo();
byte[] mb = new byte[32];
Buffer.BlockCopy(mess, 0, mb, 0, 32);
info.TagReadBytes = mb;
info.ClientFlag = mess[32];

byte[] message = new byte[mess.Length - 33];
Buffer.BlockCopy(mess, 33, message, 0, mess.Length - 33);
string[] infos = System.Text.Encoding.UTF8.GetString(message,0,message.Length).Split(',');
info.ClientIP = infos[0];
info.GasStationID = infos[1];
return info;
}
}
}
