-
12-12-2008, 12:24 PM #1
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?
-
12-12-2008, 12:49 PM #2Livin the iPhone Life
- Join Date
- Jan 2008
- Location
- Iowa (ISU for Vet School!)
- Posts
- 3,848
- Thanks
- 69
- Thanked 309 Times in 263 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
-
12-15-2008, 10:15 AM #3
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:
5) That will establish a 9600 N-8-1 serial connection over USB.Code:# ./(install directory)/serial (ie "./serial/serial")
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:
...and here is the make file: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; }
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.
-
12-17-2008, 06:33 AM #4
Thanks for the info cmk1523. I don't need this now, but who knows.
-
12-17-2008, 03:15 PM #5
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; }
-
12-17-2008, 04:42 PM #6Livin the iPhone Life
- Join Date
- Mar 2008
- Location
- Orange County, CA
- Posts
- 1,463
- Thanks
- 127
- Thanked 89 Times in 64 Posts
nice work cmk, sounds very complicated. lol

-
12-22-2008, 05:05 PM #7
Are you trying to communicate with a micro controller? If so have you had any success?
-
12-23-2008, 03:36 PM #8
Yes I have been successful. The TX/RX lines work just like any other UART so a micro controller would work.
-
12-23-2008, 04:00 PM #9
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-01-2009, 10:39 PM #10
Hello cmk1523.
Thanks a lot! Do you have any update for iPhone/iPod 3.0?
-
01-06-2010, 07:17 AM #11
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.
-
02-26-2010, 10:40 AM #12
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
-
03-08-2010, 03:00 AM #13
Bump? Anything obvious I've missed?
-
05-20-2011, 04:41 AM #14
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!!
-
01-04-2012, 03:40 AM #15
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.



LinkBack URL
About LinkBacks
Reply With Quote
