Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Newline and Carriage-Return into Swift String

I'm writing an OS X app that interacts with an Arduino UNO via serial connection. It currently has no problem reading strings from the UNO but I can't manage to send it the proper Newline and Carriage-Return characters on outgoing strings.

The code responsible for sending the string is as follows:

    @IBAction func SendCommand(sender: AnyObject) {
    let data = self.sendTextField.stringValue.dataUsingEncoding(NSUTF8StringEncoding)
    self.serialPort?.sendData(data)
}

For the moment I have attempting to manually insert '\r\n' at the end of messages when the program is running. Should this not be equivalent to what the Arduino IDE would do programatically in it's own serial monitor? However my app isn't interpreting these commands as Newline of Carriage-Return characters. Is it only possible programatically and if so how?

Thank you in advance!

like image 541
user3185748 Avatar asked Dec 09 '14 01:12

user3185748


1 Answers

Well after all it looks like the escape characters weren't being recognized when I included them with the command so I've fixed it in the program itself. Thank you for your time!

   @IBAction func SendCommand(sender: AnyObject) {
    var test = (self.sendTextField.stringValue) + "\r\n"
    let data = test.dataUsingEncoding(NSUTF8StringEncoding)
    self.serialPort?.sendData(data)
}
like image 60
user3185748 Avatar answered Sep 21 '22 15:09

user3185748