Jump to content

Issue with TCP IP communication


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

Link to comment
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.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
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

Link to comment
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
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
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

Link to comment
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
 Share

×
×
  • Create New...