Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #72256 > unrolled thread

Binary data exchange

Started by"RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com>
First post2014-05-29 15:08 -0700
Last post2014-05-29 17:20 -0600
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Binary data exchange "RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com> - 2014-05-29 15:08 -0700
    Re: Binary data exchange "RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com> - 2014-05-29 15:13 -0700
    Re: Binary data exchange MRAB <python@mrabarnett.plus.com> - 2014-05-30 00:09 +0100
      Re: Binary data exchange "RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com> - 2014-05-29 16:25 -0700
        Re: Binary data exchange Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-05-30 00:56 +0100
    Re: Binary data exchange Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-29 17:20 -0600

#72256 — Binary data exchange

From"RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com>
Date2014-05-29 15:08 -0700
SubjectBinary data exchange
Message-ID<e94b4d33-3ccf-44c7-94c2-4f6f6f566ff0@googlegroups.com>
friends

I have a pair of simple python programs as follows:

#!/usr/bin/python
# broadcast.py
import socket
from ctypes import *
import random

class PurgeData(Structure):
    _fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
    
myPort = 10756

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('localhost',myPort)
#sock.sendto(data,addr)

presdata = PurgeData()
presdata.press = 0
presdata.ticks = 100
    
for msg in range(1,20):
    presdata.press = presdata.press+1
    presdata.ticks = presdata.ticks+1
    presdata.volume = random.random()
    sock.sendto(presdata,addr)

#--------------------

#!/usr/bin/python
# Receiver
import socket

from ctypes import *

class PurgeData(Structure):
    _fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
    
myPort = 10756

sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
addr = ('localhost',myPort)
sock.bind(addr)
presdata=PurgeData()
    
while True:
    data,addr = sock.recvfrom(1024)
    memmove(addressof(presdata),data.strip(),len(data.strip()))
    print presdata.press, presdata.ticks, presdata.volume

---------------------

When I tried to run this I get some bizarre results:


1 101 0.343009024858
2 102 0.36397305131
3 103 0.495895296335
4 104 0.372055351734
5 105 0.933839201927
6 106 0.931187808514
7 107 0.876732826233
8 108 0.298638045788
1828716544 -754974720 0.183644190431
1845493760 1660944384 0.186560109258
1862270976 1056964608 0.18631502986
1879048192 1728053248 0.186902835965
1895825408 2097152000 0.18658298254
14 114 0.407857120037
15 115 0.833854913712
16 116 0.00646247947589
17 117 0.297783941031
18 118 0.58082228899
19 119 0.61717569828

the received data for the messages 9 thru 13 are not as expected.

I wonder if anyone can see what I am doing wrong?

Appreciate any hints. thanks, srini

[toc] | [next] | [standalone]


#72257

From"RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com>
Date2014-05-29 15:13 -0700
Message-ID<e2f6a87b-9ad7-4966-8bac-73c28f6f7939@googlegroups.com>
In reply to#72256
BTW - My environment is:

