B4J Question Resend command if no reply

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am using a UDP socket and I am sending a message to a device and then it will then reply.

Sometimes I get the reply and sometimes I don't.

I can only use UDP for the communication.

I have been trying to work out how to use a timer or something like a timer to re-send the same command if I don't get a reply within 2 seconds, but if I do get a reply I don't want to send that command again.

Anyone have any ideas on how I can create a timer or something simlar to send the command again if I don't get a reply, but if I do get a reply not to send the command again ?

(I know how to use timers, but can't work out how to not send the command again if I get the reply)

For Example:
I send 'ABC' UDP message to device.
The device will then reply with a message by UDP.
If I don't get a reply in 2 seconds time, resend 'ABC' again. But if I do get a reply don't send 'ABC' again.
 

aaronk

Well-Known Member
Licensed User
Longtime User
Problem is there could be 100 or so devices, which means I would need 100 or so timers ?

Any tips on how I can use the 1 timer for multiple devices ?
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
you could have a map recording the device id + received or not
B4X:
sub send
    dim m as map
    m.initialize

    for i = 1 to 100
         m.put(i, false) 'not received the answer
    next
    dim t as timer
    t.initialize("t", 2000)
    t.enabled = true
end sub

sub receivedUDPMessage(id as int)
    m.put(id, true)
end sub

sub t_tick
    for i = 0 to m.size -1
         dim received as boolean = m.getvalueat(i)
         if not(received) then
              'send again to   m.getKeyat(i)
         end if
   next
end sub
Then resend to the devices that didn't receive
 
Last edited:
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I ended up using CallSubUtils with a Map.

When I send a message I am putting the Key as the ID, and the value of that as the value I am sending.

I am also then triggering a sub using CallSubUtils. So once the message is sent, I am calling a sub in 3 seconds time.

B4X:
csu.CallSubPlus2(Me, "Send_again", 3000, Array("DeviceIDHere"))

When the Sub (Send_again) is triggered in 3 seconds time it will check the Map to see if that command is in the list. If it is, then it will re-send the command and won't remove it just in case it didn't get a reply. I am also using the above code in this sub so it triggers this sub again in 3 seconds, but only calling this code if there is still a item for this device in the list. If the device is removed (or not in the list no more) then I am not running that above code since there is no reason to check since we know there is no item in the Map.

When I receive the command from the device, I am removing the Key from the Map.

So far it all seems to be working fine. Thought I would post how I got it to work in case anyone else wanted to know how I ended up making it work.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…