1

I am trying to establish connection to a ADAM-4017+ I/O module over the network using a Lantronix EDS2100 module through socket communication in python. For the life of me I cannot get it to work.

The EDS has an IP address and a port (10001) that the adam unit is connected to. I am trying to query the adam for the value of ch 1 (ascii command is #000)

Any help greatly appreciated:

import socket
edsIP = "192.168.1.135"
edsPORT = 10001
MESSAGE="#000\r"


srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
srvsock.bind( ('',23000))
srvsock.listen(1)
newsock, (remhost, remport) = srvsock.accept()
srvsock.send((MESSAGE),(edsIP, EdsPORT) )



 while 1:


    data, addr = srvsock.recv(4096) 
    print ("received message:", data,addr)
    srvsock.close()
2
  • Is the workflow supposed to be that you listen for a connection from the unit, as opposed to connecting to it? From what I see, you bind a port and wait for connections. Commented Aug 28, 2012 at 21:44
  • workflow is as follows: send ascii command and then receive response Commented Aug 28, 2012 at 21:48

2 Answers 2

2

I don't know anything about this device specifically, but from your description, you said its expecting a connection on port 10001. But what you are doing in your code is opening your own socket and listening for connections on port 23000, and then waiting for connections. If you aren't expecting something to connect to you, then you will just be waiting for no reason.

If all your device needs is for you to connect and send a message, then I would think this would do it:

import socket

edsIP = "192.168.1.135"
edsPORT = 10001
MESSAGE="#000\r"

srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srvsock.settimeout(3) # 3 second timeout on commands
srvsock.connect((edsIP, edsPORT)))
srvsock.sendall(MESSAGE)

data = srvsock.recv(4096) 
print "received message:", data

srvsock.close()

Update

Your comments suggest you might be using python3. If so, you may have to adjust the code like this:

MESSAGE=b'#000\r'

And when you receive your bytes response, if you want to turn it into a string:

print data.decode("UTF-8")
Sign up to request clarification or add additional context in comments.

9 Comments

I tried this adding parantheses around the print and removing extra bracket on the connect line. It returns: srvsock.send(MESSAGE) TypeError: 'str' does not support the buffer interface
fyi I am a NOOB python programmer
What version of python are you using?
I am using python 3 and APtna studio PyDev
Yea figured. See my update. You need to send a bytes object instead of a string
|
0

The only problem i can see directly is that you have indented the while statement with one space, but thats probably just from cutting and pasting in here.

After some testing It's clear that when you try to accept on the serversocket it will block, so you're never sending anything to your device.

Comments

Your Answer

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.