Jump to content

Recommended Posts

Hi, 

I am new to AutoIT scripting and I am still learning. I am trying to communicate with a Labview application that acts like a server. it basically takes commands from the client. But for some commands, it also send back some data. 

When i am sending commands from my script, i can see that the labview is getting them. But i am not able to get anything back. I tried different code pieces that are available online in the forum.

This is the working piece of code which i been using to send data.

#cs
    This module is used to establish tcp connection with lab view
#ce
#include <File.au3>

Func SendCmd($cmd)
TCPStartup()

Local $IpAddress="192.168.10.101"
Local $Port="5353"

$Labview = TCPConnect($IpAddress,$Port)
If @error Then
    ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF)
    TCPCloseSocket($Labview)
    TCPShutdown()
    Exit
EndIf

TCPSend($Labview, $cmd & @CRLF)
TCPCloseSocket($Labview)
TCPShutdown()

EndFunc

SendCmd("wt42d")

 

This is slightly modified code to send and receive data, which is not working. I am not getting any response back

SendCmd("galil")


Func SendCmd($cmd)
TCPStartup()

Local $IpAddress="192.168.10.101"
Local $Port="5353"

$Labview = TCPConnect($IpAddress,$Port)
If @error Then
    ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF)
    TCPCloseSocket($Labview)
            TCPShutdown()
            Exit
EndIf

TCPSend($Labview, $cmd & @CRLF)

$ip = @IPAddress1

;create listening socket
$Listensocket = TCPListen($ip, $Port)
ConsoleWrite("Listening to Socket - " & $Listensocket & @CRLF)

If $Listensocket = -1 Then
    ConsoleWrite("Exiting..." & @CRLF)
    Exit
EndIf

;Accept incoming clients and recieve info
While 1
$connectedsocket = TCPAccept($Listensocket)

ConsoleWrite("Connecting to Socket - " & $connectedsocket & "Error -" & @error & @CRLF)
If $ConnectedSocket >= 0 Then
$ip2 = TCPRecv($connectedsocket,1000000)
EndIf
WEnd

TCPCloseSocket($connectedsocket)
TCPCloseSocket($Labview)

TCPShutdown()
EndFunc

I am not getting anything back. I am getting the following output in the console

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
Listening to Socket - 544
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0
Connecting to Socket - -1Error -0

its going through that loop forever. i need to force stop it.

But when i open putty and send the same command, i am getting response right away. 

Can someone please help me with that.

Thanks in advance

Regards

Yogendra

Share this post


Link to post
Share on other sites

Thank you TheDcoder. Base on your suggestion i made some changes to my TCP IP. Now it receives data.

SendCmd("galil")

Func SendCmd($cmd)
TCPStartup()

Local $IpAddress="192.168.10.101"
Local $Port="5353"

$Labview = TCPConnect($IpAddress,$Port)
If @error Then
    ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF)
    TCPCloseSocket($Labview)
            TCPShutdown()
            Exit
EndIf
TCPSend($Labview, $cmd & @CRLF)
;Accept recieve info
While 1
ConsoleWrite("Receiving..." & "Error -" & @error & @CRLF)
$Data = TCPRecv( $Labview,1000000)
ConsoleWrite("Data received - " & $Data & @CRLF)
WEnd
TCPCloseSocket($Labview)
TCPShutdown()
EndFunc

This code gives me output. 

Output is

Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - {"X Val":13.560689999999999,"Y Val":7.2500111111111112,"Z Val":-6.821950373197688,"W Val":0.0016857506686861156,"T Val":0.00054184842922053709,"R Val":-0.0048184615532875742,"D Val":-0,"X2 Val":1.2590163934426231,"Y2 Val":1.2622950819672132,"F Val":0.99933144667151319,"S Val":-0.0063663854846410951,"D2 Val":-0.00022680468487757083}

Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0
Data received - 
Receiving...Error -0

 

Can someone help me to

1. exit the loop after getting the data

2. There are a bunch of commands where they dont return any data. So, is there a way(not using if conditionals) to not wait in that while loop for those set of commands

 

Thanks 

Yogendra

Share this post


Link to post
Share on other sites

I'm bored, let's see how much we'll spoon feed. (Notice that proper indents make the code easier to follow)

