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 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()  
    • By cruisepandey
      Hi, 
      I have a string like this : 
      Global $Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
      I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice. 
      Can anyone help me with regular expression to find the number from String using AutoIT. TIA
    • By nooneclose
      I want wanting to run multiple tests on my company website. Sadly the process I want to automate requires opening a File Explorer to add an attachment. Is there a way in to automate a Chrome opened File Explorer?
      (I am using webdriver to automate the other 99% of the process just fine. But when I have it click on attach file and chrome opens a file explorer I do not know how to handle it.) 
      Any help would be greatly appreciated. 
    • By cruisepandey
      Hi There ! 
      I have a script here : 
       
      ;Launch CMD
      Run("C:\Windows\System32\cmd.exe")
      sleep(2000)
      $cmdHandle = WinActivate("C:\Windows\System32\cmd.exe")
      Sleep(2000)
      ;Sending document
      ControlSend($cmdHandle, "", "", "ftp" & @CRLF)
      ControlSend($cmdHandle, "", "", "open" & @CRLF)
      Sleep(2000)
      ControlSend($cmdHandle, "", "", "first command" & @CRLF)
      Sleep(2000)
      ControlSend($cmdHandle, "", "", "second-coomand" & @CRLF)
       
      first-command and second-command I can't provide cause it's internal. I have complied this .au3 file into an exe and it does the work. But I need to invoke this with Java. Java code I have tried is : 
       
          ProcessBuilder pb = new ProcessBuilder("C:\\Users\\username\\eclipse-workspace\\Examples\\src\\com\\own\\examples\\etc.exe");
          pb.start();
          Thread.sleep(5000);
       
      Through java it just launches the cmd and nothing happens after that. Please help !!
    • By TheXman
      Purpose (from Microsoft's website)
      The HTTP Server API enables applications to communicate over HTTP without using Microsoft Internet Information Server (IIS). Applications can register to receive HTTP requests for particular URLs, receive HTTP requests, and send HTTP responses. The HTTP Server API includes SSL support so that applications can exchange data over secure HTTP connections without IIS.
      Description
      There have been several times in the past that I wanted to either retrieve information from one of my PCs or execute commands on one of my PCs, whether it was from across the world or sitting on my couch.  Since AutoIt is one of my favorite tools for automating just about anything on my PC, I looked for ways to make to make it happen.  Setting up a full blown IIS server seemed like overkill so I looked for lighter weight solutions.  I though about creating my own AutoIt UDP or TCP server but that just wasn't robust enough,  Then I found Microsoft's HTTP Server API and it looked very promising.  After doing a little research into the APIs, I found that it was flexible & robust enough to handle just about any of the tasks that I required now and in the future.  So a while back I decided to wrap the API functionality that I needed into an AutoIt UDF file to allow me to easily create the functionality I needed at the time.  It has come in very handy over the years.  Of course it wasn't all wrapped up with a nice little bow like it is now.  That only happened when I decided to share it with anyone else who could use it or learn from it.
      The example file that I included is a very granular example of the steps required to get a lightweight HTTP Server up and listening for GET requests.  The UDF is a wrapper for the Microsoft APIs.  That means to do anything over and above what I show in the example, one would probably have to have at least a general knowledge of APIs or the ability to figure out which APIs/functions to use, what structures and data is needed to be passed to them, and in what order.  However, the UDF gives a very solid foundation on which to build upon.  Of course, if anyone has questions about the UDF or how to implement any particular functionality, I would probably help to the extent that I could or point you in the right direction so that you can figure out how to implement your own solution.
      The APIs included in the UDF are the ones that I needed in the past to do what I needed to do.  If any additional APIs need to be added to the UDF file, please make those suggestions in the related forum topic.
      Being that this is basically an AutoIt wrapper for the Microsoft API functions, there's no need to create AutoIt-specific documentation.  All of the UDF functions, structures, constants, and enumerations are named after their Microsoft API counterparts.  Therefore, you can refer to Microsoft's extensive documentation of their HTTP Server API.  As stated earlier, if there is one or more APIs that you find yourself needing for your particular solution, please suggest it in the related Example Scripts forum topic.
      Related Links
      Microsoft HTTP Server API - Start Page
      Microsoft HTTP Server API - API v2 Reference
      Microsoft HTTP Server API - Programming Model
×
×
  • Create New...