This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-23 08:07:47

ARLUMI
New Scratcher
Registered: 2012-03-14
Posts: 9

Python receiver data from scratch

HI:

I am using Python to get data from Scratch

if scratch send a broadcast, after my python program receive data will do someting

My Scratch will send a broadcast: YesLesson

A part of my python source code below:

while True:
        time.sleep(0.01)
        data = scratchSock.recv(1024)
       
        if data=="broadcast "+'"YesLesson"':
                print(data)
-----------------------------------------------------------
this is not working
I have stucked here for a few days...

can somebody tell me how to solve this problem?
Thanks

Offline

 

#2 2012-05-23 08:10:40

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Python receiver data from scratch

Try this tutorial: http://wiki.scratch.mit.edu/wiki/Commun … with_a_GUI Hope it helps!  wink


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#3 2012-05-23 10:05:17

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Python receiver data from scratch

That is because you are only checking for "broadcast YesLesson", you need to look for the length at the beginning of the message, you also need to put double-quotes around your message:

Code:

\x0\x0\x0\x15broadcast "YesLesson"

Try this code:

Code:

while True:
    time.sleep(0.01)
    data = scratchSock.recv(1024)
    
    if data == '\x00\x00\x00\x15broadcast "YesLesson"':
        print(data)

Have you set up your scratchSock connection as well? (Was the code you provided the full code, or not?)

Last edited by Magnie (2012-05-23 10:11:41)

Offline

 

#4 2012-05-23 10:41:59

rookwood101
Scratcher
Registered: 2011-07-29
Posts: 500+

Re: Python receiver data from scratch

You could use this library from a scratcher


http://i.imgur.com/zeIZW.png

Offline

 

#5 2012-05-23 11:17:43

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Python receiver data from scratch

rookwood101 wrote:

You could use this library from a scratcher

I agree — definitely try Scratra  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#6 2012-05-23 11:33:33

ARLUMI
New Scratcher
Registered: 2012-03-14
Posts: 9

Re: Python receiver data from scratch

Thanks guys!
Glad to see all these...
and it's really work

smile

Offline

 

#7 2012-05-23 11:36:37

ARLUMI
New Scratcher
Registered: 2012-03-14
Posts: 9

Re: Python receiver data from scratch

Magnie wrote:

That is because you are only checking for "broadcast YesLesson", you need to look for the length at the beginning of the message, you also need to put double-quotes around your message:

Code:

\x0\x0\x0\x15broadcast "YesLesson"

Try this code:

Code:

while True:
    time.sleep(0.01)
    data = scratchSock.recv(1024)
    
    if data == '\x00\x00\x00\x15broadcast "YesLesson"':
        print(data)

Have you set up your scratchSock connection as well? (Was the code you provided the full code, or not?)

can you tell me what is this means: x00\x00\x00\x15
I am not pretty sure about that.

this is just a part of my source code,I want to make a python website to connect scratch

Offline

 

#8 2012-05-23 12:03:23

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Python receiver data from scratch

ARLUMI wrote:

can you tell me what is this means: x00\x00\x00\x15
I am not pretty sure about that.

this is just a part of my source code,I want to make a python website to connect scratch

The first 4 bytes, "\x00\x00\x00\x15", tell you the length of the message. "\x15" is hexadecimal for 21. You could parse them, but it's probably easier to ignore them:
    data = data[4:]


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

Board footer