Vijolica555
Member
Hello guys,
I am using TwinCAT 3 and communication with sensor with RS485 Protokol using SendString and ReceiveString FBs https://infosys.beckhoff.com/englis...erial_communication/9007204546041867.html&id=). So i trigger send command and then receive data fb in order to get response. I am communicating for a while now and we are getting data out of sensor successfully. Circumstances changed and we need some more info out of the sensor, which means that i need to send 200 different command to the sensor in cycle. I tried to use FOR loop in two ways but unsuccessfully. I would appreciate if anybody has an Idea how to make it working.
Please look step 4. I tried to do for loop for 200 commands. For each command we need to do the same steps as for RST command: so nStep0 - we choose command, nStep1 - we calculate the checksum, nStep 2 - sendString and nStep 3- receive string.
Example I with FOR loop (i would like that whole process under FOR is done - i want to wait fbSendString255 is not Busy, write/save the values and then do next command), Example II with CASE inside of FOR loop (here is program overloaded).
Example I:
Example II:
What i need is:
FOR nRequest = 0 do :
1. take sRequest := arrRequest [nRequest];
2. fbCheckSum(sSendString := sRequest);
3. SenString
4. ReceiveString and save string
5. wait 10ms
For nRequest = 1 do:
....
For nRequest = 200 do steps 1-5 and reset the sensor, then wait time is true, go from beginning nRequest = 0....
I am using TwinCAT 3 and communication with sensor with RS485 Protokol using SendString and ReceiveString FBs https://infosys.beckhoff.com/englis...erial_communication/9007204546041867.html&id=). So i trigger send command and then receive data fb in order to get response. I am communicating for a while now and we are getting data out of sensor successfully. Circumstances changed and we need some more info out of the sensor, which means that i need to send 200 different command to the sensor in cycle. I tried to use FOR loop in two ways but unsuccessfully. I would appreciate if anybody has an Idea how to make it working.
Please look step 4. I tried to do for loop for 200 commands. For each command we need to do the same steps as for RST command: so nStep0 - we choose command, nStep1 - we calculate the checksum, nStep 2 - sendString and nStep 3- receive string.
Example I with FOR loop (i would like that whole process under FOR is done - i want to wait fbSendString255 is not Busy, write/save the values and then do next command), Example II with CASE inside of FOR loop (here is program overloaded).
Example I:
Code:
CASE nStep OF
0:
sRequest := 'RST'; //We sent RST command and reset the sensor
nStep := 1;
1: //CheckSum
fbCheckSum(sSendString := sRequest); //we calculate checksum of the command
nStep := 2;
2: //Send RST Command
sSendString := sRequest;
CheckSum := fbCheckSum.Checksum;
fbSendString255.SendString := concat(sSendString, Checksum);
fbSendString255.SendString := concat(fbSendString255.SendString, '$0D');
fbSendString255(TxBuffer := GVL_Gepa.TxBuffer);
bWait := TRUE;
END_IF
IF NOT fbSendString255.Busy THEN //We wait that send string finished
nStep := 3;
END_IF
3:
fbReceiveString255(
Prefix := ,
Suffix := '$0D',
Timeout := T#5MS,
Reset := ,
StringReceived => bStringReceived,
Busy => bReceiveBusy ,
Error => eReceiveErrorID,
RxTimeout => bReceiveTimeout,
ReceivedString := sReceivedStringRST,
RXbuffer := GVL_Gepa.RxBuffer
);
tWaitRST(
IN:= TRUE,
PT:= T#10MS);
sCheck := LEFT(sReceivedStringRST,2);
IF sCheck ='OK' AND NOT fbReceiveString255.Busy AND tWaitRST.Q AND ((nMinutes = 0 AND nSeconds = 0) OR
(nMinutes = 10 AND nSeconds = 0) OR
(nMinutes = 20 AND nSeconds = 0) OR
(nMinutes = 30 AND nSeconds = 0)OR
(nMinutes = 40 AND nSeconds = 0)OR
(nMinutes = 50 AND nSeconds = 0))
THEN
tWaitRST(IN := FALSE,PT := T#10MS);
nStep := 4;
ELSE
nStep := 0;
END_IF
4:
FOR nRequest := 0 TO 200 DO
sRequest := arrRequest[nRequest];
GVL_Gepa.bSave := FALSE; //reset save value
fbCheckSum(sSendString := sRequest );
sSendString := sRequest;
CheckSum := fbCheckSum.Checksum;
fbSendString255.SendString := concat(sSendString, Checksum);
fbSendString255.SendString := concat(fbSendString255.SendString, '$0D');
fbSendString255(TxBuffer := GVL_Gepa.TxBuffer);
IF NOT fbSendString255.Busy THEN
fbReceiveString255(
Prefix := ,
Suffix := '$0D',
Timeout := T#5MS,
Reset := ,
StringReceived => bStringReceived,
Busy => bReceiveBusy ,
Error => eReceiveErrorID,
RxTimeout => bReceiveTimeout,
ReceivedString := sReceivedString,
RXbuffer := GVL_Gepa.RxBuffer);
sCheck := LEFT(sReceivedString,2);
tWait(IN := TRUE,PT := T#10MS);
IF tWait.Q AND NOT fbReceiveString255.Busy AND sCheck ='OK' THEN
tWait(IN := FALSE,PT := T#10MS);
GVL_Gepa.bSave := TRUE;
ELSE
nStep := 0;
GVL_Gepa.bSave := FALSE;
END_IF
END_IF
IF nRequest = 201 THEN
nStep := 0;
END_IF
END_FOR
//nStep := 0;
END_CASE
Example II:
Code:
0: // RST request
sRequest := 'RST';
nStep := 1;
1: //CheckSum
fbCheckSum(sSendString := sRequest);
nStep := 2;
2: //Send RST Command
sSendString := sRequest;
CheckSum := fbCheckSum.Checksum;
fbSendString255.SendString := concat(sSendString, Checksum);
fbSendString255.SendString := concat(fbSendString255.SendString, '$0D');
fbSendString255(TxBuffer := GVL_Gepa.TxBuffer);
END_IF
IF NOT fbSendString255.Busy THEN
nStep := 3;
END_IF
3:
fbReceiveString255(
Prefix := ,
Suffix := '$0D',
Timeout := T#5MS,
Reset := ,
StringReceived => bStringReceived,
Busy => bReceiveBusy ,
Error => eReceiveErrorID,
RxTimeout => bReceiveTimeout,
ReceivedString := sReceivedStringRST,
RXbuffer := GVL_Gepa.RxBuffer
);
tWaitRST(
IN:= TRUE,
PT:= T#10MS);
sCheck := LEFT(sReceivedStringRST,2);
IF sCheck ='OK' AND NOT fbReceiveString255.Busy AND tWaitRST.Q AND ((nMinutes = 0 AND nSeconds = 0) OR
(nMinutes = 10 AND nSeconds = 0) OR
(nMinutes = 20 AND nSeconds = 0) OR
(nMinutes = 30 AND nSeconds = 0)OR
(nMinutes = 40 AND nSeconds = 0)OR
(nMinutes = 50 AND nSeconds = 0))
THEN
tWaitRST(IN := FALSE,PT := T#10MS);
nStep := 4;
END_IF
4:
FOR nRequest := 0 TO 200 DO
nRequestStep := 0; //reset case
WHILE nRequestStep <9 DO
CASE nRequestStep OF
0:
sRequest := arrRequest[nRequest];
GVL_Gepa.bSave := FALSE; //reset save value
nRequestStep := 1;
1:
fbCheckSum(sSendString := sRequest );
nRequestStep := 2;
2:
sSendString := sRequest;
CheckSum := fbCheckSum.Checksum;
fbSendString255.SendString := concat(sSendString, Checksum);
fbSendString255.SendString := concat(fbSendString255.SendString, '$0D');
fbSendString255(TxBuffer := GVL_Gepa.TxBuffer);
IF NOT fbSendString255.Busy THEN //SentString finished go on
nRequestStep := 3;
END_IF
3:
fbReceiveString255(
Prefix := ,
Suffix := '$0D',
Timeout := T#5MS,
Reset := ,
StringReceived => bStringReceived,
Busy => bReceiveBusy ,
Error => eReceiveErrorID,
RxTimeout => bReceiveTimeout,
ReceivedString := sReceivedString,
RXbuffer := GVL_Gepa.RxBuffer);
sCheck := LEFT(sReceivedString,2); //OK or ER.. When ER check sMessage and do what needed: restart ect
tWait(IN := TRUE,PT := T#10MS);
IF tWait.Q AND NOT fbReceiveString255.Busy AND sCheck ='OK' THEN
tWait(IN := FALSE,PT := T#10MS);
GVL_Gepa.bSave := TRUE;
nRequestStep := 4;
ELSE
nRequestStep := 10;
nStep := 0;
END_IF
4:
GVL_Gepa.bSave := TRUE;
nRequestStep := 10;
END_CASE
END_WHILE
IF nRequest <201 THEN
nRequestStep := 0;
ELSE
nStep := 0;
END_IF
END_FOR
//nStep := 0;
END_CASE
What i need is:
FOR nRequest = 0 do :
1. take sRequest := arrRequest [nRequest];
2. fbCheckSum(sSendString := sRequest);
3. SenString
4. ReceiveString and save string
5. wait 10ms
For nRequest = 1 do:
....
For nRequest = 200 do steps 1-5 and reset the sensor, then wait time is true, go from beginning nRequest = 0....