SendCmd("galil")

Func SendCmd($cmd)
    TCPStartup()

    Local $IpAddress="192.168.10.101"
    Local $Port="5353"

    $Labview = TCPConnect($IpAddress,$Port)
    If @error Then
        ConsoleWrite('!--> TCPConnect error number ( ' & @error & ' ).' & @CRLF)
        TCPCloseSocket($Labview)
        TCPShutdown()
        Exit
    EndIf
    
    TCPSend($Labview, $cmd & @CRLF)
    
    While True
        $Data = TCPRecv($Labview,1000000)
        ; If there is no error returned from TCPRecv and there is data Then
        If (Not @error) And ($Data <> "") Then 
            ; Print the data received
            ConsoleWrite("Data received - " & $Data & @CRLF)
            ; Stop looping, jumps to just after WEnd
            ExitLoop
        EndIf
    WEnd
    
    TCPCloseSocket($Labview)
    TCPShutdown()
EndFunc

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

seadoggie01,

TCPRecv could be stuck in that while loop endlessly.

A better approach would be a loop counter.

A For/Next loop for example...

SendCmd('galil')

Func SendCmd($cmd)
    Opt('TCPTimeout', 10)
    TCPStartup()

    Local $IpAddress = '192.168.10.101'
    Local $Port = '5353'

    Local $Labview = TCPConnect($IpAddress, $Port)
    Local $nError = @error

    If $nError Then
        ConsoleWrite('!--> TCPConnect error number ( ' & $nError & ' ).' & @CRLF)
        TCPCloseSocket($Labview)
        TCPShutdown()
        Exit
    EndIf

    TCPSend($Labview, $cmd & @CRLF)

    Local $Data

    ; receive info
    For $i = 1 To 100
        $Data = TCPRecv($Labview, 32768)
        $nError = @error
        ConsoleWrite('Loop #' & $i & ' - Receive Error: ' & $nError & @CRLF & 'Data Received: ' & $Data & @CRLF)

        If $Data <> '' Then
            ExitLoop
        EndIf

        Sleep(10)
    Next

    TCPCloseSocket($Labview)
    TCPShutdown()
EndFunc

 

