forked from nakeeun/lsxgt_tcp_communication
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxgt_tcp_protocol.js
120 lines (116 loc) · 4.77 KB
/
xgt_tcp_protocol.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
exports.companyHeader = function(dataFrame){
var company_id = Buffer.from('LSIS-XGT');
var reserved1 = Buffer.from([0x00,0x00]);
var plc_info = Buffer.from([0x00,0x00]);
var cpu_info = Buffer.from([0xB0]);
var frame_dir = Buffer.from([0x33]);
var InvokeID = Buffer.from([0x01,0x00]);
var data_length = Buffer.from([dataFrame.length,0x00]);
var enetPos = Buffer.from([0x00]);
var reserved2 = Buffer.from([0x00]);
var total_length = company_id.length+reserved1.length+plc_info.length+cpu_info.length+
frame_dir.length+InvokeID.length+data_length.length+reserved2.length+
enetPos.length;
var data = Buffer.concat([company_id,reserved1,plc_info,cpu_info,frame_dir,
InvokeID,data_length,reserved2,enetPos],total_length);
//console.log(data);
return data;
}
//
exports.readData = function(address,dataType){
var data;
var command = Buffer.from([0x54,0x00]);
var set_data_Type = setDataType(dataType);
var reserved = Buffer.from([0x00,0x00]);
var block = Buffer.from([0x01,0x00]);
var addr = Buffer.from(address, 'utf8');
var addr_length = Buffer.from([addr.length,0x00]);
var total_length = command.length+set_data_Type.length+reserved.length+block.length+addr_length.length+addr.length;
data = Buffer.concat([command, set_data_Type,reserved,block,addr_length,addr],total_length);
//console.log(data);
return data;
}
exports.writeData = function(address,dataType,tempData){
var data;
var command = Buffer.from([0x58,0x00]);
var set_data_Type = setDataType(dataType);
var reserved = Buffer.from([0x00,0x00]);
var block = Buffer.from([0x01,0x00]);
var addr = Buffer.from(address, 'utf8');
var addr_length = Buffer.from([addr.length,0x00]);
var data = Buffer.from(tempData, 'hex');
if(data.length == 1){
data = Buffer.from([tempData,0x00],'hex');
}
var data_length = Buffer.from([data.length,0x00]);
var total_length = command.length+set_data_Type.length+reserved.length+block.length+addr_length.length+addr.length+data.length+data_length.length;
data = Buffer.concat([command, set_data_Type,reserved,block,addr_length,addr,data_length,data],total_length);
return data;
}
exports.seqWriteData = function(address,dataType,tempData){
var data;
var command = Buffer.from([0x58,0x00]);
var set_data_Type = setDataType(dataType);
var reserved = Buffer.from([0x00,0x00]);
var block = Buffer.from([0x01,0x00],'hex');
var addr = Buffer.from(address, 'utf8');
var addr_length = Buffer.from([addr.length,0x00]);
var add_addr_length = command.length+set_data_Type.length+reserved.length+block.length+addr_length.length+addr.length;
data = Buffer.concat([command, set_data_Type,reserved,block,addr_length,addr],add_addr_length);
var add_data = Buffer.from(tempData[0], 'hex');
if(add_data.length == 1){
add_data = Buffer.from([tempData[0],0x00],'hex');
}
for(var i=1;i<tempData.length;i++)
{
var temp_add_data = Buffer.from(tempData[i], 'hex');
if(temp_add_data.length == 1){
temp_add_data = Buffer.from([tempData[i],0x00],'hex');
}
var add_data_length = temp_add_data.length+add_data.length;
add_data = Buffer.concat([add_data,temp_add_data],add_data_length);
//console.log(temp_add_data);
}
var data_length = Buffer.from([add_data.length,0x00]);
var add_data_length = data.length+data_length.length+add_data.length;
data = Buffer.concat([data,data_length,add_data],add_data_length);
//console.log(data.toString('hex'))
return data;
}
exports.seqReadData = function(address,dataType,tempData){
var data;
var command = Buffer.from([0x54 ,0x00]);
var set_data_Type = setDataType(dataType);
var reserved = Buffer.from([0x00,0x00]);
var block = Buffer.from([0x01,0x00],'hex');
var addr = Buffer.from(address, 'utf8');
var addr_length = Buffer.from([addr.length,0x00]);
var data_length = Buffer.from([tempData,0x00],'hex');
var add_addr_length = command.length+set_data_Type.length+reserved.length+block.length+addr_length.length+addr.length+data_length.length;
data = Buffer.concat([command, set_data_Type,reserved,block,addr_length,addr,data_length],add_addr_length);
return data;
}
var setDataType = function(dataType){
var temp;
switch(dataType){
case 'bit':
temp = Buffer.from([0x00,0x00]);
break;
case 'byte':
temp = Buffer.from([0x01,0x00]);
break;
case 'word':
temp = Buffer.from([0x02,0x00]);
break;
case 'dword':
temp = Buffer.from([0x03,0x00]);
break;
case 'lword':
temp = Buffer.from([0x04,0x00]);
break;
case 'seq':
temp = Buffer.from([0x14,0x00]);
break;
}
return temp;
}