Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending data over UDP
#1
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?
Reply
#2
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?..._client.py
Reply
#3
(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?..._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!
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python + PHP. Sending data from "Moodle" (php) to python CGI-script and get it back stanislav 0 1,525 May-07-2022, 10:32 AM
Last Post: stanislav

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020