iPhone serial port communication

http://devdot.wikispaces.com/Iphone+Serial+Port+Tutorial

 

Requirement:
1.Jailbroken Iphone with BSD Subsystem installed (Search the web for jailbreak tutorials)
2.Iphone development toolchain for Mac OSX, Linux, Windows (Cygwin) or other OS. I use this http://code.google.com/p/winchain/ in Windows.
3.Sparkfun Ipod connector or breakout board see http://www.sparkfun.com/commerce/product_info.php?products_id=633 or http://www.sparkfun.com/commerce/product_info.php?products_id=8295
4.PC USB UART – optional, but used in this tutorial. You will need either a 3.3V level UART for PC (see http://www.sparkfun.com/commerce/product_info.php?products_id=718) or you will need an old style 12V level serial cable with a level converter like this one http://www.compsys1.com/workbench/On_top_of_the_Bench/Max233_Adapter/max233_adapter.html (see warning below)
5.A soldering iron will help you out :) 
6.Microsoft Visual Studio – optional, but used in the source code examples for the PC serial port communication.

WARNING! Do not try to use a 12V level RS232 port for this without a level converter to ~3V, it will severely damage your Iphone or render it non-functional.

Hardware:
The Dock Port
In the Ipod/Iphone dock port, the pins we are concerned with are as follows
Pin 1
Ground
Pin 18
3.3V Power (+)
Pin 12
TX also known as Serial Transmit
Pin 13
RX also known as Serial Receive

To see a full description of all of the pins in the dock connector, see here: http://pinouts.ru/Devices/ipod_pinout.shtml

Connections: iPhone/iPod Touch RX should connect to TX of the connected device, TX to RX of the connected device, and Ground to the Ground of the connected device. If your device can be powered by a low amperage, 3.3V power source you may chose to connect PIN 18 as well and power your device directly from the iPhone/iPod Touch.

Sample Codes:

 

001#include <stdio.h>   /* Standard input/output definitions */
002#include <string.h>  /* String function definitions */
003#include <unistd.h>  /* UNIX standard function definitions */
004#include <fcntl.h>   /* File control definitions */
005#include <errno.h>   /* Error number definitions */
006#include <termios.h> /* POSIX terminal control definitions */
007 
008static struct termios gOriginalTTYAttrs;
009 
010static int OpenSerialPort()
011{
012     int        fileDescriptor = -1;
013     int        handshake;
014     struct termios  options;
015 
016     // Open the serial port read/write, with no controlling terminal, and don't wait for a connection.
017     // The O_NONBLOCK flag also causes subsequent I/O on the device to be non-blocking.
018     // See open(2) ("man 2 open") for details.
019 
020     fileDescriptor = open("/dev/tty.iap", O_RDWR | O_NOCTTY | O_NONBLOCK);
021     if (fileDescriptor == -1)
022     {
023         printf("Error opening serial port %s - %s(%d)./n",
024                "/dev/tty.iap", strerror(errno), errno);
025         goto error;
026     }
027 
028     // Note that open() follows POSIX semantics: multiple open() calls to the same file will succeed
029     // unless the TIOCEXCL ioctl is issued. This will prevent additional opens except by root-owned
030     // processes.
031     // See tty(4) ("man 4 tty") and ioctl(2) ("man 2 ioctl") for details.
032 
033     if (ioctl(fileDescriptor, TIOCEXCL) == -1)
034     {
035         printf("Error setting TIOCEXCL on %s - %s(%d)./n",
036             "/dev/tty.iap", strerror(errno), errno);
037         goto error;
038     }
039 
040     // Now that the device is open, clear the O_NONBLOCK flag so subsequent I/O will block.
041     // See fcntl(2) ("man 2 fcntl") for details.
042 
043     if (fcntl(fileDescriptor, F_SETFL, 0) == -1)
044     {
045         printf("Error clearing O_NONBLOCK %s - %s(%d)./n",
046             "/dev/tty.iap", strerror(errno), errno);
047         goto error;
048     }
049 
050     // Get the current options and save them so we can restore the default settings later.
051     if (tcgetattr(fileDescriptor, &gOriginalTTYAttrs) == -1)
052     {
053         printf("Error getting tty attributes %s - %s(%d)./n",
054             "/dev/tty.iap", strerror(errno), errno);
055         goto error;
056     }
057 
058     // The serial port attributes such as timeouts and baud rate are set by modifying the termios
059     // structure and then calling tcsetattr() to cause the changes to take effect. Note that the
060     // changes will not become effective without the tcsetattr() call.
061     // See tcsetattr(4) ("man 4 tcsetattr") for details.
062 
063     options = gOriginalTTYAttrs;
064 
065     // Print the current input and output baud rates.
066     // See tcsetattr(4) ("man 4 tcsetattr") for details.
067 
068     printf("Current input baud rate is %d/n", (int) cfgetispeed(&options));
069     printf("Current output baud rate is %d/n", (int) cfgetospeed(&options));
070 
071     // Set raw input (non-canonical) mode, with reads blocking until either a single character
072     // has been received or a one second timeout expires.
073     // See tcsetattr(4) ("man 4 tcsetattr") and termios(4) ("man 4 termios") for details.
074 
075     cfmakeraw(&options);
076     options.c_cc[VMIN] = 1;
077     options.c_cc[VTIME] = 10;
078 
079     // The baud rate, word length, and handshake options can be set as follows:
080 
081     cfsetspeed(&options, B19200);    // Set 19200 baud
082     options.c_cflag |= (CS8);  // RTS flow control of input
083 
084     printf("Input baud rate changed to %d/n", (int) cfgetispeed(&options));
085     printf("Output baud rate changed to %d/n", (int) cfgetospeed(&options));
086 
087     // Cause the new options to take effect immediately.
088     if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1)
089     {
090         printf("Error setting tty attributes %s - %s(%d)./n",
091             "/dev/tty.iap", strerror(errno), errno);
092         goto error;
093     }
094     // Success
095     return fileDescriptor;
096 
097     // Failure "/dev/tty.iap"
098error:
099     if (fileDescriptor != -1)
100     {
101         close(fileDescriptor);
102     }
103 
104     return -1;
105}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值