Jump to content
Sign in to follow this  
aalau3

Receiving data from a connected TCP device

Recommended Posts

aalau3

I am trying to develop a simple program to connect to a TCP/IP communication device. Once connected the device will respond with prompt asking the user for a password. Here is what I have so far:

#include <GUIConstants.au3>
$ip = "192.168.0.1"
$port = 230
$msg = "PASSWORD"

TCPStartup()
Do
  $socket = TCPConnect($ip, $port)
Until $socket <> -1
sleep(500)
TCPSend($socket, $msg)
sleep(500)
$Recv=TCPRecv($Socket,1024)
MsgBox(0,"Received this",$Recv))
If @error Then MsgBox(0,"Error", "Message Failed To send")
TCPShutdown()

It appears I can connect because I don't get an error and get the second msgbox but the data I am getting is garbage. I should be getting ASCII data from the device - "PASSWORD:".

I have adjusted the flag on the TCPRecv command but have had no luck. I know this device is communicating because I connect to it very easily using Hyper-Terminal.

What am I missing? Any help would be appreicated..

Share this post


Link to post
Share on other sites
SoulA

I would say your sleeps are too long. Specifically the one between your TCPSend and TCPRecv... with a 500 sleep the message could have came and not been read I believe. Try this and see what happens...

$ip = "192.168.0.1"
$port = 230
$msg = "PASSWORD"

TCPStartup()
Do
  $socket = TCPConnect($ip, $port)
Until $socket <> -1

sleep(100)

_TCPSend($socket, $msg)

$Recv = _TCPRecv($Socket)
MsgBox(0,"Received this",$Recv)

TCPShutdown()

Func _TCPSend($socket, $msg)
    TCPSend($socket, $msg)
    If @error Then 
        MsgBox(0, "Error", "Message failed to send")
        Exit
    EndIf
EndFunc

Func _TCPRecv($socket)
    $timeout = TimerInit()
    Do
        $Recv = TCPRecv($socket, 1024)
        If TimerDiff($timeout) > 1000 Then
            MsgBox(0, "Error", "Timeout")
            Exit
        EndIf
    Until $Recv <> ""
    Return $Recv
EndFunc
Edited by SoulA

Share this post


Link to post
Share on other sites
aalau3

Thanks for the suggestions...I tried the revised code but I still get these funny characters -see attached image. I shortened the password, and added a Carriage Return thinking that might change the output but no luck...

What I can't seem to understand is that in Hyper-terminal (simple ASCII commands), it can be done. Here is the simplified process.

1. Connect to the IP address via port # 23.

2. Upon connection, the device will automatically respond with "PASSWORD:"

3. At which point, the password is input and I am off and running.

I think there is some more fundamental that I am missing...like something with the type of data being returned. The type of data I am getting is part of the Extended Character Set- ANSI (funny looking ys - binary:255 and hex:FF)- confusing.

If anyone has every ran across this before please share your knowledge...thanks for all your help.

post-42682-1226281414_thumb.jpg

Share this post


Link to post
Share on other sites
SoulA

You sure this isn't using some kind of encryption?

Share this post


Link to post
Share on other sites
aalau3

No kind of encryption that I am aware of.. I can use any kind of telnet software as well (i.e.- ProComm Plus or any other program.

I setup a regular TCP/IP(Winsock) connection in Hyper-Terminal and specify the IP address and Port - and that's it.

Share this post


Link to post
Share on other sites
SoulA

What kind of info what it normally return in hyper terminal? I mean its obviously communicating with you and sending you something...

Edited by SoulA

Share this post


Link to post
Share on other sites
aalau3

Just ASCII data - this is an RS-232 to TCP/IP converter - so to speak. Once you connect via Ethernet (TCP/IP), you would retreive and adjust settings and paramters of the device.

Here is a quick snapshot of the communication session in HT. For example, in this snapshot I am sending the command "keep" requesting the status of the keep alive function then the device responds with "0" indicating the function is disabled. I also follow up with the command "bits" -requesting the data length.

Thank you for all your time and help with this.

post-42682-1226285694_thumb.jpg

Share this post


Link to post
Share on other sites
SoulA

Maybe you have to wait for the PASSWORD: prompt before you start sending data. Do the TCPConnect() function and then after you connect have a _TCPRecv() there waiting for the password prompt. You should then see the PASSWORD: that I am seeing in your normal hyperterminal communication. See if that shows up garbled.

NOTE: I am not familiar with hyperterminal what so ever so that might have some kind of translation process. If you can read this stuff in telnet than you should be able to read it here using these methods.

Be something like this...

$ip = "192.168.0.1"
$port = 230
$msg = "PASSWORD"

TCPStartup()
Do
  $socket = TCPConnect($ip, $port)
Until $socket <> -1

$Recv = _TCPRecv($socket)
MsgBox(0,"PASSWORD:?",$Recv)

;_TCPSend($socket, $msg)

;$Recv = _TCPRecv($Socket)
;MsgBox(0,"Received this",$Recv)

;TCPShutdown()

Func _TCPSend($socket, $msg)
    TCPSend($socket, $msg)
    If @error Then
        MsgBox(0, "Error", "Message failed to send")
        Exit
    EndIf
EndFunc

Func _TCPRecv($socket)
    $timeout = TimerInit()
    Do
        $Recv = TCPRecv($socket, 1024)
        If TimerDiff($timeout) > 1000 Then
            MsgBox(0, "Error", "Timeout")
            Exit
        EndIf
    Until $Recv <> ""
    Return $Recv
EndFunc
Edited by SoulA

Share this post


Link to post
Share on other sites
aalau3

I tired to view the data before sending anything and still getting the same garbage. I think you may have hit the nail on the head the first time - thinking the data is encyrpted some how.

The funny thing is - this device has one port to configure the settings and another port to monitor the data coming out. If I adjust the port# on the script to view the output data, I can see the data with no problem on the Autoit script.

The problem seems to be coming from the initial password prompt. Would it be possible to use a some type of packet sniffing program to view the data using HT or other telnet program then somehow imitate that string in the Autoit script?

I am not sure if I am on the right track or not?

Thanks for all your help.

Share this post


Link to post
Share on other sites
SoulA

Yes, look at getting wireshark.

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
Sign in to follow this  

×