Python Forum
Sending data over UDP - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Sending data over UDP (/thread-5745.html)



Sending data over UDP - alidaf - Oct-19-2017

I've managed to code a C program to send various types of data to Matlab on another machine over UDP; char, int and double. I'm really struggling to do the same with Python. I can send a char with no problem but trying to just send an integer is making my head spin. If I send any integer Matlab captures it as 10, regardless of the number. I've tried converting to string, sending as a bytearray and using pack but nothing works. What am I missing?


RE: Sending data over UDP - Larz60+ - Oct-19-2017

The ten may be the number base.
Check to see if you need to be sending a tuple

check out: http://www.wellho.net/resources/ex.php4?item=y303/udp_client.py


RE: Sending data over UDP - alidaf - Oct-19-2017

(Oct-19-2017, 11:41 AM)Larz60+ Wrote: The ten may be the number base.
Check to see if you need to be sending a tuple

check out: http://www.wellho.net/resources/ex.php4?item=y303/udp_client.py

That example sends a string, which I don't have a problem with. There are some vague examples that send binary or hex by using struct to pack but I can't get any of them to work. I think it must be something in Matlab, but I have it working fine with a C program doing the same thing.

This is the segment of code:
for i in range(0, 21):
    print('Sending command {} to {}.' .format('A', host_address))
    host_socket.sendto('A', host_address)      # Accept command.
    host_socket.sendto('\n', host_address)
#    val = i
#    val = bytes(i)
#    val = struct.pack('!i', i)
    val = str(i)
#    val = bytearray()
#    val.append(i)
    print('Sending value {} to {}.' .format(val, host_address))
    host_socket.sendto(val, host_address) # Value.
    host_socket.sendto('\n', host_address)

print('Sending command {} to {}.' .format('Q', host_address))
host_socket.sendto('Q', host_address)      # Quit command.
host_socket.sendto('\n', host_address)
The commented out bits are what I have tried. Surely converting to a string should work at the very least!


RE: Sending data over UDP - Larz60+ - Oct-19-2017

I haven't used UDP in a long time, But if I recall properly, I seen to remember using uuencode on binary data before sending
and then decoding on the receiving end.  Uuencoding is a form of binary-to-text encoding

Read about here: https://en.wikipedia.org/wiki/Uuencoding.