forked from CarstenMaul/MQTT-Siemens-S7-300
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT_Sending.SCL
108 lines (94 loc) · 2.3 KB
/
MQTT_Sending.SCL
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
FUNCTION mqttSendTCP : VOID
TITLE = 'MQTT mqttSendTCP Helper Function'
VERSION : '1.0'
AUTHOR : Brand
VAR_INPUT
offset : INT;
size : INT;
END_VAR
VAR_TEMP
i : INT;
END_VAR
BEGIN
IF mqttData.sendSize + size <= mqttGlobals.TCP_SENDBUFFERSIZE THEN
FOR i := 0 TO size-1 DO
mqttData.tcpSendBuf[MIN(IN1:=(i+mqttData.sendSize),IN2:=mqttGlobals.TCP_SENDBUFFERSIZE)] := mqttData.buffer[offset+i];
END_FOR;
mqttData.sendSize := mqttData.sendSize+size;
mqttData.reqSend := true;
ELSE
mqttData.tcp_sendBufferFull := true;
END_IF;
END_FUNCTION
(**************************************************************)
FUNCTION mqttWrite : VOID
TITLE = 'MQTT mqttWrite Helper Function'
VERSION : '1.0'
AUTHOR : Brand
VAR_INPUT
header : BYTE;
length : INT;
END_VAR
VAR_TEMP
lenBuf : ARRAY[0..3] OF BYTE;
llen : INT;
digit : BYTE;
digit_bitview at digit : ARRAY[0..7] of BOOL;
pos : INT;
rc : INT;
_len : INT;
i : INT;
END_VAR
BEGIN
llen := 0;
pos := 0;
_len := length;
REPEAT
digit := INT_TO_BYTE(_len MOD 128);
_len := _len / 128;
IF (_len > 0) THEN
digit_bitview[7] := 1; // digit |= 0x80;
END_IF;
lenBuf[pos] := digit;
pos := pos + 1;
llen := llen + 1;
UNTIL (_len <= 0) // do..while(len>0)
END_REPEAT;
mqttData.buffer[4-llen] := header;
FOR i := 0 TO llen-1 DO
mqttData.buffer[5-llen+i] := lenBuf[i];
END_FOR;
mqttSendTCP(offset := 4-llen, size := length+1+llen);
mqttData.lastOutActivity := mqttData.runTime;
END_FUNCTION
(**************************************************************)
FUNCTION mqttWriteString : INT
TITLE = 'MQTT mqttWriteString Helper Function'
VERSION : '1.0'
AUTHOR : Brand
VAR_INPUT
str : STRING;
_str AT str : STRUCT
length: BYTE;
act_length: BYTE;
str: ARRAY [1..254] OF BYTE;
END_STRUCT;
pos : INT;
END_VAR
VAR_TEMP
newPos : INT;
i : DINT;
j : INT;
END_VAR
BEGIN
newPos := pos + 2;
i := 0;
FOR j := 1 TO BYTE_TO_INT(_str.act_length) DO
mqttData.buffer[newPos] := _str.str[j];
newPos := newPos + 1;
i := i + 1;
END_FOR;
mqttData.buffer[newPos - 2 - i] := DWORD_TO_BYTE(SHR(IN := DINT_TO_DWORD(i), N := 8));
mqttData.buffer[newPos - 1 - i] := DINT_TO_BYTE(i);
mqttWriteString := newPos;
END_FUNCTION