Send ASCII command to ADAM-6017 with Python

M

Thread Starter

Maria Eugenia

Hi everybody! It was fun for a while, but now I'm strating to go crazy!

I wrote a code to send an ASCII command to an ADAM-6017 module through the ethernet using Python. I considered the module a server and my computer the client. The client would like to ask for the value of the 8 analogue inputs in the module, and get back that response. If I do this actions with the software that comes with the ADAM module, it gives me the answer that I would expect. However, when doing it with python, it doesn't. The code goes as follows....:

-------------------------------------------------

import socket
host='141.xxx.xxx.xx'
##(I am not sure which IP address to put here. If I put the Internet IP address of the computer I'm working with, it doesn't give me an error. If I put the module's IP address, which is the same as the computers changing the last digit, it sais 'errno 10049 the requested address is not valid in its context')
buf=1024
port=5002
addr=(host,port)
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(addr)
s.sendto('#01\r', addr)
indata, inaddr = s.recvfrom(buf)

------------------------------------------------
NOTE= the \ on the command is an inverted bar, for some reason it appears like than on the forum!
also there's no space between soc and ket.
--------------------------------------------------

indata and iaddr give me '#01\r' and the IP address. But what I want is the response of the module when we give it the command '#01'! I don't know what I'm doing wrong.
Is it ok to put the computer's IP addres?
Does this server/client structure make sense?
Do I have to write a code for the server (ADAM), and in that case, where do I run it?

Thank you very much for sharing your knowledge, I seriously appreciate it. Best,

Maria E.
memorell [at] umich.edu
 
You have posted the question under two threads, but I will answer both here.

I think your problems are as follows:

1) You are using port number 5002. I think that should be 1025 for the ASCII commands. Actually, I think that 1024 to 1029 all work. You might need to try different port numbers if something else on your computer is already using 1025. This is something that Advantech documents poorly, if at all.

2) This is UDP/IP, not TCP/IP. You have to specify an IP and port for both your computer and the Advantech module. The port number will be the same for both (this is dictated by which port the Advantech module is listening on). The IP addresses will be different.

For "bind" you want localhost. Just do it as:

s.bind(('', 1025))


3) For "sendto" you want the IP address of the Advantech module. The default is '10.0.0.1', but you many have changed this.

s.sendto('#012\r', ('10.0.0.1', 1025))

According to the manual, this should read analogue channel 2 on the module.

The manual says you should get a reply which looks something like this:

>+10.000

However, you may actually see something like this:

!+10.000

You will need to test this (print the result to the screen), as I don't recall if the manual is correct on this point.

In you post you said: "the \ on the command is an inverted bar, for some reason it appears like than on the forum!"

I see this as a backslash "\" character. The backslash is correct. A '\r' is a way of encoding carriage return (ASCII code 13). It gets turned into a single character. There should be a carriage return on the end of the response as well, although you won't see it of course (it is invisible). However, your code should detect it in the string.

As for the Adam module, it has the server code running on it already and it is waiting for your commands. The commands you can use are listed in the Adam manual. You should be able to test your Python code by typing it into Python interactively (this is one of the really nice things about Python in this type of application).

To reiterate a previous point:

Port Number:
Both bind and send have to specify the same port. The port number is the one that the module wants to use. I don't know if there is any way of changing that from the default.

IP Address:
For bind, just use an IP of '' (empty string). For send, use the IP of the Adam module.

UDP works differently from TCP. Most examples you will have seen use TCP. Just keep in mind that for UDP the implementation is slightly different.

If you have any more questions, please let me know and I will be happy to help.

P.S. When you go to read the data from the response string, you will probably want to split up the string to separate the different numbers. I will give you a hint and suggest that you look at the regular expressions module.
 
Hi Michael!

Thank you so much for your clear response, it's reading it properly now. I see how the client/server structer works, thanks for that too. What I wrote is:
_________________________________________________
import socket
_IP='xxx.xxx.xxx.xx' ##(IP of the ADAM module)
buf=1024
port=1025
addr=(_IP,port)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('',port))
s.sendto('#01\r',addr)
indata, inaddr = s.recvfrom(buf)
_________________________________________________

</b>Best regards,
Maria E.
 
Top