TCP interaction from python/plugin

Forum rules

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo

Posted on
Thu Aug 16, 2012 11:21 am
pgershon offline
Posts: 509
Joined: Jul 10, 2004

TCP interaction from python/plugin

(apologize if I am repeating a question from another thread)

I am looking to read from and send to a TCP port (192.168.1.103:9090) from within a plug in python script. I already have a serial port open (serial over IP on 192.168.1.70:4999). I know I can telnet commands to and from the 9090 port while the 4999 port is open. Not sure the best way from within my plugin however. I have looked at the Cynical Network Sent Text function but do not know how to reference from within my separate plugin, only as action group command from Indigo Interface (and I cannot dynamically change the text to send out). There must be a simple way to send a line of text to a TCP port and receive a response, no? Very similar to the serial port I/O, except not serial.

Posted on
Thu Aug 16, 2012 12:21 pm
jay (support) offline
Site Admin
User avatar
Posts: 18253
Joined: Mar 19, 2008
Location: Austin, Texas

Re: TCP interaction from python/plugin

Seems like you could just use the standard Python socket interface. No clue as to the port number differences, something that's unique to that device type of course.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Aug 16, 2012 12:44 pm
pgershon offline
Posts: 509
Joined: Jul 10, 2004

Re: TCP interaction from python/plugin

Thanks Jay. I tried earlier and I got hung trying to receive. Here is the code I tried. Indigolog statements were so I could see where I was hanging:

HOST = "192.168.1.103" # The remote host
PORT = 9090 # The same port as used by the server
indigo.server.log ("1")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
indigo.server.log ("2")
s.connect((HOST, PORT))
s.send("player name 1 ?")
indigo.server.log ("3")
data = s.recv(1024)
indigo.server.log ("4")
s.close()
indigo.server.log ("Received"+ repr(data))

I got to display 1,2 and 3, so I assume "data = s.recv(1024)" cause the hang. One thing I see as I am typing is I did not put the CR+LF at the end of my text line. I will see if that makes a difference.

UPDATE: That solved it. Needed to add the "\r\n" to s.send("player name 1 ?\r\n")

Posted on
Thu Aug 16, 2012 2:15 pm
berkinet offline
User avatar
Posts: 3297
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

Re: TCP interaction from python/plugin

Take a look at the ad2usb plugin... startCom in ad2usb.py That's where the connection is opened. Then take a look at panelMsgWrite and the first part of basicReadZoneMessage to see how I read and write.

I won't claim this is great Python, but, it works and is stable.

Of course, YMMV

Posted on
Thu Aug 16, 2012 9:59 pm
pgershon offline
Posts: 509
Joined: Jul 10, 2004

Re: TCP interaction from python/plugin

Thanks - I got it going now. Was pretty simple once I put in the CR and LF characters at end of text to send. The code above worked.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests