TCP Reconnect

4 posts in this topic

I am working on a script that will receive a TCP command then send that command to the serial port receive the response and send back out to the TCP client. All works at startup. I am having an issue with reconnecting to the TCP server for a second time. any help would be greatly appreciated.



#include 'CommMG.au3'

Global $MainSocket = -1
Global $ConnectedSocket = -1
Global $mode
Local $MaxLength = 512; Maximum Length Of String
Local $Port = 5200; Port Number
Local $Server = ""; Server IpAddress
Global $Comport = 27 ; CommPort Connected


$MainSocket = TCPListen($Server, $Port)
If $MainSocket = -1 Then Exit MsgBox(16, "Error", "Unable to intialize socket.")
While 2
    $Data = TCPRecv($ConnectedSocket, $MaxLength) ; Receive Data from AMX
    If $Data <> "" Then
; Unconditional Receive
        ConsoleWrite($Data & @CRLF)
        SetPort($mode = 1)
         _CommSendstring($Data & @CR) ; Send Received Data to CommPort
        $instr = _commGetline(@CR,20,200) ; Read Responce from CommPort
            If $instr <> '' Then;if we got something
                $instr2 = StringTrimRight($instr,3) ; Removed Garbage at end of String
                ConsoleWrite($instr2 & @CRLF)
                TCPSend($ConnectedSocket,$instr2 & @CR) ; Send Responce Back to AMX
        $ConnectedSocket = TCPAccept($MainSocket)
        If $ConnectedSocket = -1 Then ContinueLoop
        If $ConnectedSocket <> -1 Then
; Someone Connected
            TCPSend($ConnectedSocket, "Connected!" & @CR)


Func SetPort($mode = 1)
    Local $sportSetError
    $retval = 0
    $resOpen = _CommSetPort($Comport, $sportSetError, 115200, 8, "none", 1, "NONE")
    Return $retval
EndFunc   ;==>SetPort

Func OnAutoItExit()
    If $ConnectedSocket <> - 1 Then
    If $MainSocket <> -1 Then TCPCloseSocket($MainSocket)


Share this post

Link to post
Share on other sites

When i try to reconnect to the server it will not receive anymore commands. i am not sure exactly in the script where i am getting caught on. 

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