恺之 发表于 2013-2-15 15:47 
ios端的软件是怎么写的呢?
xcode4.6 + iphone6.1开发
代码:
1. 串口处理文件: serial.c
//////////////////////////////////////////////////////////////////////////
// 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.
//////////////////////////////////////////////////////////////////////////
#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(unsigned long speed)
{
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;
}
//使用非阻塞方式
/* if( fcntl(fileDescriptor, F_SETFL, FNDELAY) == -1)
{
printf("Error clearing O_NONBLOCK %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, speed); // 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;
//}
2.在xcode中包装一下上面的serial.c文件,取名SerialIO ,并使用serial.c中的OpenSerialPort、write、read,在控制LED这个实验中不需要使用read函数,因为iphone只发出控制指令,不需要read。
#import "SerialIO.h"
#import "serial.c"
@implementation SerialIO
+(int)open:(unsigned long)speed{
if(serialPort == -1){
serialPort = OpenSerialPort(speed);
return serialPort;
}
else
return serialPort;
}
+(long)write:(NSString *)msg{
char *s = [msg cStringUsingEncoding:NSASCIIStringEncoding];
if(serialPort == -1){
return 0;
}
int length = [msg length];
return write(serialPort, s, length);
}
+(long)read:(char [])rec length:(int)len{
// int bytes_left;
int bytes_read;
//char *ptr;
// bytes_left = len;
//while (bytes_left > 0) {
bytes_read = read(serialPort, rec, len);
// if(bytes_read < 0 ){
//
// }else if(bytes_read==0){
// break;
// }
// bytes_left-=bytes_read;
// ptr+=bytes_read;
// }
// rec = ptr;
// return (len - bytes_left);
return bytes_read;
}
@end
3.最终调用(滑动事件): blueChange、redChange、greenChange
//
// ViewController.m
// SerialControl
//
// Created by Panda on 13-2-8.
// Copyright (c) 2013年 Panda. All rights reserved.
//
#import "ViewController.h"
#import "SerialIO.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)blueChange:(id)sender {
int a;
a = (int)[_blueSilder value];
a = 255-a;
NSString *s=[[NSString alloc] initWithFormat:@"%d",a];
[SerialIO open:9600];
NSString *sendStr = [[@"db" stringByAppendingString:s] stringByAppendingString:@"#"];
long count = [SerialIO write:sendStr];
}
- (IBAction)redChange:(id)sender {
int a;
a = (int)[_redSlider value];
a = 255-a;
NSString *s=[[NSString alloc] initWithFormat:@"%d",a];
[SerialIO open:9600];
NSString *sendStr = [[@"dr" stringByAppendingString:s] stringByAppendingString:@"#"];
long count = [SerialIO write:sendStr];
}
- (IBAction)greenChange:(id)sender {
int a;
a = (int)[_greenSlider value];
a = 255-a;
NSString *s=[[NSString alloc] initWithFormat:@"%d",a];
[SerialIO open:9600];
NSString *sendStr = [[@"dg" stringByAppendingString:s] stringByAppendingString:@"#"];
long count = [SerialIO write:sendStr];
}
@end
|