Objective-C Serial - Mac OS X

一笑奈何 提交于 2019-12-18 15:18:45

问题


I'm currently running the followin in Terminal to send a command over USB serial.

/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?


回答1:


Some google-fu found:

  • Serial Communication Cocoa Framework (on arduino.cc!)
  • AMSerialPort

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




回答2:


ORSSerialPort is a newer, easier to use alternative to AMSerialPort.

Using ORSSerialPort to open a port and send data can be as simple as this:

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



回答3:


If you just want to run that command from your code, you can use the system 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++.




回答4:


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



来源:https://stackoverflow.com/questions/6153818/objective-c-serial-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!