Arduino将ASCII字符转换为字符串

Arduino convert ascii characters to string

我将此传感器与arduino板一起使用。

在第2页上,它描述了引脚5的串行输出。

http://www.maxbotix.com/documents/HRXL-MaxSonar-WR_Datasheet.pdf

The output is an ASCII capital"R", followed by four ASCII character
digits representing the range in millimeters,followed by a carriage
return (ASCII 13). The serial data format is 9600 baud, 8 data bits, no parity,
with one stop bit (9600-8-N-1).

这是我的arduino代码(不正确)。它仅输出" 82",即大写字母R。

1
2
3
4
5
6
7
8
9
10
11
void setup()
{  
 Serial.begin(9600);
}

void loop()
{  
 int data = Serial.read();
 Serial.println(data);
 delay (1000);
}

如何获取到字符串的距离?

非常感谢


您是否尝试过readBytesUntil方法?

您应该这样使用它:

1
2
byte DataToRead [6];
Serial.readBytesUntil(char(13), DataToRead, 6);

您的数据包含在DataToRead中(您在DataToRead[0]中的'R'等)


在我阅读时,问题是:
如何将字符的字节(ascii)表示转换为可读的字母数字字符,例如" a"与97?

实际问题:Arduino将ascii字符转换为字符串。
为什么ppl会发布未回答问题的回复?

没有确切答案,但使用(char)进行强制转换将使您进入那里。

1
2
3
char inByte  = 0;
inByte = (char)Serial.read();  // ascii 97 received
Serial.println((char)inByte); // => prints an 'a' without the quotes