Socket programming (robot arm)

Discussion in 'iOS Programming' started by czatryk, Oct 3, 2010.

  1. czatryk macrumors newbie

    Joined:
    Oct 1, 2010
    #1
    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.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #3
    It would be slightly more efficient to use sprintf, since using NSString means building a unicode string and converting it to a C string.
     
  4. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #4
    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.
     
  5. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #5
    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.
     

Share This Page