+ Reply
Results 1 to 15 of 15

Your favorite Apple, iPhone, iPad, iOS, Jailbreak, and Cydia site.


Thread: How do I access the serial / UART / USB connection on the iPhone?

is a discussion within the

iPhone / iPod Touch SDK | Development Discussion

forums, a part of the

3rd Party Apps For iPhone | iPod Touch

section;
I haven't developed a single thing yet for the iPhone but what I need is access to the iPhones's serial/USB connection to read/write data to a device. 1) It this
...
  1. #1
    What's Jailbreak?
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I access the serial / UART / USB connection on the iPhone?
    I haven't developed a single thing yet for the iPhone but what I need is access to the iPhones's serial/USB connection to read/write data to a device.

    1) It this possible yet, using Apple's SDK, to access the USB port and communicate with any device?

    2) If not, can I obtain access if I Jailbreak the iPhone?

    3) Does this UART trick still work?

    4) Where the heck are guides to developing applications for a Jailbroken iPhone? If Jailbroken, can I still use Apple's SDK at all?

  2. #2
    Livin the iPhone Life sziklassy's Avatar
    Join Date
    Jan 2008
    Location
    Iowa (ISU for Vet School!)
    Posts
    3,920
    Thanks
    69
    Thanked 310 Times in 264 Posts

    I know that Apple does not allow this under their SDK. Not sure about jailbreak, etc.
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------
    I once prayed to God for an iPhone, but quickly found out He didn't work that way...so I stole an iPhone and prayed for His forgiveness.

    A dog is the only thing on earth that loves you more than you love yourself. - Josh Billings

  3. #3
    What's Jailbreak?
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here it is everyone, thanks to Collin's tutorial at: DevDot Iphone Serial Port Tutorial, I modified it so it actually runs on a 2.2 jailbroken iPhone/iPod Touch. Here are the directions to get 3.3v UART access.

    1) Jailbreak your 2.2 iPhone/iPod Touch.
    2) Using Cydia, install "OpenSSH".
    3) Using Cydia, install the "iPhone 2.0 Toolchain"
    4) Ensure SSH is running
    5) Using WinSCP, extract the serial folder found here to anywhere on your phone (ie "/" root).
    4) Using Putty or whatever console app you chose, run:
    Code:
    # ./(install directory)/serial (ie "./serial/serial")
    5) That will establish a 9600 N-8-1 serial connection over USB.

    UPDATE: You do not need to create the "*" handshake if your using firmware after 1.1, nor do you need to do the strobe hack mentioned in Collin's tutorial.

    If you make changes to it, compiling is as simple as:
    Code:
    # cd /(install directory)/ (ie "cd /serial/")
    # make


    If my link goes down, here is the serial.c code with minimal comments:

    Code:
    //////////////////////////////////////////////////////////////////////////
    //  Iphone Serial I/O demo.  see DevDot.wikispaces.com for more info
    //  ---------------------------------------------------------------------------------------
    //  By Collin Meyer (TheRain)
    //  12-09-2007 revision 1
    //  Feel free to reuse this code.
    //
    // Modified by Chris K.
    //////////////////////////////////////////////////////////////////////////
    
    #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;
    
        fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NONBLOCK);
        if (fileDescriptor != -1) {
    		if (ioctl(fileDescriptor, TIOCEXCL) != -1) {
    			if (fcntl(fileDescriptor, F_SETFL, 0) != -1) {
    				if (tcgetattr(fileDescriptor, &gOriginalTTYAttrs) != -1) {
    					options = gOriginalTTYAttrs;
    
    					printf("Current input baud rate is %dn", (int) cfgetispeed(&options));
    					printf("Current output baud rate is %dn", (int) cfgetospeed(&options));
    
    					return fileDescriptor;
    				} else {
    					printf("Error getting tty attributes %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    					if (fileDescriptor != -1) {
    						close(fileDescriptor);
    					}
    					return -1;
    				}
    			} else {
    				printf("Error clearing O_NONBLOCK %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    				if (fileDescriptor != -1) {
    					close(fileDescriptor);
    				}
    				return -1;
    			}
    		} else {
    			printf("Error setting TIOCEXCL on %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    			if (fileDescriptor != -1) {
    				close(fileDescriptor);
    			}
    			return -1;
    		}
    	} else {
            printf("Error opening serial port %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    		if (fileDescriptor != -1) {
    			close(fileDescriptor);
    		}
    		return -1;
        }
    
        return fileDescriptor;
    } 
    
    int main(int args, char *argv[]) {
    	int fd;
    	char somechar[8];
    	fd = OpenSerialPort(); 
    	if(fd >- 1) {
    		write(fd,"*",1); 
    		
    		///////////////////////////////////////////////////////////////////////////////////////////////////
    		// 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); 
    		if(somechar[0]=='*') { 
    			printf("Serial connection established!  n");
    			while(1) {
    				read(fd,&somechar[0],1);
    				putchar(somechar[0]);
    			}
    		}
    	}
    	return 0;
    }
    ...and here is the make file:

    Code:
    CC=gcc
    TARGET=serial
    LD=$(CC)
    LDFLAGS = -framework CoreFoundation 
              -framework Foundation 
              -framework UIKit 
              -framework CoreGraphics 
              -lobjc
    
    all:	$(TARGET)
    
    $(TARGET):	$(TARGET).o
    	@$(LD) $(LDFLAGS) -o $@ $^
    		@ldid -S $(TARGET)
    
    clean:
    		@rm -f *.o $(TARGET)
    Last edited by cmk1523; 12-17-2008 at 03:17 PM.

  4. #4
    Mes
    Mes is offline
    Livin the iPhone Life
    Join Date
    May 2008
    Posts
    8,025
    Thanks
    102
    Thanked 788 Times in 731 Posts

    Thanks for the info cmk1523. I don't need this now, but who knows.

  5. #5
    What's Jailbreak?
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here's some even more barebones code to set up a 9600 N-8-1 UART connection...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    
    static int OpenSerialPort(void);
    
    static int OpenSerialPort() {
        int fileDescriptor = -1;
        struct termios  options;
        printf("Initializing Serial Port... ");
        fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NONBLOCK);
        if (fileDescriptor != -1) {
           if (fcntl(fileDescriptor, F_SETFL, 0) != -1) {
    	   if (tcgetattr(fileDescriptor, &gOriginalTTYAttrs) != -1) {
    	      options = gOriginalTTYAttrs;
    	      printf("[DONE]rn");	
    	      return fileDescriptor;
    	   } else {
    		printf("[FAIL]rn");
    		printf("Error getting tty attributes %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    		if (fileDescriptor != -1) {
    		   close(fileDescriptor);
    		}
    		return -1;
    	   }
    	} else {
    	   printf("[FAIL]rn");
    	   printf("Error clearing O_NONBLOCK %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    	   if (fileDescriptor != -1) {
    	      close(fileDescriptor);
    	   }
    	   return -1;
    	}
         } else {
    	printf("[FAIL]rn");
    	printf("Error setting TIOCEXCL on %s - %s(%d).n", "/dev/tty.iap", strerror(errno), errno);
    	if (fileDescriptor != -1) {
    	   close(fileDescriptor);
    	}
    	return -1;
       }
    } 
    
    int main (int argc, char * const argv[]) {
      int fd;	
      char c[1];
    
      if((fd = OpenSerialPort()) >= 1) {
         while(1) {
    	if (read(fd, (void *)c, 1) == -1) {
    	   printf("Read Error.rn");
    	}
    			
    	if (write(fd, (void *)c, 1) == -1) {
               printf("Write Error.rn");
    	}
         }
       }
    
       return 0;
    }

  6. #6
    Livin the iPhone Life Chase817's Avatar
    Join Date
    Mar 2008
    Location
    Orange County, CA
    Posts
    1,463
    Thanks
    127
    Thanked 91 Times in 65 Posts

    nice work cmk, sounds very complicated. lol
    http://modmyi.com/forums/image.php?type=sigpic&userid=303541&dateline=12514  03473

  7. #7
    What's Jailbreak?
    Join Date
    Dec 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Are you trying to communicate with a micro controller? If so have you had any success?

  8. #8
    What's Jailbreak?
    Join Date
    Feb 2008
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Yes I have been successful. The TX/RX lines work just like any other UART so a micro controller would work.

  9. #9
    What's Jailbreak?
    Join Date
    Dec 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Awesome. What sort of project are you working on? I've started building a relay and sensor control board using a PIC because its the only one I know how to use. The only computer side programs I've used to communicate with a micro controller over serial are MATLAB and Hyper Terminal. I'm aware this is a newb question but what are you using to build the iPhone software interface side?

  10. #10
    What's Jailbreak?
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hello cmk1523.

    Thanks a lot! Do you have any update for iPhone/iPod 3.0?

  11. #11
    What's Jailbreak?
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default iphone serial port connection
    cmk1523, can you help. I have no experience with codeing & am trying to use my iphone to connect to the serial port on our CCTV camera engineers port.
    I have aquired a cable from Serial Data Cable for Apple iPhone i-Phone but am not having any joy, I have tried using minicom to set the iphones baud rate etc to 9600 8N1 flow control needs to be xon/xoff
    It works when connected to my laptop as in I can use my laptop keyboard to type in mobileterminal but am not getting two way comms.
    Can you give me some instructions on how to use your code? or what I need to do with it.
    Many thanks.

  12. #12
    What's Jailbreak?
    Join Date
    Jul 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hi guys. I have the same problem. I can receive characters over a serial stream but I cannot actually send from the iPhone back to the connected device. It appears to only go one way. Im using an iPhone3G. Do I need to do the strobing trick? I'll certainly try that now I know about it but I'd certainly rather not do that if i can help it

  13. #13
    What's Jailbreak?
    Join Date
    Jul 2009
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Bump? Anything obvious I've missed?

  14. #14
    What's Jailbreak?
    Join Date
    Aug 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hi everyone!

    Someone is doing this, this days with 4.3? I'm trying but I can't compile the serial.c or run the precompiled one... (of that tutorial... )

    Help! Thanks!!

  15. #15
    What's Jailbreak?
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sorry for diggin' this out, but I'm gonna try this these days.

    I'll use DevDot's code if no one convinces me that cmk1523's stuff is better Also his serial.zip download is broken.
    The iPod Touch 4G this is for will be used to display some "higher level" information he gets through usb for a "user interface" on a coilgun.

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts