arduino receives string and int data through serial port

The arduino (0, 1) pin serial port is connected to HC-05 Bluetooth, which can be connected to other Bluetooth or Android mobile phone Bluetooth

The source code of serial port receiving string type data is as follows

String comdata = "";
void setup()
{
    Serial.begin(9600);
}

void loop()
{
    while (Serial.available() > 0){
        comdata += char(Serial.read()); //Read one char character each time and add them
        delay(2);
    }
    if (comdata.length() > 0){
                 Serial.println(comdata); //Print the received characters
        comdata = "";
    }
}

The source code of serial port receiving int type data is as follows

int p;
void setup(){
  Serial.begin(9600);
}

void loop(){
 While (Serial.available()> 0) {// The number of characters received by the serial port is greater than zero.
         p = Serial.parseInt(); // Find a valid integer in the serial data stream.
         Serial.println(p); //Print the received number
    }
} 



Intelligent Recommendation

ARDUINO research through serial port to the upper machine

ARDUINO research through serial port to the upper machine Foreword Data Transfer Protocol The transmission protocol is required according to the actual situation Packet Upper machine analysis Foreword...

arduino(2)--Receive data through serial port through ESP8266 module, and use TCP protocol for LAN communication

arduino--Receive data through serial port through ESP8266 module, and use TCP protocol for LAN communication Hardware preparation Port connection Network debugging assistant Code Function Description ...

Python receives a data from 51 single chip microcomputer through serial port (matching 2)

☼ Developer: Fool's Black Technology Studio ☼ QQ:1590643673 ☼ Email: [email protected] ☼ Official website: City of the Fool's Sky www.yuzhe.store ☼ Station B: Fool's Black Technology Studio https://sp...

More Recommendation

STM32F429 serial port IDLE interrupt + DMA receives serial port data

The key points of the above code are: 1, open the serial port free interrupt 2, serial port allows DMA request...

Unity3D serial port receives and sends data

Recently used serial port, encountered some pits, record it. Look at the code first: The pit is also annotated in the code. 1. When the number of characters is received, the code running environment a...

Java serial port receives data display to label

Recently, there is a small task, that is, the data sent from the lower computer is displayed on the label in real time, and there is no clue. Later I saw this blog post, link: It is better to open a t...

Stm32f103 serial port receives variable length data

Recommended method three   Method 1: The serial port accepts the data and the timer determines whether the timeout accepts the data completion. Method 2: DMA accepts +IDLE interrupt Implementatio...

Serial port in stm32 receives indefinite byte data

Platform: STM32ZET6 (core board) + ST-LINK / V2 + USB serial cable + SIM800C + DuPont line (used to connect SIM800C and STM32 development board) The idea adopted by the serial port to receive data of ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top