Sign in to follow this  
Followers 0
royalmarine

quick ping question

6 posts in this topic

#1 ·  Posted (edited)

hi guys, trying to set a ping function to check for a device available.

here's my code so far

if Ping($targetIP, 250) = 0 Then
      $NetworkDevice = False
      sleep(3000)
Else
      msgbox(0, '', 'device reachable')
      sleep(3000)
EndIf

however, I'm having mixed results from this, so i want to add in a count.

lets say it tries to ping 5 times. and if less than 3 of these are successful, then it says the device is not reachable.

if 3 or more are successful, then i want it to say the device is reachable.

so, i think something like this would work

Ping($targetIP, 250)
if Not @error Then $Count +=1
     if $count <3 Then
           msgbox(0. ''. 'device not reachable')
     else if $Count >=3 Then
           msgbox(0,'', 'device reachable')
     EndIf
EndIf

but i can't seem to figure out what I'm doing wrong with it..

any suggestions would be amazing!

Edited by royalmarine

Share this post


Link to post
Share on other sites



ok, after reading over my code, clearly I'm doing it wrong.

the count will obviously return a fail twice then success after since the count increases etc...

so i think i need a for loop in there to do the ping 5 times, each time increasing the count.

once its done 5 times, return the result, and then reset the count back to 0...

can anyone possibly help a bit with this? I've never used a for loop in auto it

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

ok, this is what I've tried so far, think I'm close as I'm not getting any error's, just it always comes back with a fail.

Local $Count
$TargetIP = "192.168.1.2"
  
While 1  
  
$Count1 = 0
$Count2 = 0
Do
   Ping($targetIP, 250)
If Ping($TargetIP, 250) = 0 Then
  $Count1 +=1
    ElseIf Ping($TargetIP, 250) = 1 Then
  $Count1 +=1
  $Count2 +=1
  EndIf
Until $Count1 = 5
If $Count2 <3 Then
   MsgBox(0, '', 'fail')
Else
   MsgBox(0, '', 'Success')
   EndIf
  
   WEnd
Edited by royalmarine

Share this post


Link to post
Share on other sites

ok, think I've fixed this.

I've altered it like this

Local $Count
$TargetIP = "192.168.1.2"
 
While 1 
 
$Count1 = 0
$Count2 = 0
Do
   Ping($targetIP, 250)
If Ping($TargetIP, 250) = 0 Then    ; ping fails
  $Count1 +=1
    Else                                            ; ping doesn't fail
  $Count1 +=1
  $Count2 +=1
  EndIf
Until $Count1 = 5
If $Count2 <3 Then
   MsgBox(0, '', 'fail')
Else
   MsgBox(0, '', 'Success')
   EndIf

it works. when i disconnect the device from network it fails.

when i connect it, msgbox reports success.

I've also added @error to my msgbox and I'm getting a result of 0 when the device is on

and a result of 4 when the device is off.

Share this post


Link to post
Share on other sites

one last thing to add, i forgot to rest the $count1 and $count2 back to 0 once the loop had completed.

once i did this, it works fine

;)

Share this post


Link to post
Share on other sites

Edit button broken?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0