字节数组增加Load/Save,支持保存到普通字节指针

This commit is contained in:
nnhy 2015-08-22 02:52:09 +00:00
parent 26b9cf49c0
commit 54b2fd48dc
4 changed files with 52 additions and 5 deletions

View File

@ -19,6 +19,7 @@ public:
byte SoftVer; // 软件版本
ushort Type; // 类型
byte Address; // 分配得到的设备地址
byte Password[16]; // 通信密码
byte Server; // 网关ID
byte Channel; // 通道
ushort Speed; // 传输速度

View File

@ -40,8 +40,13 @@ void TinyClient::Open()
_TaskID = Sys.AddTask(TinyClientTask, this, 0, 5000000, "客户端服务");
if(Config.Address > 0) Control->Address = Config.Address;
if(Config.Server > 0) Server = Config.Server;
if(Config.Address > 0 && Config.Server > 0)
{
Control->Address = Config.Address;
Server = Config.Server;
Password.Load(Config.Password, ArrayLength(Config.Password));
}
}
void TinyClient::Close()
@ -259,11 +264,16 @@ bool TinyClient::OnJoin(TinyMessage& msg)
Control->Address = dm.Address;
Password = dm.Password;
Password.Save(Config.Password, ArrayLength(Config.Password));
// 记住服务端地址
Server = dm.Server;
Config.Channel = dm.Channel;
Config.Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);;
Config.Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);
// 服务端组网密码,退网使用
Config.ServerKey[0] = dm.HardID.Length();
dm.HardID.Save(Config.ServerKey, ArrayLength(Config.ServerKey));
debug_printf("组网成功!由网关 0x%02X 分配得到节点地址 0x%02X ,频道:%d传输速率%dkbps密码", Server, dm.Address, dm.Channel, Config.Speed);
Password.Show(true);

View File

@ -117,6 +117,37 @@ String ByteArray::ToHex(char sep, int newLine) const
return ToHex(str, sep, newLine);
}
// 保存到普通字节数组,首字节为长度
int ByteArray::Load(const byte* data, int maxsize)
{
/*// 压缩编码整数最大4字节
Stream ms(data, 4);
_Length = ms.ReadEncodeInt();
return Copy(data + ms.Position(), _Length);*/
_Length = data[0] <= maxsize ? data[0] : maxsize;
return Copy(data + 1, _Length);
}
// 从普通字节数据组加载,首字节为长度
int ByteArray::Save(byte* data, int maxsize)
{
/*// 压缩编码整数最大4字节
Stream ms(data, 4);
ms.WriteEncodeInt(_Length);
return CopyTo(data + ms.Position(), _Length);*/
assert_param(_Length <= 0xFF);
int len = _Length <= maxsize ? _Length : maxsize;
data[0] = len;
return CopyTo(data + 1, len);
}
String& ByteArray::ToStr(String& str) const
{
return ToHex(str, '-', 0x20);

5
Type.h
View File

@ -395,6 +395,11 @@ public:
// 显示十六进制数据,指定分隔字符和换行长度
String ToHex(char sep = '-', int newLine = 0x10) const;
// 保存到普通字节数组,首字节为长度
int Load(const byte* data, int maxsize = -1);
// 从普通字节数据组加载,首字节为长度
int Save(byte* data, int maxsize = -1);
// 输出对象的字符串表示方式
virtual String& ToStr(String& str) const;
// 显示对象。默认显示ToString