Created
December 7, 2018 17:16
-
-
Save YutoMizutani/14b3d2d2a05be3f9bff043cdff69cf3b to your computer and use it in GitHub Desktop.
Swiftで書くシリアル通信 〜macOS - Arduino を例に〜 ref: https://qiita.com/YutoMizutani/items/010697ab2321294482f3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ORSSerial | |
class Delegate: NSObject, ORSSerialPortDelegate { | |
/// 改行が入力されるまでの文字列を格納するテキストバッファ | |
private var textBuffer = "" | |
/// 入力 | |
private func input(_ serialPort: ORSSerialPort) { | |
print("<< ", terminator: "") | |
let input = readLine() ?? "" | |
guard | |
input != "q", | |
let data = input.data(using: String.Encoding.utf8) | |
else { | |
serialPort.close() | |
exit(0) | |
} | |
serialPort.send(data) | |
} | |
/// 出力 | |
private func output() { | |
print(">> \(textBuffer)") | |
textBuffer = "" | |
} | |
/// ポートからの入力を検知した際に呼ばれる | |
func serialPort(_ serialPort: ORSSerialPort, didReceive data: Data) { | |
guard let string: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as String? else { return } | |
guard string.contains("\r") || string.contains("\n") else { | |
textBuffer += string | |
return | |
} | |
textBuffer += string | |
.replacingOccurrences(of: "\r", with: "") | |
.replacingOccurrences(of: "\n", with: "") | |
output() | |
input(serialPort) | |
} | |
/// ポート接続がシステムから開放された際に呼ばれる | |
func serialPortWasRemovedFromSystem(_ serialPort: ORSSerialPort) { | |
print("Serial port \(serialPort) was removed from system") | |
} | |
/// ポート接続にエラーが生じた際に呼ばれる | |
func serialPort(_ serialPort: ORSSerialPort, didEncounterError error: Error) { | |
print("Serial port (\(serialPort)) encountered error: \(error)") | |
} | |
/// ポート接続が完了した際に呼ばれる | |
func serialPortWasOpened(_ serialPort: ORSSerialPort) { | |
print("Serial port \(serialPort) was opened") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkdir (プロジェクト名) && cd $_ && swift package init --type executable && swift build && swift package generate-xcodeproj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ git submodule add https://github.com/armadsen/ORSSerialPort.git Frameworks/ORSSerialPort |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pod install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls -l /dev/cu.* | |
crw-rw-rw- 1 root wheel 21, 3 12 8 00:00 /dev/cu.usbmodem140001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Serial port usbmodem141001 was opened | |
>> Start! | |
<< 13 | |
>> 13 | |
<< q | |
Serial port usbmodem141001 was removed from system | |
Program ended with exit code: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ORSSerial | |
// ポート名 | |
let portName: String = "(ポート名)" | |
// ボーレート | |
let baudRate: NSNumber = 9600 | |
// 通知用デリゲート | |
let delegate = Delegate() | |
// ORSSerialPortManager を利用することでポート名が取得できます。 | |
// print(ORSSerialPortManager.shared().availablePorts.map { $0.name }) | |
// ORSSerialPortManager.shared().availablePorts[0].open() | |
// ポート名を元に初期化する | |
guard let port = ORSSerialPort(path: portName) else { exit(1) } | |
// デリゲートの指定 | |
port.delegate = delegate | |
// ボーレートの指定 | |
port.baudRate = baudRate | |
// 接続を開始 | |
port.open() | |
RunLoop.current.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pod 'ORSSerialPort' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 全てOutputモードに設定 | |
void pinModeAllOutput() { | |
int UnoPinNum = 13; | |
for(int i=0; i <= UnoPinNum; i++) { | |
pinMode(i, OUTPUT); | |
} | |
} | |
// String to Int | |
int convertNum(String str) { | |
return str.toInt(); | |
} | |
// 1秒間点灯 | |
void flash(int num) { | |
digitalWrite(num, HIGH); | |
delay(1000); | |
digitalWrite(num, LOW); | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinModeAllOutput(); | |
Serial.println("Start!"); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (Serial.available()) { | |
String str = Serial.readStringUntil('\n'); | |
String strx = str + '\n'; | |
int len = strx.length() + 1; | |
char buf[len]; | |
strx.toCharArray(buf, len); | |
int num = convertNum(buf); | |
Serial.println(num); | |
flash(num); | |
} | |
Serial.flush(); | |
delay(2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment