macos Objective-C 串行 - Mac OS X

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6153818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:05:46  来源:igfitidea点击:

Objective-C Serial - Mac OS X

objective-ccocoamacosarduino

提问by objectiveccoder001

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 中做到这一点?

采纳答案by Dave DeLong

Some google-fu found:

一些 google-fu 发现:

I know pretty much nothing about it, but the name "IOKit"also sounds pretty promising...

我对它几乎一无所知,但是“IOKit”这个名字听起来也很有希望......

回答by Daniel Vogelnest

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

回答by Julio Gorgé

If you just want to run that command from your code, you can use the systemfunction:

如果您只想从代码中运行该命令,则可以使用system函数:

#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++。

回答by Abizern

If you want to stick to Cocoa - Have a look at NSTask.

如果你想坚持使用 Cocoa - 看看NSTask