Sunteți pe pagina 1din 6

POU: P2_DataRead

1 //this example program receives a string (max 255 chars) from the Com-port
2 //after receiving carriage return char ($R) the received string will have
been sent back via Com-port
3 PROGRAM P2_DataRead
4
5 VAR
6 iState : INT ; //current State
7 sComRead : STRING ( 255 ) ; //string, read by ReadComPort
8 sComWrite : STRING ( 255 ) ; //string to be send via com port
9 sReceived : STRING ( 255 ) ; //assembled string, received by
ReadComPort
10 Test : STRING ( 255 ) ; //assembled string, received by
ReadComPort
11 OpenComPort : COM . Open ; //instance of Com.Open
12 CloseComPort : COM . Close ; //instance of Com.Close
13 ReadComPort : COM . Read ; //instance of Com.Read
14 WriteComPort : COM . Write ; //instance of Com.Write
15 eComError : COM . ERROR ; //enumeration Com.Error
16 aComParams : ARRAY [ 1 .. 5 ] OF COM . PARAMETER ; //Parameters
Com-port - must fit with the communication partner
17
18 xwHandle : __XWORD ; //handle generated by Com.Open
19 xComError : BOOL ; //Com Error occurred
20 xRead : BOOL := TRUE ; //activate reading Com buffer
21 xAutoRead : BOOL := TRUE ; //automatic start reading
com buffer at start and after send string - set to false in order to avoid
automatic start read
22 xCR_detected : BOOL ; //Carriage return detected ->
activate send string
23 // Señal para partir la cadena
24 splitString : BOOL ;
25 arInt1 : INT ;
26 first : STRING ;
27 arInt3 : INT ;
28 confianza : STRING ( 255 ) ;
29 Recibido : STRING ( 255 ) ;
30 XoY : STRING ( 255 ) ;
31 Temporal_2 : BOOL ;
32 Temporal_1 : BOOL ;
33 Y_Leido : BOOL ;
34 X_Leido : BOOL ;
35 END_VAR
36 VAR_INPUT
37 arInt2 : INT ;
38 Delay : TON ;
39 END_VAR
40 VAR
41 Exact : INT ;
42 END_VAR
43

1 CASE iState OF
2 0 : //init - initialisation of the Com port parameters
3
4 //Port No

Page 1 of 6
30/10/2020 9:27 a. m.
POU: P2_DataRead

5 aComParams [ 1 ] . udiParameterId := COM . CAA_Parameter_Constants . udiPort


