Serial port receiving data experiment

Serial port receiving data experiment

Experimental phenomena

The PC sends a character, the arduino stick returns it to the PC after receiving it

Theoretical study

Use if(Serial.available()>0) to query whether there is serial port data input.
Use Serial.read(); to read one byte of data received.

Code writing

char inbyte = ' ';
void setup() {
    
    
  // put your setup code here, to run once:
  Serial.begin(9600);//初始化串口设置波特率
  pinMode(13,OUTPUT);
}
void loop() {
    
    
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    
    
    inbyte = Serial.read();//每次只是读取一个字符
    Serial.write(inbyte);
    //如果是Serial.print(inbyte);则打印的是对应的输入的ASCII码值
  }
  digitalWrite(13,HIGH);
  delay(1000);
  digitalWrite(13,LOW);
  delay(1000);
}

Guess you like

Origin blog.csdn.net/qq_45671732/article/details/109490002
Recommended