我正在尝试编写一个命令行,Foundation Objective-C程序,我需要与RS232串行设备进行通信。
关于如何做到这一点的任何想法?
编辑 - 这是iPhone的 NOT !这是桌面应用程序。
答案 0 :(得分:4)
我假设您使用的是RS-232 / USB适配器。如果我这样做,我会弄清楚它出现在哪个设备上,然后使用open(“/ dev / ttyS0”等)函数打开它。然后,您可以将文件描述符和访问函数包装在对象中。
以下是在POSIX环境中播放串口的页面: http://www.easysw.com/~mike/serial/serial.html
答案 1 :(得分:0)
我使用了来自moxa的所谓“串行设备服务器”,它就像一个魅力。
优于USB串行转换器:
设备服务器通常允许您使用LAN或WiFi等网络连接到串行设备。
软件方面,网络/套接字编程非常简单,如:
(使用GCDAsyncSocket)
准备数据
static u_int8_t cmd1[] = { 0x1a, 0x73, 0x10 }; //defined by the serial device's protocol
static u_int8_t cmd2[] = { 0x1b, 0x51, 0x01 };
self.data = [NSMutableData dataWithBytes:&cmd1 length:sizeof(cmd1)];
[self.data appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[self.data appendBytes:&cmd2 length:sizeof(cmd2)];
发送数据
-(IBAction)buttonAction:(id)sender
{
NSError *error = nil;
[self.socket connectToHost:@"192.168.1.9" onPort:1234 withTimeout:-1 error:&error];
if (error) {
NSLog(@"error: %@", error);
}
}
-(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
[self.socket writeData:self.data withTimeout:-1 tag:23];
}