hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

send float through ethernet

Tue Dec 24, 2019 4:13 pm

hello mumbers
please help
i'm trying to transfer 3 flaots form raspberry to pc through ethernet cable.
i used this code
on client

Code: Select all

s.send(b'5.5')
s.send(b'30.21')
s.send(b'10.02')
and on server

Code: Select all

x1=client.recv(1024)
x2=client.recv(1024)
x3=client.recv(1024)
print(x1)
print(x2)
print(x3)
i got this result
b'5.530.2110.02'
b' '
b' '
how can i solve it

ejolson
Posts: 4305
Joined: Tue Mar 18, 2014 11:47 am

Re: send float through ethernet

Tue Dec 24, 2019 4:17 pm

hussein91 wrote:
Tue Dec 24, 2019 4:13 pm
hello mumbers
please help
i'm trying to transfer 3 flaots form raspberry to pc through ethernet cable.
i used this code
on client

Code: Select all

s.send(b'5.5')
s.send(b'30.21')
s.send(b'10.02')
and on server

Code: Select all

x1=client.recv(1024)
x2=client.recv(1024)
x3=client.recv(1024)
print(x1)
print(x2)
print(x3)
i got this result
b'5.530.2110.02'
b' '
b' '
how can i solve it
What programming language is this? Have you tried placing newlines after each number in the send commands?

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: send float through ethernet

Tue Dec 24, 2019 4:29 pm

ejolson wrote:
Tue Dec 24, 2019 4:17 pm
hussein91 wrote:
Tue Dec 24, 2019 4:13 pm
hello mumbers
please help
i'm trying to transfer 3 flaots form raspberry to pc through ethernet cable.
i used this code
on client

Code: Select all

s.send(b'5.5')
s.send(b'30.21')
s.send(b'10.02')
and on server

Code: Select all

x1=client.recv(1024)
x2=client.recv(1024)
x3=client.recv(1024)
print(x1)
print(x2)
print(x3)
i got this result
b'5.530.2110.02'
b' '
b' '
how can i solve it
What programming language is this? Have you tried placing newlines after each number in the send commands?
Thanks for answering
I'm programming with python.
I don't understand what you mean by newlines

ejolson
Posts: 4305
Joined: Tue Mar 18, 2014 11:47 am

Re: send float through ethernet

Tue Dec 24, 2019 4:44 pm

hussein91 wrote:
Tue Dec 24, 2019 4:29 pm
Thanks for answering
I'm programming with python.
I don't understand what you mean by newlines
I don't know much about Python, but what happens if you try

Code: Select all

s.send(b'5.5\n')
s.send(b'30.21\n')
s.send(b'10.02\n')
instead?

scotty101
Posts: 3811
Joined: Fri Jun 08, 2012 6:03 pm

Re: send float through ethernet

Tue Dec 24, 2019 6:23 pm

You are sending the float as a string so the recieving end needs to be told when one number ends and the next begins. You can do this by adding a delimiter, a character that indicates the gap between two values.

A newline or something like a comma or semi-colon are commonly used as delimiters.

If you are using python and use a comma as a delimiter, they you can split the string in to separate values using the split function.

x1,x2,x3 = client.recv(1024).split(',')

Another alternative is to send the float as a float rather than a string. In python a float is 24bytes in size. So recieving 24 bytes will read in one float.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

hussein91
Posts: 18
Joined: Tue Dec 10, 2019 9:12 am

Re: send float through ethernet

Wed Dec 25, 2019 7:50 am

scotty101 wrote:
Tue Dec 24, 2019 6:23 pm
You are sending the float as a string so the recieving end needs to be told when one number ends and the next begins. You can do this by adding a delimiter, a character that indicates the gap between two values.

A newline or something like a comma or semi-colon are commonly used as delimiters.

If you are using python and use a comma as a delimiter, they you can split the string in to separate values using the split function.

x1,x2,x3 = client.recv(1024).split(',')

Another alternative is to send the float as a float rather than a string. In python a float is 24bytes in size. So recieving 24 bytes will read in one float.
thank you so much
it work

User avatar
PeterO
Posts: 5474
Joined: Sun Jul 22, 2012 4:14 pm

Re: send float through ethernet

Wed Dec 25, 2019 7:56 am

hussein91 wrote:
Tue Dec 24, 2019 4:29 pm
I don't understand what you mean by newlines
There's a Wikipedia page (*) for "newline" with more information than you'll ever need to know :lol: https://en.wikipedia.org/wiki/Newline
PeterO
(*) Who knew ? :lol:
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “General discussion”