I'm able to read multiple subscription topics by using this code. However I'm subscribed to a lot of different topics and the long if statement is slowing down my code. I already had to up the amount of cycle ticks to 20 (this is probably overkill, but 10 was not enough) in PlcTask. I'm looking for a smarter solution that will function with less cycle ticks. In the code shown below it is clear how long this IF statement is getting and this is not even the entire if statement (just for the topics machine and motion1).
IF fbMessageQueue.nQueuedMessages > 0 THEN
IF fbMessageQueue.Dequeue(fbMessage:=fbMessage) THEN
IF fbMessage.CompareTopic(sTopic:='machine/on') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Machine.bOnPB := STRING_TO_BOOL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='machine/off') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Machine.bOffPB := STRING_TO_BOOL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/position') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Postion := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/velocity') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Velocity := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/acceleration') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Acceleration := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/deceleration') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Deceleration := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/execute') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Execute := STRING_TO_LREAL(sPayloadRcv);
// same for motion2 and motion3
END_IF
END_IF
END_IF
My topics are build up like 'motion1/position' 'motion1/acceleration' 'motion2/acceleration' etc etc (I hope you get the idea). So I was already able to subscribe to all motion1 topics by subscribing to 'motion1/#'. So I tried to use fb.Message.CompareTopic(sTopic:='motion1/#') to find the topics belonging to motion1 and than an if statement that recognizes the topics 'motion1/somethingsomething'. However the fb.Message.CompareTopic(sTopic:='motion1/#') did not recognize motion1 topics.
IF fbMessageQueue.nQueuedMessages > 0 THEN
IF fbMessageQueue.Dequeue(fbMessage:=fbMessage) THEN
IF fbMessage.CompareTopic(sTopic:='machine/on') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Machine.bOnPB := STRING_TO_BOOL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='machine/off') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Machine.bOffPB := STRING_TO_BOOL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/#') THEN
IF fbMessage.CompareTopic(sTopic:='motion1/position') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Postion := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/velocity') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Velocity := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/acceleration') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Acceleration := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/deceleration') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Deceleration := STRING_TO_LREAL(sPayloadRcv);
ELSIF fbMessage.CompareTopic(sTopic:='motion1/execute') THEN
fbMessage.GetPayload(pPayload:=ADR(sPayloadRcv), nPayloadSize:=SIZEOF(sPayloadRcv), bSetNullTermination:=TRUE);
Motion.nMotion1Execute := STRING_TO_LREAL(sPayloadRcv);
END_IF
// same for motion2 and motion3
END_IF
END_IF
END_IF
So the first code that I showed does read all of the messages that I need from MQTT so that is really nice. However there should be a more efficient way to do this. I tried the method shown in the second code, however that did not work.