|
本帖最后由 whiteraven 于 2021-3-2 15:17 编辑
刚接触Arduino的小白,在做一个串口通信的实验,就是在电脑Arduino IDE软件的串口监视器上输入ascii码形式的报文给Arduino Uno board,然后板子可以返回对应报文的二进制形式显示在串口监视器上。例如我在串口监视器上发送报文“hello",则UNO board返回“[size=14.6994px]01101000 [size=14.6994px]01100101 [size=14.6994px]01101100 [size=14.6994px]01101100 [size=14.6994px]01101111”二进制形式显示在serial monitor上,但是现在我的代码只能实现发送“hello”然后原样返回“hello”,求助各位大佬能帮我看一下代码应该怎么改吗,谢谢啦~
[size=14.6994px]String comdata = "";
[size=14.6994px]void setup()
[size=14.6994px]{
[size=14.6994px] Serial.begin(9600);
[size=14.6994px]}
[size=14.6994px]void loop()
[size=14.6994px]{
[size=14.6994px] while (Serial.available() > 0)
[size=14.6994px] {
[size=14.6994px] comdata += char(Serial.read());
[size=14.6994px] delay(2);
[size=14.6994px] }
[size=14.6994px] if (comdata.length() > 0)
[size=14.6994px] {
[size=14.6994px] Serial.print(comdata);
[size=14.6994px] comdata = "";
[size=14.6994px] }
[size=14.6994px]}
|
|