;
6 aComParams [ 1 ] . udiValue := PersistentVars . Puerto ;
7
8 //Baudrate
9 aComParams [ 2 ] . udiParameterId := COM . CAA_Parameter_Constants .
udiBaudrate ;
10 aComParams [ 2 ] . udiValue := PersistentVars . Baudrate ;
11
12 //Parity
13 aComParams [ 3 ] . udiParameterId := COM . CAA_Parameter_Constants .
udiParity ;
14 aComParams [ 3 ] . udiValue := PersistentVars . Pariedad ;
15
16 //StopBits
17 aComParams [ 4 ] . udiParameterId := COM . CAA_Parameter_Constants .
udiStopBits ;
18 //aComParams[4].udiValue := PersistentVars.BitsParada;
19 aComParams [ 4 ] . udiValue := COM . STOPBIT . ONESTOPBIT ;
20
21 //Number of Bytes
22 aComParams [ 5 ] . udiParameterId := COM . CAA_Parameter_Constants .
udiByteSize ;
23 //aComParams[5].udiValue := PersistentVars.NumeroBits;
24 aComParams [ 5 ] . udiValue := 7 ;
25
26 IF xAutoRead THEN
27 iState := 10 ;
28 END_IF
29
30
31 10 : // open Com port
32
33 OpenComPort (
34 xExecute := TRUE ,
35 xDone => ,
36 xBusy => ,
37 xError => xComError ,
38 usiListLength := 5,
39 pParameterList := ADR ( aComParams [ 1 ] ) ,
40 eError => eComError ,
41 hCom => xwHandle ) ;
42
43 IF OpenComPort . xDone THEN
44 OpenComPort ( xExecute := FALSE ) ;
45 Delay ( IN := TRUE ) ;
46 IF Delay . Q THEN
47 Delay ( IN := FALSE ) ;
48 iState := 20 ;
49 END_IF
50 END_IF
51
52 20 : // read from Com port
53
54 ReadComPort (
55 xExecute := xRead AND NOT ReadComPort . xDone ,

Page 2 of 6
30/10/2020 9:27 a. m.
POU: P2_DataRead

56 xAbort := ,
57 udiTimeOut := ,
58 xDone => ,
59 xBusy => ,
60 xError => xComError ,
61 xAborted => ,
62 hCom := xwHandle ,
63 pBuffer := ADR ( sComRead ) ,
64 szBuffer := 18 ,
65 eError => eComError ,
66 szSize => ) ;
67
68 xCR_detected := INT_TO_BOOL ( Find ( sComRead , '$$80' ) ) ;
69
70 sReceived := Concat ( sReceived , sComRead ) ;
71 sComRead := '' ;
72
73 IF xCR_detected THEN
74 GVL_1 . T3_IN := FALSE ;
75 ReadComPort ( xExecute := FALSE ) ;
76 //Delay(IN:= TRUE, PT:=T#100MS);
77 Delay ( IN := TRUE ) ;
78 IF Delay . Q THEN
79 Delay ( IN := FALSE ) ;
80 iState := 22 ;
81 END_IF
82 ELSE
83 GVL_1 . T3_IN := TRUE ;
84 END_IF
85 iState := 21 ;
86 IF Recibido = '$$80' AND Temporal_2 = FALSE THEN
87 GVL_1 . T5_IN := TRUE ;
88
89 IF Temporal_1 = TRUE THEN
90 GVL_1 . T6_0 ( PT := GVL_1 . T5_0 . ET ) ;
91 GVL_1 . T5_IN := FALSE ;
92 Temporal_2 := TRUE ;
93 END_IF
94
95 Temporal_1 := TRUE ;
96
97 END_IF
98 21 :
99
100 IF GVL_1 . T0_Q THEN
101 GVL_1 . DRead := 0 ;
102 GVL_1 . DReadREAL := 0 ;
103 GVL_1 . ORead := 0 ;
104 GVL_1 . DDesviacion := ABS ( REAL_TO_INT ( GVL_1 . DError ) ) ;
105 GVL_1 . T0_IN := FALSE ;
106 END_IF
107 GVL_1 . T0_IN := TRUE ;
108
109 arInt1 := FIND ( sComRead , '$R' ) ;
110 arInt2 := FIND ( sComRead , '$$' ) ;
111 arInt3 := FIND ( sComRead , '$NM' ) ;

Page 3 of 6
30/10/2020 9:27 a. m.
POU: P2_DataRead

112 confianza := MID ( sComRead , 3 , arInt3 + 3 ) ;


