Arduino serial port receiving string



   
 

Arduino serial port receiving string


Friends who are used to Arduino serial port transmission know that Serial.read() of Arduino can only read one byte at a time, but sometimes it is very troublesome to communicate with strings. 
Don't talk nonsense, just go to the complete example: 

Only one Arduino is needed for compilation and no external components are required. 
Use the serial monitor of the Arduino compiler to see the result. What text we type in, what text will be returned below. 

String comdata = "";

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

void loop()
{
    
while (Serial.available() > 0)  
    {
        
comdata += char(Serial.read());
        
delay(2);
    }
    
if (comdata.length() > 0)
    {
        
Serial.println(comdata);
        
comdata = "";
    }
}

The code is very simple, comdata is a string type variable. Serial.available() is the amount of data in the current serial buffer pool. Serial.read() is a statement that reads the buffer pool, and can only read one byte at a time. 
uses String type variables, it is very simple to realize the addition of characters to strings, as well as string output, assignment and other troublesome issues, so very simple codes can handle serial data. 
Pay special attention to delay(2) when reading the serial port, otherwise the serial port buffer will not have enough time to accept data. Even if the delay is reduced, errors will occur. The specific value can also be determined experimentally. 

One more reminder: comdata said it is a string, and also an array. To quote each word, you can use comdata[0], comdata[1]. . . comdata[n]. If we want to take out each byte, we can quote each one. 


Effect: What string is input, what is output. 
Input: 
 
After pressing send: 
 



An example is attached. Inputting 1011101.. in the serial port will cause the D2~Dx pins of Arduino to generate high/low levels. Of course, the data sent once depends on the Arduino pins. Depends on the number, such as Arduino UNO/nano, there are only twelve pins D2~D13. We just hit 12 numbers. For example, 101101011010, if there are characters other than 0 and 1 in the middle, the bit setting is automatically skipped: such as 122202221222. Because 2 is not in the allowable range, just set the value of D2/D6/D10:


String comdata = "";
void setup()
{
  
Serial.begin(9600);
  
for(int i = 2; i <= 13; i++) pinMode(i, OUTPUT);
}

void loop()
{
  
while (Serial.available() > 0)
  {
    
comdata += int(Serial.read()) - '0';
    
delay(2);
  }
  
if(comdata.length() > 0)
  {
    
for(int i = 0; i < comdata.length(); i++)
    {
      
if(comdata[i]=='0'||comdata[i]=='1')
      {
        
digitalWrite(i + 2, comdata[i] - '0');
        
Serial.print("Pin ");
        
Serial.print(i + 2);
        
Serial.print(" is ");
        
Serial.println(comdata[i]);
      }
    }
    
comdata = "";
  }
}





 

 


One more: Advanced version (to be tested)
Enter six comma separated numbers such as: 50,20,5,255,20,20
can make Arduino's PWM pins (3,5,6,9,10,11): emit light according to the PWM value. So the number of commas must be 0~255



String comdata = "";
int numdata[6] = {0}, PWMPin[6] = {3, 5, 6, 9, 10, 11}, mark = 0;
void setup()
{
  
for(int i = 0; i < 6; i++) pinMode(PWMPin[i], OUTPUT);
  
Serial.begin(9600);
}

void loop()
{
  
int j = 0;
  
while (Serial.available() > 0)
  {
    
comdata += char(Serial.read());
    
delay(2);
    
mark = 1;
  }

  
if(mark == 1)
  {
    
Serial.println(comdata);
    
Serial.println(comdata.length());
    
for(int i = 0; i < comdata.length() ; i++)
    {
      
if(comdata[i] == ',')
      {
        
j++;
      }
      
else
      
{
        
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
      }
    }
    
comdata = String("");


    
for(int i = 0; i < 6; i++)
    {
      
Serial.print("Pin ");
      
Serial.print(PWMPin[i]);
      
Serial.print(" = ");
      
Serial.println(numdata[i]);
      
analogWrite(PWMPin[i], numdata[i]);
      
numdata[i] = 0;
    }
    
mark = 0;
  }
}

Intelligent Recommendation

Use of Arduino serial port

The serial port isArduinoThe interface for communicating with other devices, we need to have a good grasp of its use.ArduinoCommon functions related to serial port use10A(With the upgrade of the versi...

Detailed Arduino serial port

The serial port is one of the most important communication methods of the single-chip microcomputer. Our mouse, keyboard, etc. are all communicated through the serial port, so how to use the Arduino s...

Arduino serial port

Arduino Control GPIO using serial communication After the ESP8266 is bought, sometimes there is no switch or the like for inputting ESP8266. There is also a case where there is no LED observation GPIO...

FPGA serial port transceiver string (2) - receiving module

Today, the previously written UART program has been further optimized to solve the historical problems. First, let's review the sending module written in the previous section. Here is a special descri...

STM32 - a method of receiving a string in serial port to avoid overflow and interrupt disorders

A method of avoiding overflow and interrupt disorders: STM32-serial port receiving strings: The key is that the understanding bus idle interruption is that the bus has no level for a period of time, i...

More Recommendation

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 dat...

ARDUINO development, use serial port receiving fixed-long strings to process the run function

Arduino's serial port seems to be only one reader, except for the parseint (). I get a character string in the specified format to achieve control. Each command starts with a # end, with a total lengt...

STM32F103 instance application (13) - serial port receiving interrupt + serial port idle interrupt reception unordance to string

I. Introduction to the application This article describes how the serial port interrupt receives the method of unregified long strings. Configure the serial port 1 to receive an interrupt enable, and ...

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

Top