H:\>python
Enthought Canopy Python 2.7.6 | 64-bit | (default, Apr 11 2014, 20:31:44) [MSC v
.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

[toc] | [prev] | [next] | [standalone]


#72259

FromMRAB <python@mrabarnett.plus.com>
Date2014-05-30 00:09 +0100
Message-ID<mailman.10462.1401405151.18130.python-list@python.org>
In reply to#72256
On 2014-05-29 23:08, RasikaSrinivasan@gmail.com wrote:
> friends
>
> I have a pair of simple python programs as follows:
>
> #!/usr/bin/python
> # broadcast.py
> import socket
> from ctypes import *
> import random
>
> class PurgeData(Structure):
>      _fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
>
> myPort = 10756
>
> sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> addr = ('localhost',myPort)
> #sock.sendto(data,addr)
>
> presdata = PurgeData()
> presdata.press = 0
> presdata.ticks = 100
>
> for msg in range(1,20):
>      presdata.press = presdata.press+1
>      presdata.ticks = presdata.ticks+1
>      presdata.volume = random.random()
>      sock.sendto(presdata,addr)
>
> #--------------------
>
> #!/usr/bin/python
> # Receiver
> import socket
>
> from ctypes import *
>
> class PurgeData(Structure):
>      _fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
>
> myPort = 10756
>
> sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> addr = ('localhost',myPort)
> sock.bind(addr)
> presdata=PurgeData()
>
> while True:
>      data,addr = sock.recvfrom(1024)
>      memmove(addressof(presdata),data.strip(),len(data.strip()))
>      print presdata.press, presdata.ticks, presdata.volume
>
> ---------------------
>
> When I tried to run this I get some bizarre results:
>
>
> 1 101 0.343009024858
> 2 102 0.36397305131
> 3 103 0.495895296335
> 4 104 0.372055351734
> 5 105 0.933839201927
> 6 106 0.931187808514
> 7 107 0.876732826233
> 8 108 0.298638045788
> 1828716544 -754974720 0.183644190431
> 1845493760 1660944384 0.186560109258
> 1862270976 1056964608 0.18631502986
> 1879048192 1728053248 0.186902835965
> 1895825408 2097152000 0.18658298254
> 14 114 0.407857120037
> 15 115 0.833854913712
> 16 116 0.00646247947589
> 17 117 0.297783941031
> 18 118 0.58082228899
> 19 119 0.61717569828
>
> the received data for the messages 9 thru 13 are not as expected.
>
> I wonder if anyone can see what I am doing wrong?
>
> Appreciate any hints. thanks, srini
>
I don't understand why you're using the .strip method. That's for
stripping whitespace from text, but you're not sending and receiving
text, you're sending and receiving binary data.

Personally, I'd use the struct module.

[toc] | [prev] | [next] | [standalone]


#72261

From"RasikaSrinivasan@gmail.com" <RasikaSrinivasan@gmail.com>
Date2014-05-29 16:25 -0700
Message-ID<c1f6a64e-26b3-4e5e-9dd6-42aab0091bc1@googlegroups.com>
In reply to#72259
Of course!!!! Cut and paste issue. Anyhow, i will look at the struct module. cheers, srini

On Thursday, May 29, 2014 7:09:21 PM UTC-4, MRAB wrote:
> On 2014-05-29 23:08, RasikaSrinivasan@gmail.com wrote:
> 
> > friends
> 
> >
> 
> > I have a pair of simple python programs as follows:
> 
> >
> 
> > #!/usr/bin/python
> 
> > # broadcast.py
> 
> > import socket
> 
> > from ctypes import *
> 
> > import random
> 
> >
> 
> > class PurgeData(Structure):
> 
> >      _fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
> 
> >
> 
> > myPort = 10756
> 
> >
> 
> > sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> 
> > addr = ('localhost',myPort)
> 
> > #sock.sendto(data,addr)
> 
> >
> 
> > presdata = PurgeData()
> 
> > presdata.press = 0
> 
> > presdata.ticks = 100
> 
> >
> 
> > for msg in range(1,20):
> 
> >      presdata.press = presdata.press+1
> 
> >      presdata.ticks = presdata.ticks+1
> 
> >      presdata.volume = random.random()
> 
> >      sock.sendto(presdata,addr)
> 
> >
> 
> > #--------------------
> 
> >
> 
> > #!/usr/bin/python
> 
> > # Receiver
> 
> > import socket
> 
> >
> 
> > from ctypes import *
> 
> >
> 
> > class PurgeData(Structure):
> 
> >      _fields_ = [("press",c_int), ("ticks",c_int), ("volume",c_float)]
> 
> >
> 
> > myPort = 10756
> 
> >
> 
> > sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> 
> > addr = ('localhost',myPort)
> 
> > sock.bind(addr)
> 
> > presdata=PurgeData()
> 
> >
> 
> > while True:
> 
> >      data,addr = sock.recvfrom(1024)
> 
> >      memmove(addressof(presdata),data.strip(),len(data.strip()))
> 
> >      print presdata.press, presdata.ticks, presdata.volume
> 
> >
> 
> > ---------------------
> 
> >
> 
> > When I tried to run this I get some bizarre results:
> 
> >
> 
> >
> 
> > 1 101 0.343009024858
> 
> > 2 102 0.36397305131
> 
> > 3 103 0.495895296335
> 
> > 4 104 0.372055351734
> 
> > 5 105 0.933839201927
> 
> > 6 106 0.931187808514
> 
> > 7 107 0.876732826233
> 
> > 8 108 0.298638045788
> 
> > 1828716544 -754974720 0.183644190431
> 
> > 1845493760 1660944384 0.186560109258
> 
> > 1862270976 1056964608 0.18631502986
> 
> > 1879048192 1728053248 0.186902835965
> 
> > 1895825408 2097152000 0.18658298254
> 
> > 14 114 0.407857120037
> 
> > 15 115 0.833854913712
> 
> > 16 116 0.00646247947589
> 
> > 17 117 0.297783941031
> 
> > 18 118 0.58082228899
> 
> > 19 119 0.61717569828
> 
> >
> 
> > the received data for the messages 9 thru 13 are not as expected.
> 
> >
> 
> > I wonder if anyone can see what I am doing wrong?
> 
> >
> 
> > Appreciate any hints. thanks, srini
> 
> >
> 
> I don't understand why you're using the .strip method. That's for
> 
> stripping whitespace from text, but you're not sending and receiving
> 
> text, you're sending and receiving binary data.
> 
> 
> 
> Personally, I'd use the struct module.

[toc] | [prev] | [next] | [standalone]


#72262

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-05-30 00:56 +0100
Message-ID<mailman.10464.1401407808.18130.python-list@python.org>
In reply to#72261
On 30/05/2014 00:25, RasikaSrinivasan@gmail.com wrote:
> Of course!!!! Cut and paste issue. Anyhow, i will look at the struct module. cheers, srini
>

Please let us know how you get on, please don't top post, and please 
either use the mailing list 
https://mail.python.org/mailman/listinfo/python-list or read and action 
this https://wiki.python.org/moin/GoogleGroupsPython to prevent us 
seeing double line spacing and single line paragraphs, thanks.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

[toc] | [prev] | [next] | [standalone]


#72260

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-05-29 17:20 -0600
Message-ID<mailman.10463.1401405690.18130.python-list@python.org>
In reply to#72256
On Thu, May 29, 2014 at 5:09 PM, MRAB <python@mrabarnett.plus.com> wrote:
> On 2014-05-29 23:08, RasikaSrinivasan@gmail.com wrote:
>> the received data for the messages 9 thru 13 are not as expected.
>>
>> I wonder if anyone can see what I am doing wrong?
>>
>> Appreciate any hints. thanks, srini
>>
> I don't understand why you're using the .strip method. That's for
> stripping whitespace from text, but you're not sending and receiving
> text, you're sending and receiving binary data.

And indeed, ASCII characters 9-13 are all whitespace.  The receiver
appears to be stripping off the first byte of the (presumably
little-endian) data and thus shifting the whole struct by a byte for
those entries.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web