Connect the Accessory to my JailBreak iPhone Device
Asked Answered
T

2

7

I am using serial programming for get connected serial cable to my iPhone

my code is as under

#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

static struct termios gOriginalTTYAttrs;

static int OpenSerialPort()
{
    int        fileDescriptor = -1;
    int        handshake;
    struct termios  options;

    // Open the serial port read/write, with no controlling terminal, and don't wait for a connection.
    // The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking.
    // See open(2) ("man 2 open") for details.

    fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (fileDescriptor == -1)
    {
        printf("Error opening serial port %s - %s(%d).\n",
               "/dev/tty.iap", strerror(errno), errno);
        goto error;
    }

    // Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
    // unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
    // processes.
    // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.

    if (ioctl(fileDescriptor, TIOCEXCL) == -1)
    {
        printf("Error setting TIOCEXCL on %s - %s(%d).\n",
            "/dev/tty.iap", strerror(errno), errno);
        goto error;
    }

    // Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
    // See fcntl(2) ("man 2 fcntl") for details.

    if (fcntl(fileDescriptor, F_SETFL, 0) == -1)
    {
        printf("Error clearing O_NONBLOCK %s - %s(%d).\n",
            "/dev/tty.iap", strerror(errno), errno);
        goto error;
    }

    // Get the current options and save them so we can restore the default settings later.
    if (tcgetattr(fileDescriptor, &gOriginalTTYAttrs) == -1)
    {
        printf("Error getting tty attributes %s - %s(%d).\n",
            "/dev/tty.iap", strerror(errno), errno);
        goto error;
    }

    // The serial port attributes such as timeouts and baud rate are set by modifying the termios
    // structure and then calling tcsetattr() to cause the changes to take effect. Note that the
    // changes will not become effective without the tcsetattr() call.
    // See tcsetattr(4) ("man 4 tcsetattr") for details.

    options = gOriginalTTYAttrs;

    // Print the current input and output baud rates.
    // See tcsetattr(4) ("man 4 tcsetattr") for details.

    printf("Current input baud rate is %d\n", (int) cfgetispeed(&options));
    printf("Current output baud rate is %d\n", (int) cfgetospeed(&options));

    // Set raw input (non-canonical) mode, with reads blocking until either a single character 
    // has been received or a one second timeout expires.
    // See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.

    cfmakeraw(&options);
    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 10;

    // The baud rate, word length, and handshake options can be set as follows:

    cfsetspeed(&options, B19200);    // Set 19200 baud    
    options.c_cflag |= (CS8);  // RTS flow control of input


    printf("Input baud rate changed to %d\n", (int) cfgetispeed(&options));
    printf("Output baud rate changed to %d\n", (int) cfgetospeed(&options));

    // Cause the new options to take effect immediately.
    if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1)
    {
        printf("Error setting tty attributes %s - %s(%d).\n",
            "/dev/tty.iap", strerror(errno), errno);
        goto error;
    }    
    // Success
    return fileDescriptor;

    // Failure "/dev/tty.iap"
error:
    if (fileDescriptor != -1)
    {
        close(fileDescriptor);
    }

    return -1;
}

int main(int args, char *argv[])
{
    int fd;
    char somechar[8];
    fd=OpenSerialPort(); // Open tty.iap with no hardware control, 8 bit, BLOCKING and at 19200 baud
    if(fd>-1)
    {
        write(fd,"*",1); // Write handshaking message over serial
        ///////////////////////////////////////////////////////////////////////////////////////////////////
        // After this, our device or our PC program should be strobing serial ground to gain access to the Iphone Serial Line
        //////////////////////////////////////////////////////////////////////////////////////////////////
        read(fd,&somechar[0],1); // Read 1 byte  over serial.  This will block (wait) untill the byte has been received
        if(somechar[0]=='*') // Check if this byte is a "handshaking" message
        {
            printf("Serial connection established!\n"); // If it is, we have established a connection to the device and can freely read/write over serial!
            while(1) // Do this forever or untill someone presses CTRL+C
            {
                read(fd,&somechar[0],1);  // Read a character over serial!
                putchar(somechar[0]); // Write the character to the Terminal!!
            }
        }
    }
    return 0;
}

but when i am going to check the cable is connected or not i got one Error like this

Error opening serial port /dev/tty.iap - Operation not permitted(1).

is there any one know the solution and please give me suggestion if i am going in wrong way actually i am a new in iOS development so more confused.

Thanks

Thunderclap answered 30/9, 2011 at 8:46 Comment(2)
I'm not at all familiar with that level of coding on a jb iPhone. However some unix type checks may enlighten others. What permissions are on /dev/tty.iap and does the user you're running the code as have permissions to the device (though I'm guessing you're running the code ssh'd in as root?).Choreography
@Thunderclap how did you view the output of your printf statements? Obviously the console window is Xcode won't work because your iDevice is connected to your serial device using a custom dock connector I presume?Emperor
R
2

Perhaps the following articles iPhone Serial Communication may help?

iPhone/iPod Touch Serial Port Tutorial By Collin Meyer

and

iPhone Serial Communication

Remitter answered 9/11, 2011 at 4:7 Comment(3)
I am success for communicate jb iPhone to accessory via serial communication but i face one problem for iPhone 4g. i got one error "Serial connection failed" every time whenever i try to connect that accessory to iPhoen4g. its work fine with 3g or 3gsThunderclap
@Thunderclap Okay, well you should update your original question to reflect that then.Remitter
@Fulvio, second link had the answer, at least in my case. +1 for that (was a while ago).Mcnary
M
0

I got the same error when trying to open the serial port on a jailbroken iPod Touch running iOS 5.0 using a binary created with Xcode. However when I used gcc (for the iPhone with appropriate flags set) on the command line on my Mac instead of Xcode and created a command line app for iOS, the binary that was generated was able to open the serial port and read commands from my external accessory. This should be possible to create a library and link it with Xcode and have the same result (though I haven't tried it yet).

Update: Unfortunately creating a library in gcc and linking it in an Xcode project came up with the same error as before. Possible solutions:

  • call the command line app from the iOS Xcode GUI app
  • compile the app itself with gcc instead of Xcode

Actually, it's even simpler than this, found it while going through the second link in Fulvio's answer: You just need to move the app to a different directory than where Xcode normally puts them. As suggested in the blog entry, I put my app in a directory for Cydia apps (/private/var/stash/Applications), and was able to open the serial port--no gcc compiling necessary.

So my gcc-compiled command-line app was able to read from the serial port because I was executing it in the root's home directory, which also doesn't have the restrictions of the normal apps directory (/private/var/mobile/Applications)--whatever those restrictions might be.

Mcnary answered 5/5, 2012 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.