【问题标题】:ASCII into an INT ArduinoASCII 到 INT Arduino
【发布时间】:2021-11-24 12:57:39
【问题描述】:

所以我想通过 RX TX 从一个价值为 INT 的 Arduino 将传感器发送到另一个 Arduino。问题是我想用这个值打开另一个 Arduino 上的 LED。但是数字以 ASCII 块的形式出现,我想知道是否以及如何将数字转换为 INT。

这是发件人的代码

int i = 601;
int sensorValue = 0;
int input = A0;
void setup () {

Serial.begin (9600); // Begin serial communication with 9600 baud

sensorValue = analogRead (input); // here the sensor value is written into the variable "sensorValue"

Serial.println (sensorValue); // send from the variable "sensorValue" via the serial interface
}
void loop () {

}

【问题讨论】:

    标签: arduino integer ascii


    【解决方案1】:

    要以二进制形式传输int,您必须将 int 的字节作为字节数组发送。

    Serial.write((const uint8_t*) &sensorValue, sizeof(sensorValue));
    

    你可以收到它们

    if (Serial.available()) {
      Serial.readBytes((uint8_t*) &sensorValue, sizeof(sensorValue));
      ...
    }
    

    如果要将数字作为文本传输并在另一个 Arduino 上解析,可以使用 Serial.parseInt() 函数。

    if (Serial.available()) {
      int sensorValue = Serial.parseInt();
      ...
    }
    

    【讨论】:

    • 谢谢你的回答我会试试看是否有效
    • 在我能够完成项目的情况下帮助了我
    【解决方案2】:

    根据您的代码: 从analogRead() 返回的数据不是ascii。它是从 0 到 1023。 模数转换器 (ADC) 将引脚的电压转换为数字。例如 0 代表 0 伏,1023 代表 5 伏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      相关资源
      最近更新 更多