简体   繁体   English

Arduino通过串行端口传递多个参数

[英]Arduino pass multiple parameters over serial port

For my project i have two servos and i need to set them to the right angle every time the program loops. 对于我的项目,我有两个伺服器,并且每次程序循环时都需要将它们设置为正确的角度。 In order do this i use a serial usb connection and send a number to set one of the servos. 为了做到这一点,我使用串行USB连接并发送一个数字来设置其中一个伺服器。 But for the second servo i need to pass two numbers in one message. 但是对于第二个伺服器,我需要在一条消息中传递两个数字。 I was thinking about a string "X,Y" that i could send and split it between the comma on the arduino, but it seems that i can only send numbers and only 1 number at that over the serial port? 我在考虑一个字符串“ X,Y”,我可以在arduino上的逗号之间发送和拆分它,但是看来我只能通过串行端口发送数字,并且只能发送一个数字。 How would i go about doing this. 我将如何去做。

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

According to the documentation , a call such as Serial.write("90,90"); 根据文档 ,调用如Serial.write("90,90"); should work and will result in the given string being sent. 应该可以正常工作,并导致发送给定的字符串。 There's no mention of the comma having some separate meaning. 没有提到逗号有一些单独的含义。

You should probably check the return value. 您可能应该检查返回值。

You can do: 你可以做:

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

Or: 要么:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM