Programa 2
Programa 2
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
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
Page 4 of 6
30/10/2020 9:27 a. m.
POU: P2_DataRead
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.