Edited by ripdad
added: Opt('TCPTimeout', 10)

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By MiKa666
      Hi all,
      I'm developing a tool for exploratively testing another software. For that I wanna cover all the GUI functionality from that software and verify everything works as intended. But now I've a problem which I couldnt solve so far.
      One thing that software I'm testing does, is to load up either a single picture or a series of pictures. Depending on how much pictures got loaded, further actions will be proceeded...
      This loaded pictures are schon within a ToolbarWindow and they're clickable. What I need to know is, how much pictures are in this ToolbarWindow...
      Can anybody provide me a tip on how to achive this?
      Would be amazing!!
      BR,
      Mic
    • By meety
      Hello!
      wingettext function can get the text content of the IE browser page, but the text content of the page cannot be obtained in the chrome browser? What should I do?
    • By DirtyJohny
      Hi everyone.Need rewrite this function how in еxample.
      Original:
      #RequireAdmin #NoTrayIcon Opt("MustDeclareVars",1) Func _a() Local $sls=ObjGet("winmgmts:{impersonationLevel=impersonate," _ &"authenticationLevel=Pkt}!\\"& _ @ComputerName&'\root\wmi'),$lss=$sls.ExecQuery _ ('SELECT * FROM WmiMonitorID'), _ $lll,$sll,$sss="",$lsl,$lls,$i,$z For $z In $lss $lsl=$z.UserFriendlyName For $i=0 To Ubound($lsl)-1 if ($lsl[$i]) Then $lll&=Chr($lsl[$i]) Next $lls=$z.SerialNumberID For $i=0 To Ubound($lls)-1 if ($lls[$i]) Then $sll&=Chr($lls[$i]) Next $sss&="Model"&@TAB&@TAB&": "&$lll&@CR&"Serial Number"&@TAB&": "&$sll&@CR&@CR $lll="" $sll="" Next MsgBox(262144,'Monitors '&$lss.Count,$sss&" "&@CR) $lss=Null $sls=Null EndFunc _a() Example:
      Func _InfoPC() Local $ObjService = ObjGet('winmgmts:{impersonationLevel = impersonate}!\\' & @ComputerName & '\root\cimv2') Local $ObjMB = $ObjService.ExecQuery('SELECT * FROM Win32_BaseBoard', 'WQL', 0x30) If IsObj($ObjService) Then For $objItemMB In $ObjMB $sInfo &= @TAB & 'Motherboard: ...... ' & $objItemMB.Product & @CRLF I'm beginner in this sphere and need  you all speak easy and simply because i'm Russian.Thanks)
    • By Deshanur
      Am trying to automate injecting credential on the login form for all kind of Web application for IE. I know how to identify the form name by viewing the source code and using the method - _IEFormGetObjByName($ie, $form_Name).
      I would like to know how to identify or get the form object for the web app where there is no form name tag for example below, for the is I have used - _IEFormGetCollection($ie, 0) to get the form object.
      My Question is does it work for all kind of application "_IEFormGetCollection($ie, 0)" how to identify Index value? is it always 0? is there any better solution?
      The final solution am looking for is find out form object, get the username, password field and inject credential and submit the form.
      How to find out index value? for the forms which does not have form name field.
      $login_form = _IEFormGetCollection($ie, 0) $email_field = _IEFormElementGetObjByName($login_form, $form_UserName) $pass_field = _IEFormElementGetObjByName($login_form, $form_password) $login_button = _IEFormElementGetObjByName($login_form, $form_submitbutton) _IEFormElementSetValue($email_field, $CmdLine[2]) _IEFormElementSetValue($pass_field, $CmdLine[3]) ControlSend($hwnd, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]","{Enter}") OR This works fine if the form has form name. $login_form = _IEFormGetObjByName($ie, $form_Name) $email_field = _IEFormElementGetObjByName($login_form, $form_UserName) $pass_field = _IEFormElementGetObjByName($login_form, $form_password) $login_button = _IEFormElementGetObjByName($login_form, $form_submitbutton) _IEFormElementSetValue($email_field, $CmdLine[2]) _IEFormElementSetValue($pass_field, $CmdLine[3]) ControlSend($hwnd, "", "[CLASS:Internet Explorer_Server; INSTANCE:1]","{Enter}")
    • By JuanFelipe
      Hello guys!
      I have a problem with a script, in the past I made a program and it worked perfect, I recently used it again and it already stands still in one step, the problem is a Javascript event that changed but now I cannot execute it.
       
      <td colspan="2" align="center"><input type="button" id="Buscar" name="Buscar" value="Buscar" onclick="javascript:enviarForma(document.obtenercasosPersonaPorDocumento,'0');" class="boton"></td>  
      Previously I used this code and it worked, but now it does nothing
      $botonconsulta = _IEGetObjByName($oIE,"Buscar") _IEAction($botonconsulta, 'click') _IELoadWait($oIE)  
      Now I have tried the following codes but none works.
       
      ;==================== 1 _IEHeadInsertEventScript($oIE, "document", "onclick", "javascript:enviarForma(document.obtenercasosPersonaPorDocumento,'0');") ControlClick("SUPER CELAC", "", "[CLASS:Internet Explorer_Server; INSTANCE:1]") _IELoadWait($oIE) ;==================== 2 $forma = $oIE.document.obtenercasosPersonaPorDocumento $oIE.Navigate("JavaScript:enviarForma("&$forma&",""0"");") _IELoadWait($oIE) ;==================== 3 $boton = _IEGetObjById($oIE, "Buscar") $boton.document.parentwindow.execScript("enviarForma(document.obtenercasosPersonaPorDocumento,'0');", "javascript") I hope you can guide me, because I think the error is in the parameters that the javascript function receives but I don't know how to send it, here I leave the first part of the JavaScript function.
      function enviarForma(frm,tipoForma){ var bExisteDatoConsulta = false; var bError=true; var mensajeError=""; switch (tipoForma) { //--Forma documento case "0": if (validar_campo_no_vacio_no_print(frm.numeroDocumento)) { if (!isNum(frm.numeroDocumento.value)) { bError = false; mensajeError += "El número de documento debe ser un valor numérico\n"; } bExisteDatoConsulta = true; } else { mensajeError += "Debe digitar un número de documento\n"; } break;  Thanks!
×
×
  • Create New...