Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Serial - Mac OS X

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?

like image 622
objectiveccoder001 Avatar asked May 27 '11 14:05

objectiveccoder001


4 Answers

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];
like image 199
Daniel Vogelnest Avatar answered Nov 16 '22 16:11

Daniel Vogelnest


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...

like image 20
Dave DeLong Avatar answered Nov 16 '22 16:11

Dave DeLong


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++.

like image 1
Julio Gorgé Avatar answered Nov 16 '22 16:11

Julio Gorgé


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

like image 1
Abizern Avatar answered Nov 16 '22 15:11

Abizern