Hi All,

I am a control engineer and i am trying to communicate with a sensor I have spoken to the manufacturer but they cannot help with the python side.I am trying to send a message to the sensor which tells it to connect. The sensors config is ip192.168.60.3 port 9008 the PC client config inside the sensor is set to port 5634. My code below should send the message to the sensor and a reply should come back. However i get no reply and it hangs at the .recvfrom line. Any help would be great as I have never done UDP before.

CODE

s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
print("Connecting....")
s.connect(('192.168.60.10', 9008))
print("Connected")    
s.sendto(bytes(data,'UTF-8'),('192.168.60.3',9008))
print("sent:- " + data)
RetMess=s.recvfrom(1024)[0]
print(RetMess)
s.close()

try with importing a module named urllib. i will try to fix this though

try with this:

   from socket import socket

   data = 'UDP CONTENT'
   s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    print("Connecting....")
    s.connect(('192.168.60.10', 9008))
    print("Connected")
    s.sendto(data,('192.168.60.3',9008))
    print("sent:- " + data)
    RetMess=s.recv(1024)[0]
    print(RetMess)
    s.close()

and instead of

Hi This is the error i get back not sure how importing a library that then is'nt used will help ?? The data needs encoding or so the fault belows shows.
Appreciate the help and anything more you can give i will try

Traceback (most recent call last):
  File "C:\Users\robert.imber\workspace\server_client\src\main.py", line 14, in <module>
    s.sendto(data,('172.16.162.100',8080))
TypeError: 'str' does not support the buffer interface

Thanks
Regards
deepthought

The sendto() documentation says

socket.sendto(string[, flags], address): Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address.

So perhaps you should remove the first call to connect().

python documentation has an example which establishes connections with socket. Here is the program:

import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

>

 import socket
> HOST = ''                 # Symbolic name meaning all available interfaces
> PORT = 50007              # Arbitrary non-privileged port
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((HOST, PORT))
> s.listen(1)
> conn, addr = s.accept()
> print 'Connected by', addr
> while 1:
>     data = conn.recv(1024)
>     if not data: break
>     conn.send(data)
> conn.close()

Hi Again All,

The example above is a server example i believe and only listens to the port i am looking at sending the data to ip 192.168.60.3 port 9008 and then listening for a reply. PC address is 192.168.60.10 sensor is 192.168.60.3 and not sure if this makes a great defference i am using python 3.2

thanks again all

Hi Guys I think I resolved the problem not only do i need to set up the client to send the message i need to then set up a server module to read incoming messages so i closed the connection then reopened a connection then binding to my other port of 5634 to read the incoming code below shows this.

import socket
import time
UDP_IP = "192.168.60.3"
UDP_PORT = 9008
MESSAGE="FF FF FF FF 00 00 32 31 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"

print("UDP target IP:",UDP_IP)
print("UDP target port:",UDP_PORT)
print("message:",MESSAGE)
sock = socket.socket( socket.AF_INET,socket.SOCK_DGRAM ) # UDP
sock.connect((UDP_IP, UDP_PORT))
sock.sendto(bytes(MESSAGE,'ASCII'),(UDP_IP, UDP_PORT) )
sock.close()


UDP_IP_SERV="192.168.60.10"
UDP_PORT_SERV=5634

sock = socket.socket( socket.AF_INET, # Internet
                      socket.SOCK_DGRAM ) # UDP
sock.bind( (UDP_IP_SERV,UDP_PORT_SERV) )

#while True:
time.sleep(1)
data, addr = sock.recvfrom(4096) # buffer size is 4096 bytes
print ("received message:", data,addr)
sock.close()

Now All i need to do is decode the message and use the data for what i need.
Thanks all for your contributions.
Deepthought

I hope the message is expected in alphanumeric string not as binary values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.