Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何与Objective-C中的RS232设备通信? 我试图写一个命令行,基础Objy-C程序,我需要与RS232串行设备通信。_Objective C_Serial Port_Foundation_Serial Communication - Fatal编程技术网

如何与Objective-C中的RS232设备通信? 我试图写一个命令行,基础Objy-C程序,我需要与RS232串行设备通信。

如何与Objective-C中的RS232设备通信? 我试图写一个命令行,基础Objy-C程序,我需要与RS232串行设备通信。,objective-c,serial-port,foundation,serial-communication,Objective C,Serial Port,Foundation,Serial Communication,有什么办法吗 编辑-这不适用于iPhone!这是一个桌面应用程序。我假设您使用的是RS-232/USB适配器。如果我这样做,我会找出它出现在哪个设备上,然后使用open(“/dev/ttyS0”等)函数打开它。然后可以将文件描述符和访问函数包装到对象中 以下是关于在POSIX环境中使用串行端口的页面: 我使用了moxa公司的所谓“串行设备服务器”,它工作起来很有魅力 与USB串行转换器相比的优势: 该设备可以集成到网络中的任何位置 在应用程序方面,它只是一个记录良好的网络编程 设备服务器通常

有什么办法吗


编辑-这不适用于iPhone!这是一个桌面应用程序。

我假设您使用的是RS-232/USB适配器。如果我这样做,我会找出它出现在哪个设备上,然后使用open(“/dev/ttyS0”等)函数打开它。然后可以将文件描述符和访问函数包装到对象中

以下是关于在POSIX环境中使用串行端口的页面:

我使用了moxa公司的所谓“串行设备服务器”,它工作起来很有魅力

与USB串行转换器相比的优势:

  • 该设备可以集成到网络中的任何位置
  • 在应用程序方面,它只是一个记录良好的网络编程
设备服务器通常允许您使用或之类的网络连接到串行设备

软件方面,网络/套接字编程非常简单,如:

(使用)

准备数据

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];
}

在哪里可以找到此功能的手册?对不起,我刚刚开始使用这种语言。另外,我应该如何配置波特率、tx/rx阻塞等?我链接到的页面应该会提供这些信息和一些示例代码。对于open()系统调用,启动终端,只需键入“man 2 open”。