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 tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Getting started
      Download the script from AutoIt or pull it from the official github repo git@github.com:tarreislam/Autoit-Socket-IO.git and checkout the tag 4.0.0-beta Check out the documentaion Take a look in the examples/ folder Changelog
      To see changes from 3.x.x and 2.x.x please checkout the 3.x branch
      Version 4.0.0-beta (This update break scripts.)
      Code base fully rewritten with Autoit-Events and decoupled to improve code quality and reduce bloat. The new UDF is very different from 3.x.x so please checkout the UPGRADE guide to fully understand all changes Added new documentation documentaion Success stories
      Since December 2017-now I have used version 1.5.0 in an production environment for 150+ clients with great success, the only downtime is planned windows updates and power outages.
       
      Newest version (2020-09-15!)

       
      Older versions (Not supported anymore)
      Autoit-Socket-IO-1.0.0.zip Autoit-Socket-IO-1.1.0.zip Autoit-Socket-IO-1.3.0.zip Autoit-Socket-IO-1.4.0.zip Autoit-Socket-IO-1.5.0.zip
      Autoit-Socket-IO-2.0.0.zip
    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Read more in the official thread
    • By learner123
      Hi All,
       
      I am new to this AUTO IT and I have created a script that will open an app,enter pin and copy the code generated to clipboard. My java code call this autoIT script and use the copied generated code from clipboard.
      This works fine when server  window is on focus. My server is an windows server. 
      But when I minimize or disconnect the server, the script opens the app.exe but doesn't copy any value to clipboard.  
      Can anyone help me on this 😐
       
      Run("C:\Program Files (x86)\RSA SecurID Software Token\SecurID.exe")
      Local $hWnd=WinWait("abc - RSA SecurID Token") ; waits until the window is the active window
      $hWin = WinGetHandle("abc - RSA SecurID Token");
      ControlSend($hWnd,"","","1111") ; simulates pressing the Home key
      ControlSend($hWnd,"","","{ENTER}");
      ControlSend($hWnd,"","","^c");
      Sleep(1000) ;
      ControlSend($hWnd,"","","^c");
       
    • By learner123
      Hi All,
      So I have created a small autoIT script to enter pin into a RSA token(app which generate new code every 30 second), and copy the generated code.
      I have a java application which requires this code so every time my java-code requires this RSA code, it runs the autoIT script and the copied generated code is then used in my java application. 
      I have deployed this code on a windows server and it works fine when I am logged in and the window is on focus, But as soon as I schedule task and disconnect the server (not logged out only disconnect), or even minimize the server window, the autoIT scripts fails and its not able to copy the value.
       
      Please find below the code for AUTOIT.
       
      WinActivate("rsa - RSA SecurID Token") ; activates the window that has old in the tilte bar
      WinWaitActive("rsa - RSA SecurID Token") ; waits until the window is the active window
      Send("1111") ; simulates pressing the Home key, enters password to get the code
      Send("{ENTER}") ; simulates pressing the Enter key
      Sleep(1000) ;
      Send("^c") ; simulates pressing the CTRL+c keys (copy)
       
      Also I saw some post regarding that WINACTIVE only works when window is active. But my below AUTO IT script to handle windows pop up  works perfectly fine when the server is disconnected. 
       
      Opt("WinTitleMatchMode", 1)
      WinWait("https://url","","10")
      WinWaitActive("https://url","","10")
      Sleep(2000)
      Send("userid")
      Sleep(1000)
      Send("{TAB}")
      Sleep(1000)
      Send("passwrd")
      Send("{TAB}")
      Sleep(500)
      Send("{ENTER}")
       
       
×
×
  • Create New...