1

I have following script that I expect to connect to our product, send the tcl commands(set frequency and some others), but it just shows:

import Server

Server.ScktConn()

frequency(450-2500): 600

recv: 0 ace_set_frequency C1 600

which is the fist command, but it's not even setting frequency (just shows the cmd, but not applying it)!

here is the script:

import socket
import threading
import time
address = ('127.0.0.1', 5426)
def ScktRecv():
 r = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 r.bind(address)
 r.listen(5)
 conn, _ = r.accept()
 csvid = conn.recv(4096)
 print "recv: %s" % csvid
 conn.close()
def ScktConn():
 recv_thread = threading.Thread(target=ScktRecv)
 recv_thread.start()
 time.sleep(1)
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(address)
 # our local IP is 192.168.2.1, but it works even with 127.0.0.1, I don't know from where #it is coming
 Freq=raw_input('Frequency(450-2500): ')
 CmdF='0 ace_set_frequency C1 '+str(Freq)+' \r\n'
 s.send(CmdF)
 s.send('0 ace_azplayer_remove_player AzPlayer1 \r\n')
 s.send('0 ace_azplayer_add_player \r\n')
 s.send('0 ace_azplayer_add_ace AzPlayer1 C1\r\n')
 Path='C:/Users/amir_rajaee/Desktop/gridview_script/PBF/4x4U_wocorr_SNR.csv'
 s.send('0 ace_azplayer_load_csvfile AzPlayer1 '+Path+' \r\n')
 s.close()

but when I use following script, it sets the frequency, but my problem is, I don't have receiver; I need to have output of last command(load csv file)!:

import socket
def ScktConn():
 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.connect(('127.0.0.1', 5006))
 Freq=raw_input('Frequency(450-2500): ')
 CmdF='0 ace_set_frequency C1 '+str(Freq)+' \r\n'
 s.send(CmdF)
 s.send('0 ace_azplayer_remove_player AzPlayer1 \r\n')
 s.send('0 ace_azplayer_add_player \r\n')
 s.send('0 ace_azplayer_add_ace AzPlayer1 C1\r\n')
 Path='C:/Users/amir_rajaee/Desktop/gridview_script/PBF/4x4U_wocorr_SNR.csv'
 s.send('0 ace_azplayer_load_csvfile AzPlayer1 '+Path+' \r\n')
asked Dec 19, 2012 at 21:45

1 Answer 1

1

You seem to have two problems here.

1) You expect that printing the tcl commands will execute them. This isn't the case. Please look at the subprocess library for how to achieve this.

2) You only call socket.recv once. To capture all the sends you need the receiver to continually listen for sent messages. Change SockRecv to:

address = ('127.0.0.1', 5426)
def ScktRecv():
 r = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 r.bind(address)
 r.listen(5)
 conn, _ = r.accept()
 while True:
 csvid = conn.recv(4096)
 if csvid:
 print "%s" % csvid
 conn.close()

You will need to make further amendments to properly deal with the life-cycle of the thread.

answered Dec 20, 2012 at 15:43
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks , Now i receive all commands, but just the commands name! I don't see they work! when I use the second script w/o Recv, the commands do what they should do, but when I run first script w/ recv and scktconn, all I have is the commands text, they don't do what they should do!
def ScktRecv(): r = socket.socket(socket.AF_INET, socket.SOCK_STREAM) r.bind(address) r.listen(5) conn, _ = r.accept() while True: csvid = conn.recv(4096) if csvid: print "recv: %s" % csvid conn.close()
>>> import Server >>> Server.ScktConn() Frequency(450-2500): 500 recv: 0 ace_set_frequency C1 500 >>> recv: 0 ace_azplayer_remove_player AzPlayer1 0 ace_azplayer_add_player 0 ace_azplayer_add_ace AzPlayer1 C1 0 ace_azplayer_load_csvfile AzPlayer1 C:/Users/amir_rajaee/Desktop/gridview_script/PBF/4x4U_wocorr_SNR.csv
Refer to part (1) of my answer regarding executing the commands instead of printing them.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.