Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

czatryk

macrumors newbie
Original poster
Oct 1, 2010
1
0
Hi everybody,
I'm writing an application that controls robot arm and I need Your assistance.

Long story short robot arm is controller with telnet session. I was able to successfully connect to it using

Code:
#import <CoreFoundation/CFSocket.h>

and send defined commands like this:

Code:
UInt8 message[] = "do home\r\n";    //returns arm to home possition
CFDataRef data = CFDataCreate(NULL, message, sizeof(message));
CFSocketSendData(s, NULL, data, 0);
CFRelease(data);

but as the application develops I'm trying to put variables into sanded commands, i.e.:

Code:
do drive 1,20
do drive 2,45
do drive 4,-10


I approached this challenge by defining my command as NSString:

Code:
int joint=1, value=45;
command = [NSString stringWithFormat: @"do drive %d,%d\r\n",joint,value];

and I'm trying to covert my command (NSString) to message format (UInt8 array) so it can be sanded the like before.

After couple of hours googleing and searching through forum I came to realize that I can’t do it alone and I need help.

Any advice is greatly appreciated.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
If you code using UInt8 is valid (a string in " characters is an array of char technically, not UInt8, although these types may well be interchangeable on the iPhone). Given than any of the NSString methods that return a char array will be fine). Say cStringUsingEncoding: or UTF8String.
 

ianray

macrumors 6502
Jun 22, 2010
452
0
@
It would be slightly more efficient to use sprintf, since using NSString means building a unicode string and converting it to a C string.
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
This is somewhat orthogonal to your original question, but you may find it easier to use the AsyncSocket library which is a nice Objective-C wrapper around the CFSocket functionality and will let you work with native Objective-C objects like NSString more easily.
 

Krevnik

macrumors 601
Sep 8, 2003
4,100
1,309
It would be slightly more efficient to use sprintf, since using NSString means building a unicode string and converting it to a C string.

More efficient, sure. But I can see using NSString's UTF8String or cStringWithEncoding as a way to deal with this particular problem while at the same time learning better habits for code development going forward (where you will be dealing more with localizable strings, and multiple encodings, and less with ASCII strings).

The processing power certainly allows for it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.