113 Exact := STRING_TO_INT ( confianza ) ;
114 Recibido := MID ( sComRead , 3 , 1 ) ;
115
116 Delay ( PT := REAL_TO_TIME ( ( TIME_TO_REAL ( GVL_1 . T6_0 . PT ) * 1 ) ) ) ;
117
118
119
120 IF Recibido = '$$80' AND Exact >= 950 THEN
121 XoY := MID ( sComRead , 1 , arInt3 + 2 ) ;
122 //first := MID(sComRead, 6,3);
123 first := MID ( sComRead , 5 , 3 ) ;
124
125 IF STRING_TO_INT ( first ) > 500 AND STRING_TO_INT ( first ) < 20001
THEN
126 GVL_1 . T0_IN := FALSE ;
127 GVL_1 . T0_1 . PT := T#0MS ;
128 GVL_1 . PrimerCiclo := FALSE ;
129
130 IF XoY = 'X' THEN
131 GVL_1 . DReadX := STRING_TO_INT ( first ) ;
132 X_Leido := TRUE ;
133 END_IF
134 IF XoY = 'Y' THEN
135 GVL_1 . DReadY := STRING_TO_INT ( first ) ;
136 Y_Leido := TRUE ;
137 END_IF
138 IF ( GVL_1 . DReadX <= ( GVL_1 . DReadY * 1.4 ) ) AND ( GVL_1 .
DReadX >= ( GVL_1 . DReadY * 0.6 ) ) THEN
139 IF X_Leido AND Y_leido THEN
140 GVL_1 . DRead := ( ( GVL_1 . DReadX + GVL_1 . DReadY ) / 2 ) ;
141 GVL_1 . DReadREAL := INT_TO_REAL ( GVL_1 . DRead ) * 0.001 ;
142 GVL_1 . ORead := ABS ( GVL_1 . DReadX - GVL_1 . DReadY ) ;
143 GVL_1 . DDesviacion := ABS ( REAL_TO_INT ( GVL_1 . DError ) ) ;
144 X_Leido := FALSE ;
145 Y_Leido := FALSE ;
146 END_IF
147 END_IF
148 GVL_1 . T0_1 . PT := T#3S ;
149
150
151 ELSE
152 GVL_1 . DRead := 0 ;
153
154 END_IF
155 END_IF
156 IF GVL_1 . Comunicar = FALSE THEN
157
158
159 iState := 20 ;
160
161
162 ELSE
163 GVL_1 . Comunicar := FALSE ;
164 Recibido := '' ;
165 Temporal_1 := FALSE ;

Page 4 of 6
30/10/2020 9:27 a. m.
POU: P2_DataRead

166 Temporal_2 := FALSE ;


167 iState := 25 ;
168
169 END_IF
170
171
172
173
174
175
176 22 :
177 //Delay(IN:= TRUE, PT:=T#92MS);
178 Delay ( IN := TRUE ) ;
179 IF Delay . Q THEN
180 Delay ( IN := FALSE ) ;
181 iState := 20 ;
182 END_IF
183 23 :
184 iState := 20 ;
185
186 25 : //create Write String
187 IF GVL_1 . HoI THEN
188 Test := 'H' ; //Enviar datos continuo
189 ELSE
190 Test := 'I' ; // Detener el envio de datos
191 END_IF
192 sComWrite := concat ( 'The following string was received: ' , Test ) ;
193 xCR_detected := FALSE ;
194 iState := 30 ;
195
196
197 30 : // write string to Com port
198
199 WriteComPort (
200 xExecute := TRUE ,
201 xAbort := ,
202 udiTimeOut := ,
203 xDone => ,
204 xBusy => ,
205 xError => xComError ,
206 xAborted => ,
207 hCom := xwHandle ,
208 pBuffer := ADR ( sComWrite ) ,
209 szSize := len ( sComWrite ) ,
210 eError => eComError ) ;
211
212 IF WriteComPort . xDone THEN
213 WriteComPort ( xExecute := FALSE ) ;
214
215 IF xAutoRead THEN // if xAutoRead is still true -> return to
iState 20 and listen to opened Com-port again. If not -> close com-port
216 iState := 20 ;
217 ELSE
218 iState := 40 ;
219 END_IF
220

Page 5 of 6
30/10/2020 9:27 a. m.
POU: P2_DataRead

221 END_IF
222
223 40 : // close Com port
224
225 CloseComPort (
226 xExecute := TRUE ,
227 xDone => ,
228 xBusy => ,
229 xError => xComError ,
230 hCom := xwHandle ,
231 eError => eComError ) ;
232
233 IF CloseComPort . xDone THEN
234 CloseComPort ( xExecute := FALSE ) ;
235 iState := 0 ;
236 END_IF
237
238
239 99 : // error handling
240
241 ;
242
243 END_CASE
244
245 IF xComError THEN
246 iState := 99 ;
247 END_IF
248

Page 6 of 6
30/10/2020 9:27 a. m.

S-ar putea să vă placă și