我正在尝试在 Swift 中使用 ORSSerialPort。但是我从串口读取数据有问题。问题是错误的serialPort函数实现。也许这部分会有所帮助:
- (void)serialPort:(ORSSerialPort *)serialPort didReceiveResponse:(NSData *)responseData toRequest:(ORSSerialRequest *)request;
//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