Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Reading UDP from external device without device software
#1
Hello everyone,

Before I get to my actual problem, here is some background for the project in which this problem occurs:
I am trying to control a microcontroller, based on the data coming from a sensor. This sensor is not compatible with the microcontroller, since it is a fibre optic sensor. An external device, an interrogator, has to be used to extract the data from the sensor. This interrogator will send data to an included software on my laptop.
I traced the datastream with Wireshark and decoded the sent bytes. The dataprotocol used for this data is UDP.

Now the problem: I need to read the incoming data using python so I can make the microcontroller respond to changing sensorvalues. The interrogator sends the data to 10.0.0.37 port 30002 from 10.0.0.150 port 30071. If I kill the included software with taskmanager, the port opens up for python and the datastream continues according to Wireshark. However, my code does not read the data. (code below). The incoming data are UDP packets of 1488 bytes for the total package and 1444 bytes for the actual data.

import socket
import time
 
port = 30002  # Port to receive data on

bufferSize = 2048 # buffer size
 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('',port))
s.setblocking(0)
 
while(1):
    try:
        result = select.select([s],[],[])
        msg = result[0][0].recv(bufferSize) 
        print (msg)
        time.sleep(1)
    except:
        print('failed to receive data')
        time.sleep(1)
Does anybody have any suggestions how to collect the data from port 30002?
Quote
#2
I figured it out! Pardon me for replying on my own post but I thought people might appreciate it if they ever stumble upon this same problem ;) See below for the code I used:
import socket

UDP_IP = "10.0.0.37" #the IP of the ethernetport of the pc
UDP_PORT = 30002  # the receiving port on the pc

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(4096) # buffer size is 4096 bytes
    print("received message:", data)
replace the UDP_IP and UDP_port with your own values ofcourse
Quote
#3
Quote: Pardon me for replying on my own post but I thought people might appreciate it if they ever stumble upon this same problem
No pardon necessary, Thank You, we are grateful when answers are shared.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  monitor when a device connects (mac address) drifterf 4 398 Jun-29-2019, 12:52 AM
Last Post: Larz60+
  Writing device driver to stop electric supply to any I/O port sumandas89 0 230 May-02-2019, 10:22 AM
Last Post: sumandas89
  Playing mp3 file to specific device treii28 3 431 Mar-26-2019, 04:02 PM
Last Post: Larz60+
  I need a serial device expert! Caesars_10th 10 888 Dec-18-2018, 10:33 PM
Last Post: jeanMichelBain
  Segmentation fault when connecting to modbus device with Libmodbus alice 0 444 Dec-18-2018, 04:03 PM
Last Post: alice
  get webcam name from device manger !! evilcode1 2 664 Nov-03-2018, 10:41 AM
Last Post: j.crater
  is it possible to run commands on a device which is two servers away from our PC in P Nirosh123 7 861 Jun-21-2018, 06:33 PM
Last Post: nilamo
  Change Windows Sound Output Device with Python delfar 1 2,089 Sep-15-2017, 12:11 AM
Last Post: Larz60+
  'OSError: [Errno 16] Device or resource busy' for ralationship with ds1307 gray 4 3,128 Jul-31-2017, 02:17 AM
Last Post: jackbk
  Display device details i.e connected with more than one device shintonp 6 1,929 May-10-2017, 06:00 AM
Last Post: shintonp

Forum Jump:


Users browsing this thread: 1 Guest(s)