WinCE对象序列化

最近项目要用到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, 032);
            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, 032);
            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;
        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值