I'm trying to work with ORSSerialPort in Swift. But I have problem with reading data from serial port. The trouble is wrong serialPort function implementation. Maybe this part would be helpful:
//Auduino code. Send data for request.
int incomingByte = Serial.read();
if (incomingByte = '1', DEC) {
Serial.print(h);
Serial.print(',');
Serial.print(t);
//OS X
class SerialController : NSObject, ORSSerialPortDelegate {
var port : ORSSerialPort
init(path: String){
port=ORSSerialPort(path: path)
port.close()
}
func open(){
port.baudRate=9600
port.delegate=self
port.open()
}
func close(){
port.delegate=nil
port.close()
}
func SendString(data: String){
port.sendData(data.dataUsingEncoding(NSUTF8StringEncoding))
}
//I'm trying to figure out how do I need to read data from serial
func serialPort(serialPort: ORSSerialPort!, idReceiveResponse:(NSData *)responseData toRequest:(ORSSerialRequest *)request) -> String {
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
println(string)
return(string)
}
}
let myPort = SerialController(path: "/dev/tty.usbmodem14141")
myPort.open()
myPort.SendString("1")
var dataFromSerial = myPort.serialPort //read
Did you copy/paste your code, or retype it for the question? It has several errors that would cause it not to compile, but I don't think those are the errors that provoked your question.
The main issue is that -serialPort:didReceiveResponse:toRequest:
will only be called if you previously sent an ORSSerialRequest
using the -sendRequest:
method. Since you're simply sending raw data, you need to implement -serialPort:didReceiveData:
(or rather serialPort(serialPort: ORSSerialPort!, didReceiveData data: NSData!)
since you're using Swift) instead.
You should take a look at the ORSSerialPortSwiftDemo sample project in the Examples folder if you haven't already.