现在我得到了以下课程,但它不起作用。它在open
通话中失败。我认为这是因为沙盒,但我不确定如何前进。这看起来也像 C 一样,有没有更好的 Objective-C (IOKIT?) 方法来解决这个问题?
设备本身是一个 USB 串行端口。
给出的错误:
打开设备失败:不允许操作
当前代码:
#include <termios.h>
#import "Com.h"
#define BAUDCOUNT 17
speed_t baud_const[BAUDCOUNT] = { B50, B75, B110, B134, B150, B200,
B300, B600, B1200, B1800, B2400, B4800,
B9600, B19200, B38400, B57600, B115200 };
unsigned long baud_value[BAUDCOUNT] = { 50, 75, 110, 134, 150, 200,
300, 600, 1200, 1800, 2400, 4800,
9600, 19200, 38400, 57600, 115200 };
@interface Com()
@property (assign) int fd;
@property (assign) struct termios oldio;
@end
@implementation Com
@synthesize fd = _fd;
@synthesize oldio = _oldio;
- (id)init
{
self = [super init];
if (self)
{
self.fd = -1;
return self;
}
return nil;
}
- (BOOL)open:(NSString*)device withBaud:(int)baud
{
struct termios oldio;
struct termios newtio;
// Find the correct baud rate
int baudid = -1;
for(int i = 0; i < BAUDCOUNT; i++) {
if (baud_value[i] == baud) {
baudid = i;
break;
}
}
if(baudid == -1)
{
NSLog(@"Invlaid baud rate: %d", baud);
return NO;
}
// Open the device
self.fd = open([device UTF8String], O_RDWR | O_NOCTTY | O_NDELAY);
if (self.fd < 0)
{
NSLog(@"Failed to open device: %s", strerror(errno));
return NO;
}
// Save old settings
tcgetattr(self.fd, &oldio);
self.oldio = oldio;
// Init memory
memset(&newtio, 0x00 , sizeof(newtio));
cfmakeraw(&newtio);
// settings flags
newtio.c_cflag |= CS8;
newtio.c_cflag |= CLOCAL;
newtio.c_cflag |= CREAD;
newtio.c_iflag = IGNPAR | IGNBRK;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
// Timeout in 100ms
newtio.c_cc[VTIME] = 0;
// read 1 character
newtio.c_cc[VMIN] = 0;
// Setting baudrate
cfsetispeed (&newtio, baud_const[baudid]);
cfsetospeed (&newtio, baud_const[baudid]);
// Flushing buffer
tcflush(self.fd, TCIOFLUSH);
// aplying new configuration
tcsetattr(self.fd, TCSANOW, &newtio);
return YES;
}
@end
当前权利:
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.device.usb</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>