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

TCP is a duplex connection, meaning you can use the same socket to both read and write (send and receive) data, so use a single socket with both TCPSend and TCPRecv. You don't have to use TCPListen for client connections.


A cross-platform implementation of the AutoIt language

My contributions to the AutoIt Community ##AutoIt at freenode, real-time chat

3fHNZJ.gif

Spoiler

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) intend to do that to anybody.

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
Posted (edited)

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 Jahar
      I have an AutoIT script It monitors 2 websites for content that applys to me and the services that I provide. One site is : www.Freelancer.com The other: www.PeoplePerHour.com Both sites publish new jobs on their site hourly or so. My AutoIT app, will view those sites and present new jobs to me in a grid that pops up on my screen. Lately, the app has stopped showing me any jobs from PeoplePerHour.
       
      For freelancer.com,  Inetget is giving full html but for peopleperhour, now its not coming.
      Func _CheckPPH()
          Local Static $hTimer = 0
          Local Static $hDownload = 0
          Local $aTitlesandUrls = 0
          Local Static $sTempFile = ""
          If $hTimer = 0 Then $hTimer = TimerInit()
          If $hDownload = 0 Then
              $sTempFile = _WinAPI_GetTempFileName(@TempDir)
              ConsoleWrite("Checking PPH..." & @CRLF)
              ConsoleWrite(">Downloading..." & @CRLF)
      ;~         $hDownload = InetGet("http://www.peopleperhour.com/freelance-jobs", $sTempFile, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)
              $hDownload = InetGet("http://www.peopleperhour.com/freelance-jobs", $sTempFile, $INET_FORCERELOAD)
      ;~         Return 0
          EndIf
      ;~     Sleep(30)
      ;~     Local $isCompleted = InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)
      ;~     Local $isError = InetGetInfo($hDownload, $INET_DOWNLOADERROR)
      ;~     Sleep(30)
      ;~     If TimerDiff($hTimer) > 3000 And $isError Then
      ;~         ConsoleWrite("!PPH Fail" & @CRLF)
      ;~         InetClose($hDownload)
      ;~         $hDownload = 0
      ;~         Return 0
      ;~     EndIf
      ;~     Sleep(30)
          Local $Show = 0
      ;~     If TimerDiff($hTimer) > 3000 And $isCompleted Then
          If $hDownload > 0 Then
              ConsoleWrite("+Downloaded..." & @CRLF)
              Local $sPPHHtml = FileRead($sTempFile)
              $aTitlesandUrls = _StringBetween($sPPHHtml, '"title">' & @LF, 'time>')
      ;~         _ArrayDisplay($aTitlesandUrls)
              Local $aPPH[0][4]
              Local $sTitle = ""
              Local $sUrl = ""
              Local $sID = ""
              Local $sDate = ""
              Local $iRet=0
              Sleep(30)
              For $i = 0 To UBound($aTitlesandUrls) - 1
                  $sTitle = _StringBetween($aTitlesandUrls[$i], '<a title="', '" class')
                  $sUrl = _StringBetween($aTitlesandUrls[$i], 'href="', '">')
                  $sDate = _GetDate($aTitlesandUrls[$i])
                  If IsArray($sTitle) And IsArray($sUrl) Then
                      $sID = _GetID($sUrl[0])
      ;~                 _ArrayAdd($aPPH, $sDate & "|" & $sTitle[0] & "|" & $sUrl[0] & "|" & $sID)
                      $iRet = _BuildPopupsPPH($sID, $sDate, "PPH: " & $sTitle[0], $sUrl[0])
                      If $iRet Then $Show+=1
                  EndIf
              Next
              Sleep(30)
      ;~         If $Show > 0 Then ShowLatestJobs()
      ;~         _ArrayDisplay($aPPH)
              FileDelete($sTempFile)
              InetClose($hDownload)
              $hDownload = 0
              $hTimer = 0
              Return $Show
          EndIf
          Sleep(30)
      EndFunc   ;==>_CheckPPH
    • By malicioussoap
      back when I was still doing autohotkey you could write (in code) spanisha::á
      and whenever you were in google docs or notepad you could type (in the document) spanisha  and it instantly turns it into á 
      I tried doing this in autoit, here's what I have so far (my entire code so if this language requires a main function or a return/exit to function properly please let me know):
      ;is this a proper comment? HotKeySet ( "spanisha" , "SendAltered" ) Func SendAltered() Send("á", flag = 0); EndFunc I assume that autoit follows a sort of contemporary hotkey pressing model where you have to hold all keys at the same time? which makes this impossible. So
      I'm wondering if anyone has a workaround for this? or maybe an alternative to HotKeySet()
      I have a vague memory of someone saying that doing something like spanisha:á would work. I don't know if I'm slightly off syntax but this doesn't work.
      Also I tried doing 
      ;is this a proper comment? ;HotKeySet ( "a" , "SendAltered" ) ;spanisha::á Func SendAltered() Send("á"); EndFunc just to try and debug,
      I'm also wondering if my approach is fundamentally wrong since HotKeySet() was made for something like !^A. Or if Send() is syntactically wrong.
      I'm also noticing on these forums that specifying installation is important so to whom it may concern I downloaded both installers, editor and main files. Although I'm about to be real mad if the editor installation makes a difference.
    • By shelly
      I have to press tab to reach a selected button then double click on that
      Here's my script but its not working:
      WinActivate("Policy Decisions -- Webpage Dialog","")
      Sleep(3000)
      Send("{TAB}")
      Send("{TAB}")
      Send("{TAB}")                    // at this point its working according to my need  but double click is not working
      Mouseclick("Left",510, 320, 2, 0)

    • By TheDcoder
      See this thread for info:
    • By nooneclose
      Why can I not click on either of my input fields?
      #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 730, 437, 192, 124) ;GUISetFont(14, 800, 0, "MS Sans Serif") GUISetBkColor(0xC0DCC0) Global $Label1 = GUICtrlCreateLabel("Enter your Password. UserName is auto filled", 62, 40, 604, 41, $SS_CENTER) GUICtrlSetFont(-1, 26, 800, 0, "MS Sans Serif") Global $Label2 = GUICtrlCreateLabel("User Name:", 120, 130, 604, 31, $SS_LEFT) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") Global $Label3 = GUICtrlCreateLabel("Password:", 135, 210, 604, 31, $SS_LEFT) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") Global $Input1 = GUICtrlCreateInput(@UserName, 274, 130, 185, 32) GUICtrlSetFont(-1, 14, 800, 1, "MS Sans Serif") Global $Input2 = GUICtrlCreateInput("Password", 274, 210, 185, 32, $ES_PASSWORD) GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif") Global $OK = GUICtrlCreateButton("OK", 274, 270, 185, 57) GUICtrlSetFont(-1, 20, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $OK Global $UserName = GUICtrlRead($Input1) Global $PassWord = GUICtrlRead($Input2) ExitLoop EndSwitch WEnd ; Delete the GUI window GUIDelete()  
×
×
  • Create New...