某公司3万元外包开发的商业软件源码,使用Delphi 10.3.3 为某智能设备厂家开发的商业上位机源码,数据库采用Sqlite,串口通讯组件采用SPComm,购买本套源码你可以学习到开发上位机串口的完整技术、用unDAC组件开发Sqlite数据库的强大SQL技术、进制转换、CRC校验等
代码单元文件展示:
unit Unitprotocol_dj;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TProtocol = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
function areaSet(netid, areaId, areaLoopId, deviceId, deviceLoopId:integer):TBytes;
function cm06Set(netid, cm06Id, cm06KeyId, functionId:integer; sceneID: array of integer):TBytes;
function cm06Set_device(netid, cm06Id, cm06KeyId, functionId, delayTime, openjinaju, huanbianTime:integer; deviceID: array of integer):TBytes;
function data_Crc(data:TBytes):TBytes;//冗余校验,返回byte[] //OK
function gyqLightAutoSet(netid, deviceId, groupId, lightValueTrue, lightValueFalse:integer):TBytes;
function gyqLightSceneAutoSet(netid, deviceId, groupId, areaId, sceneId:integer):TBytes;
function KeyScene1(net_id, slave_id, Key_Id, function_Id:integer;scene_ID: array of integer):TBytes;
function KeyScene2(net_id, slave_id, Key_Id, function_Id:integer):TBytes;
function KeyScene3(net_id, slave_id, Key_Id, function_Id:integer;scene_ID: array of integer):TBytes;
function KeyScene4(net_id, slave_id, Key_Id, function_Id:integer):TBytes;
function on_device(netid:integer):TBytes; //全开 //OK
function off_device(netid:integer):TBytes;//全关 //OK
function readDeviceStatus(netid, deceiveId:integer):TBytes;
function readSystemTime(netid:integer) :TBytes;
function scence(netid, functionId, areaId, scenceID:integer):TBytes;// 开/关场景
function sceneSet(netid, areaId, sceneId, changYime, canshu:integer;loopLight: array of integer):TBytes;
function sceneSetTime(netid, areaId, sceneId, changYime, canshu:integer;loopLight: array of integer):TBytes;
function search_device(netid, functionId:integer):TBytes;//
function sensorStatus(netid, deviceId:integer):TBytes;
function sensorWorkG(netid, deviceId:integer; light: array of integer):TBytes;
function sensorWorkMonth():TBytes;
function Set_device1(netid, slave_id, Key_Id, function_Id, openjinaju, huanbianTime:integer; deviceID: array of integer):TBytes;
function Set_device2(netid, slave_id, Key_Id, function_Id, huanbianTime:integer; deviceID: array of integer):TBytes;
function Set_device3(netid, slave_id, Key_Id, function_Id, changesize, huanbianTime:integer; deviceID: array of integer):TBytes;
function Set_device4(netid, slave_id, Key_Id, function_Id, openjinaju, huanbianTime:integer; deviceID: array of integer):TBytes;
function Set_device5(netid, slave_id, Key_Id, function_Id, huanbianTime:integer; deviceID: array of integer):TBytes;
function Set_device6(netid, slave_id, Key_Id, function_Id, changesize, huanbianTime:integer; deviceID: array of integer):TBytes;
function setDeviceAux(netid, deceiveId:integer; loopStatus: array of integer):TBytes;
function setDeviceChushi(netid, deceiveId:integer; loopStatus: array of integer):TBytes;
function setDeviceDelay(netid, deceiveId:integer; loopStatus: array of integer):TBytes;
function setDeviceLight(netid, deceiveId:integer; loopStatus: array of integer):TBytes;
function setganjiedian(netid, deceiveId:integer; loopStatus: array of integer):TBytes;
function setKongtiaoArea(netid, deviceID, AreaID:integer):TBytes;
function setKongtiaoID(netid, deviceID, AreaID:integer):TBytes;
function setLightValue(netid, deviceID:integer; Light: array of integer):TBytes;
function SetLoopPram(netid, deviceId:integer; loopdata: array of string; num:integer):TBytes;
function SetLoopTime(netid, deviceId:integer; loopdata: array of string; num:integer):TBytes;
function setSystemTime(netid, year, month, day, Week, hour, minute, second:integer):TBytes;
function setTimeTable(netid, timeTableId:integer;hour_minute: array of integer):TBytes;
function timeSet(netid, timeTableId, timeId, functionId:integer; sceneID: array of integer):TBytes;
function tableTimeSet(netid, tableId, shikeId, functionId:integer; sceneID: array of integer):TBytes;
function virtualKey(netid, deviceId, loop, functionId:integer; dAR: array of string):TBytes;
end;
var
Protocol: TProtocol;
implementation
{$R *.dfm}
{ TProtocol }
function TProtocol.areaSet(netid, areaId, areaLoopId, deviceId,
deviceLoopId: integer): TBytes;
var
data : TBytes;
begin
SetLength(data, 40);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($26);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($02);
data[5] :=StrToInt('$' + InttoHex(areaId-1));
data[6] :=StrToInt('$' + InttoHex(areaLoopId));
data[7] :=StrToInt('$' + InttoHex(deviceId));
data[8] :=StrToInt('$' + InttoHex(deviceLoopId-1));
data[9] :=byte($fd);
data[10] :=byte($fd);
data[11] :=byte($fd);
data[12] :=byte($fd);
data[13] :=byte($fd);
data[14] :=byte($fd);
data[15] :=byte($fd);
data[16] :=byte($fd);
data[17] :=byte($fd);
data[18] :=byte($fd);
data[18] :=byte($fd);
data[20] :=byte($fd);
data[21] :=byte($fd);
data[22] :=byte($fd);
data[23] :=byte($fd);
data[24] :=byte($fd);
data[25] :=byte($fd);
data[26] :=byte($fd);
data[27] :=byte($fd);
data[28] :=byte($fd);
data[29] :=byte($fd);
data[30] :=byte($fd);
data[31] :=byte($fd);
data[32] :=byte($fd);
data[33] :=byte($fd);
data[34] :=byte($fd);
data[35] :=byte($fd);
data[36] :=byte($fd);
data[37] :=byte($fd);
data[38] :=byte($fd);
data[39] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.cm06Set(netid, cm06Id, cm06KeyId, functionId: integer;
sceneID: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 8+length(sceneID)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(8+length(sceneID)+1-2));
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := byte($11);
data[5] := StrToInt('$' + InttoHex(cm06Id));
data[6] := StrToInt('$' + InttoHex(cm06KeyId-1));
data[7] := StrToInt('$' + InttoHex(functionId));
for i := 0 to Length(sceneID)-1 do
begin
if (((functionId = 5) or ((functionId = 6) or (functionId = 7))) or (functionId = 8) or (functionId = 9)) then
begin
if (i mod 2)=0 then
data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]))
else data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]-1));
end else data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]-1));
end;
data[8+length(sceneID)]:= byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.cm06Set_device(netid, cm06Id, cm06KeyId, functionId,
delayTime, openjinaju, huanbianTime: integer;
deviceID: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 11+length(deviceID)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(11+length(deviceID)+1-2));
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := byte($11);
data[5] := StrToInt('$' + InttoHex(cm06Id));
data[6] := StrToInt('$' + InttoHex(cm06KeyId-1));
data[7] := StrToInt('$' + InttoHex(functionId));
data[8] := StrToInt('$' + InttoHex(delayTime));
data[9] := StrToInt('$' + InttoHex(openjinaju));
data[10] := StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin
if (((functionId = 5) or ((functionId = 6) or (functionId = 7))) or (functionId = 8) or (functionId = 9)) then
begin
if (i mod 3)=0 then
data[11 + i] := StrToInt('$' + InttoHex(deviceID[i]))
else if (i mod 3)=1 then data[11 + i] := StrToInt('$' + InttoHex(deviceID[i]-1))
else if (i mod 3)=2 then data[11 + i] := StrToInt('$' + InttoHex(deviceID[i]-2));
end;
end;
data[8+length(deviceID)]:= byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.data_Crc(data: TBytes): TBytes;//冗余校验,返回byte[]
var
index:integer;
temp :integer;//校验字节临时变量
begin
temp := 0;
for index := 1 to Length(data)-2 do //正确 必须-2
begin
temp:= temp xor data[index];
end;
data[Length(data) - 1] := StrToInt('$' + InttoHex(temp));
result:=Data;
{ temp := 0;
for index := 1 to Length(data)-2 do
begin
temp:= temp xor data[index];
end;
data[Length(data) - 1] := StrToInt('$' + InttoHex(temp));
result:=Data; }
end;
function TProtocol.gyqLightAutoSet(netid, deviceId, groupId, lightValueTrue,
lightValueFalse: integer): TBytes;
var
data : TBytes;
begin
SetLength(data, 9);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=8;
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=160;
data[5] :=StrToInt('$' + InttoHex(deviceId));
data[6] :=StrToInt('$' + InttoHex(groupId));
data[7] :=StrToInt('$' + InttoHex(lightValueTrue));
data[8] :=StrToInt('$' + InttoHex(lightValueFalse));
result:=data_Crc(Data);
end;
function TProtocol.gyqLightSceneAutoSet(netid, deviceId, groupId, areaId,
sceneId: integer): TBytes;
var
data : TBytes;
begin
SetLength(data, 9);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=8;
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($a1);
data[5] :=StrToInt('$' + InttoHex(deviceId));
data[6] :=StrToInt('$' + InttoHex(groupId));
data[7] :=StrToInt('$' + InttoHex(areaId));
data[8] :=StrToInt('$' + InttoHex(sceneId));
result:=data_Crc(Data);
end;
function TProtocol.KeyScene1(net_id, slave_id, Key_Id, function_Id: integer;
scene_ID: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 8+length(scene_ID)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(8+length(scene_ID)+1-2));
data[3] := StrToInt('$' + InttoHex(net_id));
data[4] := 11;
data[5] := StrToInt('$' + InttoHex(slave_id));
data[6] := StrToInt('$' + InttoHex(Key_Id));
data[7] := StrToInt('$' + InttoHex(function_Id));
for i := 0 to Length(scene_ID)-1 do
begin
data[8 + i] := StrToInt('$' + InttoHex(scene_ID[i] - 1));
end;
data[8 + length(scene_ID)] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.KeyScene2(net_id, slave_id, Key_Id,
function_Id: integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 9);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := 7;
data[3] := StrToInt('$' + InttoHex(net_id));
data[4] := 11;
data[5] := StrToInt('$' + InttoHex(slave_id));
data[6] := StrToInt('$' + InttoHex(Key_Id));
data[7] := StrToInt('$' + InttoHex(function_Id));
data[8] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.KeyScene3(net_id, slave_id, Key_Id, function_Id: integer;
scene_ID: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 8+length(scene_ID)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(8+length(scene_ID)+1-2));
data[3] := StrToInt('$' + InttoHex(net_id));
data[4] := byte($12); //与KeyScene1反编译数值不同
data[5] := StrToInt('$' + InttoHex(slave_id));
data[6] := StrToInt('$' + InttoHex(Key_Id));
data[7] := StrToInt('$' + InttoHex(function_Id));
for i := 0 to Length(scene_ID)-1 do
begin
data[8 + i] := StrToInt('$' + InttoHex(scene_ID[i] - 1));
end;
data[8 + length(scene_ID)] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.KeyScene4(net_id, slave_id, Key_Id,
function_Id: integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 9);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := 7;
data[3] := StrToInt('$' + InttoHex(net_id));
data[4] := byte($12); //与KeyScene2反编译数值不同
data[5] := StrToInt('$' + InttoHex(slave_id));
data[6] := StrToInt('$' + InttoHex(Key_Id));
data[7] := StrToInt('$' + InttoHex(function_Id));
data[8] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.off_device(netid: integer): TBytes; //OK
var
data : TBytes;
begin// 全关:FE 55 07 00 12 80 80 0B 4A(校验位)
//C#反编译:byte[] { 0xfe, 0x55, 7, 0, 0x12, 0x80, 0x80, 11(十六进制0B), 0x4a(校验位)}; //C#里面的10进制数字需要转成16进制
SetLength(data, 9); //最后一个位存放校验CRC
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($07);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($12);
data[5] :=byte($80);
data[6] :=byte($80);
data[7] :=byte($0B);
data[8] :=byte($4A); //C#4A;实际校验位=data[1] ~ data[7] 之间的字节异或xor = 4B
result:=data_Crc(Data);
end;
function TProtocol.on_device(netid: integer): TBytes;//OK
var
data : TBytes;
begin // 全开:FE 55 07 00 12 80 80 0A 4B(校验位)
//C#反编译:byte[] { 0xfe, 0x55, 7, 0, 0x12, 0x80, 0x80, 10(十六进制0A), 0x4B(校验位)}; //C#里面的10进制数字需要转成16进制
SetLength(data, 9);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($07);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($12);
data[5] :=byte($80);
data[6] :=byte($80);
data[7] :=byte($0A);
data[8] :=byte($4B);//C#4B;实际校验位=data[1] ~ data[7] 之间的字节异或xorxor = 4A
result:= data_Crc(Data);
end;
function TProtocol.readDeviceStatus(netid, deceiveId: integer): TBytes;
var
data : TBytes;
begin
SetLength(data, 7);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($05);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($16);
data[5] :=StrToInt('$' + InttoHex(deceiveId));
data[6] :=byte($00);
result:=data_Crc(Data);
end;
function TProtocol.readSystemTime(netid: integer): TBytes;
var
data : TBytes;
begin
SetLength(data, 6);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($04);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($05);
data[5] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.scence(netid, functionId, areaId, scenceID: integer): TBytes; // 开/关场景
var
data : TBytes;
begin
SetLength(data, 11);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($09);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($12);
data[5] :=byte($80);
data[6] :=byte($80);
data[7] :=StrToInt('$' + InttoHex(functionId));
data[8] :=StrToInt('$' + InttoHex(areaId-1));
data[9] :=StrToInt('$' + InttoHex(scenceID-1));
data[10] :=byte($00);
result:=data_Crc(Data);
end;
function TProtocol.sceneSet(netid, areaId, sceneId, changYime, canshu: integer;
loopLight: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 9+length(loopLight)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := 40; //16进制28 =10进制40个帧长
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := 3;//byte($03); //与KeyScene1反编译数值不同
data[5] := StrToInt('$' + InttoHex(areaId-1));
data[6] := StrToInt('$' + InttoHex(sceneId-1));
data[7] := StrToInt('$' + InttoHex(changYime));//?? 05
data[8] := StrToInt('$' + InttoHex(canshu));//00
for i := 0 to Length(loopLight)-1 do
begin
data[9 + i] := StrToInt('$' + InttoHex(loopLight[i]));
end;
data[9 + length(loopLight)] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.sceneSetTime(netid, areaId, sceneId, changYime,
canshu: integer; loopLight: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 9+length(loopLight)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := 40;
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := byte($03); //与KeyScene1反编译数值不同
data[5] := StrToInt('$' + InttoHex(areaId-1));
data[6] := StrToInt('$' + InttoHex(sceneId-1));
data[7] := StrToInt('$' + InttoHex(changYime));
data[8] := StrToInt('$' + InttoHex(canshu));
for i := 0 to Length(loopLight)-1 do
begin
data[9 + i] := StrToInt('$' + InttoHex(loopLight[i]));
end;
data[9 + length(loopLight)] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.search_device(netid, functionId: integer): TBytes;
var
data : TBytes;
begin
//搜索设备:fe 55 05 00(注意:第4位设备网关地址不要写错) 01 00 51//
SetLength(data, 7);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($05);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($01);
data[5] :=StrToInt('$' + InttoHex(functionId)); //参数=0---搜索在线设备。参数=1---网关停止搜索设备,重新读eeprom。参数=2---网关保存搜索结果到eeprom。
data[6] :=byte($51);
result:=data_Crc(Data);
end;
function TProtocol.sensorStatus(netid, deviceId: integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 6);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($05);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($30);
data[5] :=StrToInt('$' + InttoHex(deviceId));
result:=data_Crc(Data);
end;
function TProtocol.sensorWorkG(netid, deviceId: integer;
light: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 7+length(Light));
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(5+length(Light)));
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := byte($35);
data[5] := StrToInt('$' + InttoHex(deviceId));
for i := 0 to Length(Light)-1 do
begin
//data[6+i]:=
end;
result:=data_Crc(Data);
end;
function TProtocol.sensorWorkMonth: TBytes;
var
data : TBytes;
begin
SetLength(data, 5);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($04);
data[3] :=byte($12);
data[4] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.setDeviceAux(netid, deceiveId: integer;
loopStatus: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 6+length(loopStatus)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex(length(loopStatus)+5));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($34);
data[5] :=StrToInt('$' + InttoHex(deceiveId));
for i := 0 to Length(loopStatus)-1 do
begin
data[6 + i] :=StrToInt('$' + InttoHex(loopStatus[i]));
end;
result:=data_Crc(Data);
end;
function TProtocol.setDeviceChushi(netid, deceiveId: integer;
loopStatus: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 6+length(loopStatus)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex(length(loopStatus)+5));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($33);
data[5] :=StrToInt('$' + InttoHex(deceiveId));
for i := 0 to Length(loopStatus)-1 do
begin
data[6 + i] :=StrToInt('$' + InttoHex(loopStatus[i]));
end;
result:=data_Crc(Data);
end;
function TProtocol.setDeviceDelay(netid, deceiveId: integer;
loopStatus: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 6+length(loopStatus)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex(length(loopStatus)+5));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($10);
data[5] :=StrToInt('$' + InttoHex(deceiveId));
for i := 0 to Length(loopStatus)-1 do
begin
data[6 + i] :=StrToInt('$' + InttoHex(loopStatus[i]));
end;
result:=data_Crc(Data);
end;
function TProtocol.setDeviceLight(netid, deceiveId: integer; //通信协议文档:3、设置输出模块回路状态
loopStatus: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin //loopStatus 12个元素
SetLength(data, 6+length(loopStatus)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex(length(loopStatus)+5)); //帧长
data[3] :=StrToInt('$' + InttoHex(netid)); //网关ID
data[4] :=byte($17); //命令代码
data[5] :=StrToInt('$' + InttoHex(deceiveId));
for i := 0 to Length(loopStatus)-1 do
begin
data[6 + i] :=StrToInt('$' + InttoHex(loopStatus[i]));
end;
result:=data_Crc(Data);
// result:=Data;
//Finalize(data);
// data:=nil;
end;
function TProtocol.setganjiedian(netid, deceiveId: integer;
loopStatus: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 6+length(loopStatus)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex(length(loopStatus)+5));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($34);
data[5] :=StrToInt('$' + InttoHex(deceiveId));
for i := 0 to Length(loopStatus)-1 do
begin
data[6 + i] :=StrToInt('$' + InttoHex(loopStatus[i]));
end;
result:=data_Crc(Data);
end;
function TProtocol.setKongtiaoArea(netid, deviceID, AreaID: integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 8); //为什么是8 因为最后一位要用来存放crc校验数值
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=6;
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=11;
data[5] :=StrToInt('$' + InttoHex(deviceID));
data[6] :=StrToInt('$' + InttoHex(AreaID));
result:=data_Crc(Data);
end;
function TProtocol.setKongtiaoID(netid, deviceID, AreaID: integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 8); //为什么是8 因为最后一位要用来存放crc校验数值
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=6;
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($5f);
data[5] :=StrToInt('$' + InttoHex(deviceID));
data[6] :=StrToInt('$' + InttoHex(AreaID));
result:=data_Crc(Data);
end;
function TProtocol.setLightValue(netid, deviceID: integer;
Light: array of integer): TBytes;
var
data : TBytes;
i : integer;
str,str1,str2,str3:string;
begin
SetLength(data, 6+length(Light)*2+1);//为什么+1?因为最后一个要存放crc校验数据
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((4+length(Light)*2)+1));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($1b);
data[5] :=StrToInt('$' + InttoHex(deviceID));
for i := 0 to Length(Light)-1 do
begin
str := Trim(Light[i].ToString).PadLeft(4,'0').Substring(0,4);
str2 := str.Substring(2, 2);
str3 := str.Substring(0, 2);
data[6 + (2 * i)] :=StrToInt('$' + InttoHex(str2.ToInteger()));
data[(6 + (2 * i)) + 1] :=StrToInt('$' + InttoHex(str3.ToInteger()));
end;
result:=data_Crc(Data);
end;
function TProtocol.SetLoopPram(netid, deviceId: integer; loopdata: array of string;
num: integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 7+length(loopdata)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((5+length(loopdata)+1)));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=4;
data[5] :=StrToInt('$' + InttoHex(deviceId));
data[6] :=StrToInt('$' + InttoHex(num));
for i := 0 to Length(loopdata)-1 do
begin
if (loopdata[i] = null) then data[7 + i] :=StrToInt('$fd')
else if (trim(loopdata[i]) = '253') then data[7 + i] :=StrToInt('$fd')
else data[7 + i] :=StrToInt('$' + InttoHex(loopdata[i].ToInteger));
end;
result:=data_Crc(Data);
end;
function TProtocol.SetLoopTime(netid, deviceId: integer;
loopdata: array of string; num: integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 7+length(loopdata)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((5+length(loopdata)+1)));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($60);
data[5] :=StrToInt('$' + InttoHex(deviceId));
data[6] :=StrToInt('$' + InttoHex(num));
for i := 0 to Length(loopdata)-1 do
begin
if (trim(loopdata[i]) = '253') then data[7 + i] :=StrToInt('$fd')
else data[7 + i] :=StrToInt('$' + InttoHex(loopdata[i].ToInteger));
end;
result:=data_Crc(Data);
end;
function TProtocol.setSystemTime(netid, year, month, day, Week, hour, minute,
second: integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 13);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($37);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=10;
data[5] :=StrToInt('$' + InttoHex(year));
data[6] :=StrToInt('$' + InttoHex(month));
data[7] :=StrToInt('$' + InttoHex(day));
data[8] :=StrToInt('$' + InttoHex(Week));
data[9] :=StrToInt('$' + InttoHex(hour));
data[10] :=StrToInt('$' + InttoHex(minute));
data[11] :=StrToInt('$' + InttoHex(second));
result:=data_Crc(Data);
end;
function TProtocol.setTimeTable(netid, timeTableId: integer;
hour_minute: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 6+length(hour_minute));
data[0] :=byte($fe);//16进制Byte化
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex(4+length(hour_minute)));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=9;
data[5] :=StrToInt('$' + InttoHex(timeTableId-1));
for i := 0 to Length(hour_minute)-1 do
begin
if (hour_minute[i].ToString() = '253') then //10进制253
begin //16进制0x10等于10进制16 也就是fromBase=16 也就是Convert.ToByte将参数FD转换成16进制Byte
data[6 + i] :=StrToInt('$fd');//C# data[6 + i] = Convert.ToByte("FD", 0x10);//Convert.ToByte(hours.ToString(), 16);
end else data[6 + i] :=StrToInt('$' + InttoHex(hour_minute[i])); //hour_minute[i]=253(10进制整数),这里转换成16进制
end;
end;
function TProtocol.Set_device1(netid, slave_id, Key_Id, function_Id,
openjinaju, huanbianTime: integer; deviceID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 10+length(deviceID)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((10+length(deviceID)+1)-2));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=11;
data[5] :=StrToInt('$' + InttoHex(slave_id));
data[6] :=StrToInt('$' + InttoHex(Key_Id));
data[7] :=StrToInt('$' + InttoHex(function_Id));
data[8] :=StrToInt('$' + InttoHex(openjinaju));
data[9] :=StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin
if ((function_Id = 5) or (function_Id = 7)) then
begin
if ((i mod 3) = 0) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]))
else if ((i mod 3) = 1) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]-1))
else if ((i mod 3) = 2) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]-2))
end;
end;
data[10 + length(deviceID)] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.Set_device2(netid, slave_id, Key_Id, function_Id,
huanbianTime: integer; deviceID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 9+length(deviceID)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((10+length(deviceID)+1)-2));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=11;
data[5] :=StrToInt('$' + InttoHex(slave_id));
data[6] :=StrToInt('$' + InttoHex(Key_Id));
data[7] :=StrToInt('$' + InttoHex(function_Id));
data[8] :=StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin
if ((function_Id = 5) or (function_Id = 7) or (function_Id = 15)) then
begin
if ((i mod 2) = 0) then
data[9 + i] :=StrToInt('$' + InttoHex(deviceID[i]))
else if ((i mod 2) = 1) then
data[9 + i] :=StrToInt('$' + InttoHex(deviceID[i]-1));
end;
end;
data[9 + length(deviceID)] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.Set_device3(netid, slave_id, Key_Id, function_Id,
changesize, huanbianTime: integer; deviceID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 10+length(deviceID)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((10+length(deviceID)+1)-2));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=11;
data[5] :=StrToInt('$' + InttoHex(slave_id));
data[6] :=StrToInt('$' + InttoHex(Key_Id));
data[7] :=StrToInt('$' + InttoHex(function_Id));
data[8] :=StrToInt('$' + InttoHex( changesize));
data[9] :=StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin //0x10
if ((function_Id = 15) or (function_Id = 16)) then
begin
if ((i mod 2) = 0) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]))
else if ((i mod 2) = 1) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]-1));
end;
end;
data[10 + length(deviceID)] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.Set_device4(netid, slave_id, Key_Id, function_Id,
openjinaju, huanbianTime: integer; deviceID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 10+length(deviceID)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((10+length(deviceID)+1)-2));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($12);
data[5] :=StrToInt('$' + InttoHex(slave_id));
data[6] :=StrToInt('$' + InttoHex(Key_Id));
data[7] :=StrToInt('$' + InttoHex(function_Id));
data[8] :=StrToInt('$' + InttoHex(openjinaju));
data[9] :=StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin
if ((function_Id = 5) or (function_Id = 7)) then
begin
if ((i mod 3) = 0) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]))
else if ((i mod 3) = 1) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]-1))
else if ((i mod 3) = 2) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]-2))
end;
end;
data[10 + length(deviceID)] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.Set_device5(netid, slave_id, Key_Id, function_Id,
huanbianTime: integer; deviceID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 9+length(deviceID)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((10+length(deviceID)+1)-2));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($12);
data[5] :=StrToInt('$' + InttoHex(slave_id));
data[6] :=StrToInt('$' + InttoHex(Key_Id));
data[7] :=StrToInt('$' + InttoHex(function_Id));
data[8] :=StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin
if ((function_Id = 8) or (function_Id = 9) or (function_Id = 15)) then
begin
if ((i mod 2) = 0) then
data[9 + i] :=StrToInt('$' + InttoHex(deviceID[i]))
else if ((i mod 2) = 1) then
data[9 + i] :=StrToInt('$' + InttoHex(deviceID[i]-1));
end;
end;
data[9 + length(deviceID)] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.Set_device6(netid, slave_id, Key_Id, function_Id,
changesize, huanbianTime: integer; deviceID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 10+length(deviceID)+1);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=StrToInt('$' + InttoHex((10+length(deviceID)+1)-2));
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=byte($12);
data[5] :=StrToInt('$' + InttoHex(slave_id));
data[6] :=StrToInt('$' + InttoHex(Key_Id));
data[7] :=StrToInt('$' + InttoHex(function_Id));
data[8] :=StrToInt('$' + InttoHex( changesize));
data[9] :=StrToInt('$' + InttoHex(huanbianTime));
for i := 0 to Length(deviceID)-1 do
begin //0x10
if ((function_Id = 15) or (function_Id = 16)) then
begin
if ((i mod 2) = 0) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]))
else if ((i mod 2) = 1) then
data[10 + i] :=StrToInt('$' + InttoHex(deviceID[i]-1));
end;
end;
data[10 + length(deviceID)] :=byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.tableTimeSet(netid, tableId, shikeId, functionId:integer;
sceneID: array of integer): TBytes;
var
data : TBytes;
i : integer;
begin
SetLength(data, 40);
data[0] :=byte($fe);
data[1] :=byte($55);
data[2] :=byte($37);
data[3] :=StrToInt('$' + InttoHex(netid));
data[4] :=10;
data[5] :=StrToInt('$' + InttoHex(tableId-1));
data[6] :=StrToInt('$' + InttoHex(shikeId));
data[7] :=StrToInt('$' + InttoHex(functionId));
data[8] :=byte($fd);
data[9] :=byte($fd);
data[10] :=byte($fd);
data[11] :=byte($fd);
data[12] :=byte($fd);
data[13] :=byte($fd);
data[14] :=byte($fd);
data[15] :=byte($fd);
data[16] :=byte($fd);
data[17] :=byte($fd);
data[18] :=byte($fd);
data[18] :=byte($fd);
data[20] :=byte($fd);
data[21] :=byte($fd);
data[22] :=byte($fd);
data[23] :=byte($fd);
data[24] :=byte($fd);
data[25] :=byte($fd);
data[26] :=byte($fd);
data[27] :=byte($fd);
data[28] :=byte($fd);
data[29] :=byte($fd);
data[30] :=byte($fd);
data[31] :=byte($fd);
data[32] :=byte($fd);
data[33] :=byte($fd);
data[34] :=byte($fd);
data[35] :=byte($fd);
data[36] :=byte($fd);
data[37] :=byte($fd);
data[38] :=byte($fd);
data[39] :=byte($fd);
for i := 0 to Length(sceneID)-1 do
begin
if (((functionId = 5) or ((functionId = 6) or (functionId = 7))) or (functionId = 8) or (functionId = 9)) then
begin
if (i mod 2)=0 then
data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]))
else data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]-1));
end else data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]-1));
end;
data[8+length(sceneID)]:= byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.timeSet(netid, timeTableId, timeId, functionId: integer;
sceneID: array of integer): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 8+length(sceneID)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(8+length(sceneID)+1-2));
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := 10;
data[5] := StrToInt('$' + InttoHex(timeTableId));
data[6] := StrToInt('$' + InttoHex(timeId));
data[7] := StrToInt('$' + InttoHex(functionId));
for i := 0 to Length(sceneID)-1 do
begin
if (((functionId = 5) or ((functionId = 6) or (functionId = 7))) or (functionId = 8) or (functionId = 9)) then
begin
if (i mod 2)=0 then
data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]))
else data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]-1));
end else data[8 + i] := StrToInt('$' + InttoHex(sceneID[i]-1));
end;
data[8 + length(sceneID)] := byte($fd);
result:=data_Crc(Data);
end;
function TProtocol.virtualKey(netid, deviceId, loop, functionId: integer;
dAR: array of string): TBytes;
var
data : TBytes;
i: integer;
begin
SetLength(data, 8+length(dAR)+1);
data[0] := byte($fe);
data[1] := byte($55);
data[2] := StrToInt('$' + InttoHex(8+length(dAR)+1-2));
data[3] := StrToInt('$' + InttoHex(netid));
data[4] := byte($12);
data[5] := StrToInt('$' + InttoHex(deviceId));
data[6] := StrToInt('$' + InttoHex(loop));
data[7] := StrToInt('$' + InttoHex(functionId));
for i := 0 to Length(dAR)-1 do
begin
data[8 + i] := StrToInt('$' + InttoHex(dAR[i].ToInteger-1));
end;
result:=data_Crc(Data);
end;
end.