【问题标题】:Issue with arduino/mac communication using ORSSerialPort使用 ORSSerialPort 的 arduino/mac 通信问题
【发布时间】:2014-04-19 02:20:27
【问题描述】:

我正在尝试通过 USB 将一个三位数的数字从 XCode 基础命令行工具发送到 arduino,作为发送数据流的概念证明。 arduino 代码应该按照传入数据中的数字闪烁的次数闪烁。使用 arduino IDE 中的串行监视器可以完美运行,但是当我尝试使用使用 ORSSerialPort 的 Objective-C 程序时,arduino 上的 Rx 灯闪烁,表示它已接收到数据,但没有其他反应。

这是 Objective-C 代码:

#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>
#import "ORSSerialPort.h"
#import "ORSSerialPortManager.h"



int main(int argc, const char * argv[])
{
    @autoreleasepool {

        ORSSerialPort *arduino = [ORSSerialPort serialPortWithPath:@"/dev/tty.usbmodem26231"];
        //initalizes ORSSerialPort instance

        NSString *string = @"012"; //creates string from the 3-digit number

        NSData *outgoingdata = [string dataUsingEncoding:NSASCIIStringEncoding]; 
        //encodes string using ASCII

        int number = 1200;
        NSNumber *baudrate = [[NSNumber alloc] initWithInt:number];
        //initializes an NSNumber for the baud rate

        [arduino open]; //opens port
        baudrate = arduino.baudRate; //sets baud rate
        [arduino sendData:outgoingdata]; //sends data
        [arduino close]; //closes port

        NSLog(@"%@ sent", string); //logs the number
    }   
    return 0;
}

这里是arduino代码:

#include <SoftwareSerial.h>

int i100;
int i10;
int i1;
int total;

void setup() {
  Serial.begin(1200);
  pinMode(2, OUTPUT);
}

void loop() {
int mail = Serial.available(); //reads number of available bytes
  if(mail >= 3) {
    int i100raw = Serial.read()-48; //reads 3 bytes and decodes from ASCII by subtracting 48
    int i10raw = Serial.read()-48;  
    int i1raw = Serial.read()-48; 

    if(i1raw >= 0) {  //checks if each byte is a valid input 
      i100 = i100raw; 
    }
    if(i10raw >= 0) {
      i10 = i10raw; 
    }
    if(i1raw >= 0) {
      i1 = i1raw; 
    }

    total = (i100*100)+(i10*10)+(i1); //puts together 3 digit number from 3 bytes

    while(total > 0) {       //flashes light  
      digitalWrite(2, HIGH);
      delay(50);
      digitalWrite(2, LOW);
      delay(50);
      total--;
    }   
  }
} 

【问题讨论】:

    标签: objective-c serial-port usb arduino orsserialport


    【解决方案1】:

    一些想法:

    1. 主要问题是您永远不会为ORSSerialPort 设置baudRate。 (你有向后的分配。)另外,我看到的例子在打开端口之前将波特率设置为ORSSerialPort。所以,我建议:

      arduino.baudRate = @(1200); //sets baud rate
      [arduino open];             //opens port
      
    2. 另外,在我的测试中,如果我在写入数据后立即尝试关闭到 Arduino 的端口,则不会收到。在关闭该端口之前,您可能需要稍等片刻

    3. 您可能还想为发送到 Arduino 的内容使用新的行终止符。这显然是发送代码和接收代码都发生了变化,但它会比仅读取接下来的三个字节更加健壮。

    4. 我可能还建议,如果您想特别小心,您可能需要为ORSSerialPort 对象设置delegate 并实现ORSSerialPortDelegate 方法。那里有一种错误报告方法,您可能想利用它。

    5. 当我在 Arduino 上 begin 我的 Serial 时,我包含一个等待 Serial 连接的循环。我从中获取的代码 sn-p 说这仅适用于 Leonardo,但如果您在第一次从 Mac 端打开连接时发现无法成功接收数据,则值得尝试:

      void setup() 
      {
        pinMode(2, OUTPUT);
      
        Serial.begin(1200); 
        while (!Serial) {
          // wait for serial port to connect. Needed for Leonardo only
        } 
      
        Serial.println("OK. Connected.");
      }
      

      此外,当您的loop() 成功读取来自Serial 的输入时,您可以让 Arduino 回写内容(通过Serial.println),您可以让 Mac 读取这些内容以确认成功接收数据。这是一种有效地确认收到命令的方法,可以帮助您将通信相关问题与接线问题隔离开来。

    【讨论】:

    • 正确设置波特率并在关闭端口之前添加延迟并没有解决问题,目前正在研究如何集成新的线路终结器
    • 我上一条评论错了。我刚刚发现打开端口后需要延迟,就像关闭它之前一样。
    • 另外,延迟好像至少要2秒。
    • 嘿 - 刚刚写了一封回复,建议您添加延迟。是的,它需要几秒钟。如果您需要,有一种方法可以禁用 Arduino 重置 - 否则只需在您的 Obj-C 代码中添加延迟。
    • @Rob,对不起,并不是要暗示这一点的内容是错误的。反向波特率分配确实显然是实际问题。我只是不希望人们遇到这个问题/答案,并认为存在打开后设置波特率的限制。也感谢您的好答案(当您最初发布时,我支持它。)
    【解决方案2】:

    刚刚遇到同样的问题并解决了。将 DTR 设置为 1 可能会解决此问题。

    arduino.DTR = YES;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多