Why is ORSSerialPort getting delegate not working in my Swift project

I have a simple SerialController class:

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))
    }

    func serialPortWasOpened(serialPort: ORSSerialPort!) {
        println("PORT IS OPEN....")
    }

    func serialPortWasClosed(serialPort: ORSSerialPort!) {
        println("PORT IS CLOSE")
    }

    func serialPort(serialPort: ORSSerialPort!, didReceiveData data: NSData!) {
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    }

    func serialPortWasRemovedFromSystem(serialPort: ORSSerialPort!) {
        println("PORT REMOVED")
    }

    func serialPort(serialPort: ORSSerialPort!, didEncounterError error: NSError!) {
        println("PORT ERR \(error)")
    }
}

      

and simple code to send data to FT232 adapter

func readLine()->String{
    return NSString(data:NSFileHandle.fileHandleWithStandardInput().availableData, encoding: NSUTF8StringEncoding)
}

let myPort = SerialController(path: "/dev/cu.usbserial-CN920229")

myPort.open()
println("type your data to send...")
let k = readLine()
myPort.SendString(k)
myPort.close()

      

The RX and TX pins of FT232 are connected together and I want to echo the data. I can connect to my adapter and SendString method correctly send data to FT232, but receive doesn't work !. in cocoaDemo I am testing my FT232 and I can get the correct answer. What can I do?

+3


source to share


1 answer


The main problem is that you close the port immediately and the program ends after sending data to the port. You need the program to work and the port is open to receive data. The easiest way to do this is to simply start the loop after sending the data:

func readLine()->String?{
    return NSString(data:NSFileHandle.fileHandleWithStandardInput().availableData, encoding: NSUTF8StringEncoding)
}

let myPort = SerialController(path: "/dev/cu.USA19H141P1.1")

myPort.open()
println("type your data to send...")
if let k = readLine() {
    myPort.SendString(k)
}

NSRunLoop.currentRunLoop().run() // <-- This will continue indefinitely.

      



Note that while this will allow you to receive data, it is certainly not a very well structured program. You can only send one line per program start because you only call readLine

() once, not loop and call it repeatedly. There is also no way to exit the program without killing it with ⌘-. or similar.

If you plan on turning this into a real program that you use for something more than just a quick one-off test, I would suggest looking at the CommandLineDemo in the ORSSerialPort examples folder. There are versions of this example available for both Swift and Objective-C.

+3


source







All Articles