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

#1 2012-02-01 07:19:13

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

Looking for someone who knows Objective-C

Ok, I'm experimenting with communication with Scratch via Obj-C. Here's what I have.

Code:

#import "ScratchConnectAppDelegate.h"

@implementation ScratchConnectAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSLog(@"Initialized. Whew.");
    NSURL *url = [NSURL URLWithString:@"***.***.***.*"];
    NSHost *host = [NSHost hostWithName:[url host]];
    
    
    NSOutputStream *outputstr = [NSOutputStream alloc];
    [NSStream getStreamsToHost:host port:42001 inputStream:nil outputStream:&outputstr];
    NSLog(@"I have a stream.");
    
    
    [outputstr open];
    NSLog(@"str opened...");
    NSData *myData = [[NSString stringWithString:@"hi"] dataUsingEncoding:NSASCIIStringEncoding];
    NSLog(@"data created");
    NSLog(@"%d", [myData length]);
    const uint8_t *bytes = (const uint8_t*)[myData bytes];
    NSLog(@"%d", [outputstr write:bytes maxLength:[myData length]]);
    [outputstr close];
    [outputstr release];
}

@end

Here's what goes wrong:

Code:

2012-02-01 17:43:39.507 ScratchConnect[1519:a0f] Initialized. Whew.
2012-02-01 17:43:39.509 ScratchConnect[1519:a0f] I have a stream.
2012-02-01 17:43:39.510 ScratchConnect[1519:a0f] str opened...
2012-02-01 17:43:39.510 ScratchConnect[1519:a0f] data created
2012-02-01 17:43:39.511 ScratchConnect[1519:a0f] 2
2012-02-01 17:43:39.512 ScratchConnect[1519:a0f] -1
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
kill
quit

I know EXC_BAD_ACCESS's mean you're referencing a released piece of data, and returning -1 from the outputstr write:maxLength: method means you've made an error. I have BYOB listening with remote sensor connections on and a hi broadcast enabled.

What's wrong?


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

Offline

 

#2 2012-02-01 09:50:56

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

Re: Looking for someone who knows Objective-C

Are you sending 'broadcast "hi"' or are you just sending 'hi'? Did you add the length of the message to the message? I don't know Obj-C, but just a few suggestions.

Last edited by Magnie (2012-02-01 09:51:15)

Offline

 

#3 2012-02-01 11:02:38

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

Re: Looking for someone who knows Objective-C

Magnie wrote:

Are you sending 'broadcast "hi"' or are you just sending 'hi'? Did you add the length of the message to the message? I don't know Obj-C, but just a few suggestions.

Well, I'm pretty sure I may not be trying to send the right thing, but more urgently at the moment, I don't seem to be sending anything...

BTW: What exactly should I be sending again? I'm trying, at the moment, for:
[empty byte][empty byte][empty byte][size][size][size][size][string]

Last edited by Hardmath123 (2012-02-01 11:12:52)


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

Offline

 

#4 2012-02-01 11:23:11

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: Looking for someone who knows Objective-C

Hardmath123 wrote:

Magnie wrote:

Are you sending 'broadcast "hi"' or are you just sending 'hi'? Did you add the length of the message to the message? I don't know Obj-C, but just a few suggestions.

Well, I'm pretty sure I may not be trying to send the right thing, but more urgently at the moment, I don't seem to be sending anything...

BTW: What exactly should I be sending again? I'm trying, at the moment, for:
[empty byte][empty byte][empty byte][size][size][size][size][string]

it's just [size][size][size][size][message]


http://i.imgur.com/1QqnHxQ.png

Offline

 

#5 2012-02-01 13:58:09

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Looking for someone who knows Objective-C

I don't think that [myData bytes] is a pointer.


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#6 2012-02-01 22:49:42

tunasushi
Scratcher
Registered: 2009-10-07
Posts: 15

Re: Looking for someone who knows Objective-C

I know C++ and can work my way around any object-oriented programming language. From your code, it looks like you are using Xcode. Maybe post this in some Xcode or Obj-C forum.

Offline

 

#7 2012-02-02 08:24:41

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

Re: Looking for someone who knows Objective-C

Now working! I get this in my log:

Code:

2012-02-02 18:52:07.679 ScratchConnect[973:a0f] 16 bytes were sent.
2012-02-02 18:52:07.691 ScratchConnect[973:a0f] An event has occured, namely:
2012-02-02 18:52:07.692 ScratchConnect[973:a0f] Number 1 (which is actually):
2012-02-02 18:52:07.692 ScratchConnect[973:a0f] Opened...
2012-02-02 18:52:07.693 ScratchConnect[973:a0f] An event has occured, namely:
2012-02-02 18:52:07.693 ScratchConnect[973:a0f] Number 4 (which is actually):
2012-02-02 18:52:07.694 ScratchConnect[973:a0f] Space left...

And my code:

Code:

#import "ScratchConnectAppDelegate.h"

@implementation ScratchConnectAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSURL *url = [NSURL URLWithString:@"192.168.105.1"];
    NSHost *host = [NSHost hostWithName:[url host]];
    
    outputstr = [[NSOutputStream alloc] initToMemory];
    [outputstr setDelegate:self];
    [outputstr scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputstr open];
    [NSStream getStreamsToHost:host port:42001 inputStream:nil outputStream:&outputstr];
    
    NSData *myData = [[NSString stringWithString:@"broadcast hi"] dataUsingEncoding:NSASCIIStringEncoding];
    NSMutableData *toSend;
    
    Byte *toAppend = (Byte*)malloc(6);
    toAppend[0]=(([myData length] >> 24) & 0xFF);
    toAppend[1]=(([myData length] >> 16) & 0xFF);
    toAppend[2]=(([myData length] >> 8) & 0xFF);
    toAppend[3]=([myData length] & 0xFF);
    
    
    toSend = [NSMutableData dataWithBytes:toAppend length:4];
    [toSend appendData:myData];
    
    const uint8_t *bytes = (const uint8_t*)[toSend bytes];
    
    NSLog(@"%d bytes were sent.", [outputstr write:bytes maxLength:[toSend length]]);
    
}
-(void)dealloc {
    [outputstr close];
    [outputstr release];
    [super dealloc];
}

- (void) stream: (NSStream *) stream handleEvent: (NSStreamEvent) eventCode
{    
    NSLog(@"An event has occured, namely:");
    NSLog(@"Number %d (which is actually):", eventCode);
    if (eventCode == NSStreamEventErrorOccurred) {
        NSLog(@"Error!");
    }
    if (eventCode == NSStreamEventEndEncountered) {
        NSLog(@"End of transfer...");
    }
    if (eventCode == NSStreamEventHasSpaceAvailable) {
        NSLog(@"Space left...");
    }
    if (eventCode == NSStreamEventOpenCompleted) {
        NSLog(@"Opened...");
    }
}
@end

But I still don't get a response from BYOB...


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

Offline

 

Board footer