【问题标题】:Arduino pass multiple parameters over serial portArduino通过串口传递多个参数
【发布时间】:2012-06-05 16:12:30
【问题描述】:

对于我的项目,我有两个伺服系统,每次程序循环时我都需要将它们设置为正确的角度。为了做到这一点,我使用串行 USB 连接并发送一个数字来设置其中一个伺服系统。但是对于第二个伺服,我需要在一条消息中传递两个数字。我正在考虑一个字符串“X,Y”,我可以发送它并在 arduino 上的逗号之间拆分它,但似乎我只能通过串行端口发送数字和 1 个数字?我将如何去做这件事。

Serial.write("90,90");//does not work...

【问题讨论】:

  • 您发布的代码Serial.write("90,90");//does not work...是在发送命令的PC上还是在arduino上?我只问,因为根据您的问题,您正在向 arduino 发送值。你能发布你用来发送命令的代码吗?发布您用来接收它们的代码也会很有帮助。

标签: c arduino


【解决方案1】:

According to the documentation,像Serial.write("90,90"); 这样的调用应该可以工作,并且会导致给定的字符串被发送。没有提到逗号有不同的含义。

您可能应该检查返回值。

【讨论】:

    【解决方案2】:

    你可以这样做:

    Serial.write(90);
    Serial.write(90);
    

    或者:

     byte buf[] = {90, 90};
     Serial.write(buf, sizeof buf);
    

    【讨论】:

    • 好的,我如何在逗号之间分割字符串并解析整数?本质上我想要Java中的等价物: String s = Serial.read(); int x = Integer.parseInt(s.split(",")[0]); int y = Integer.parseInt(s.split(",")[1]);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多