简体   繁体   English

Objective-C 串行 - Mac OS X

[英]Objective-C Serial - Mac OS X

I'm currently running the followin in Terminal to send a command over USB serial.我目前正在终端中运行以下命令,以通过 USB 串行发送命令。

/Users/drummerboyx/Library/Scripts/arduino-serial -b 9600 -p /dev/tty.usbserial-A800ev0Z -s 1

Is there a way to do this in Objective-C?有没有办法在 Objective-C 中做到这一点?

ORSSerialPort is a newer, easier to use alternative to AMSerialPort. ORSSerialPort是 AMSerialPort 的更新、更易于使用的替代品。

Using ORSSerialPort to open a port and send data can be as simple as this:使用 ORSSerialPort 打开一个端口并发送数据可以很简单:

ORSSerialPort *serialPort = [ORSSerialPort serialPortWithPath:@"/dev/cu.KeySerial1"];
serialPort.baudRate = [NSNumber numberWithInteger:4800];
[serialPort open];
[serialPort sendData:someData]; // someData is an NSData object
[serialPort close];

Some google-fu found:一些google-fu发现:

I know pretty much nothing about it, but the name "IOKit" also sounds pretty promising...我对此几乎一无所知,但“IOKit”这个名字听起来也很有希望......

If you just want to run that command from your code, you can use the system function:如果您只想从代码中运行该命令,可以使用系统function:

#include <stdio.h>
#include <stdlib.h>

system("/Users/drummerboyx/Library/Scripts/arduino-serial -b 9600 -p /dev/tty.usbserial-A800ev0Z -s 1");

You'll need to set your Objective-C source code file extension to.mm, which tells Xcode to compile it as Objective-C++.您需要将 Objective-C 源代码文件扩展名设置为 .mm,这会告诉 Xcode 将其编译为 Objective-C++。

If you want to stick to Cocoa - Have a look at NSTask .如果你想坚持 Cocoa - 看看NSTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM