Ok, I'm experimenting with communication with Scratch via Obj-C. Here's what I have.
#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];
}
@endHere's what goes wrong:
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?
Offline
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
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)
Offline
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]
Offline
I don't think that [myData bytes] is a pointer.
Offline
Now working! I get this in my log:
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:
#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...");
}
}
@endBut I still don't get a response from BYOB...
Offline