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')
1 Answer 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.