How to save the received data via Serial port

Hi arduino~
I’m trying to receive data from other MCU by python code

and I have been searching for receive data and save that data indivisually

This means I’m going to send 2 data like this(could be integer type, could be string type)

PYTHON ----> data1 data2 data1 data2 data1 dat1 data1 data2 data1 data2 ------> ARDUINO

and I should save this data at different values usch as A and B
data will be received sequentially, so these datas should be refreshed sequentially too

but I dont know how to make this algorithm to work properly
could you give me an adivice? or some link which is fit to this?

i’ve searched on google but cant find what I want
I need your help. please help me :slight_smile:

This is probably simpler than you think, but you are failing to explain your intentions.

to receive from 'other MCU" you might find this useful

but you don't say where you want to save it. Assuming it is on board, the SD examples in the IDE have all you need. It is essentially a matter of reading from serial and printing to SD, along with perhaps a timestamp using an on-board clock. If it is to go somewhere else, you might send it to a PC terminal where it may be recorded and timestamped using the PC clock.

This Python - Arduino demo may help

The examples in the link that @Nick_Pyner gave were written more recently but generally use the same approach.

...R

Nick_Pyner:
This is probably simpler than you think, but you are failing to explain your intentions.

to receive from 'other MCU" you might find this useful

http://forum.arduino.cc/index.php?topic=396450.0

but you don't say where you want to save it. Assuming it is on board, the SD examples in the IDE have all you need. It is essentially a matter of reading from serial and printing to SD, along with perhaps a timestamp using an on-board clock. If it is to go somewhere else, you might send it to a PC terminal where it may be recorded and timestamped using the PC clock.

yes I want to save it on Arduino, but not collecting (I mean dont want to accumulate the data)
and why I didn't describe where to save because I dont know where to save
If i want to save string type what should I make to save this string?

Robin2:
This Python - Arduino demo may help

The examples in the link that @Nick_Pyner gave were written more recently but generally use the same approach.

...R

I think this will help me find the way out!
Thanks for great the post :slight_smile:

kosh09:
yes I want to save it ....................dont want to accumulate

Hmmm

and why I didn't describe where to save because I dont know where to save

See the previous. You have two choices - on-board, or off-board. If it is at all possible, off-board on a PC is always easier, but only you can make the decision.

If i want to save string type what should I make to save this string?

I think the best thing to do is not use strings at all. You simply send, save, or display the data as it is received, each trip round the loop.

Nick_Pyner:
I think the best thing to do is not use strings at all. You simply send, save, or display the data as it is received, each trip round the loop.

Could u teach me differences between receving data as char form and string form?
I always wonder what is different

Could u teach me differences between receving data as char form and string form?
I always wonder what is different

A string is a NULL terminated array of chars.

kosh09:
Could u teach me differences between receving data as char form and string form?
I always wonder what is different

It is not the receiving that is different. It is the type of the Arduino data that you use for storing the data. cstrings are char arrays terminated with 0. You determine the size of the array when you write the program.

The String class (capital S) dynamically allocates the space for the data and that dynamic allocation does not work well in the small Arduino memory.

...R

Robin2:
The String class (capital S) dynamically allocates the space for the data and that dynamic allocation does not work well in the small Arduino memory.

...R

Then the work I want to do just use char? I want to send 4 letters as 1 set.
Then I should use just 4 letters size of char value, then save here and send?

kosh09:
Then the work I want to do just use char? I want to send 4 letters as 1 set.
Then I should use just 4 letters size of char value, then save here and send?

Please post the program you propose to use as that is the only way to be completely sure that you are doing it correctly.

...R

Robin2:
Please post the program you propose to use as that is the only way to be completely sure that you are doing it correctly.

…R

Hi again!
I dont know whether this is right or wrong, but here is my code
Board type is UNO and use motor shield

  #include <Servo.h>

  const int servoPin = 6;
  Servo myservo; // 서보모터 선언
  
  const int motor_direction = 13;
  const int motor_pwm = 11;
  const int motor_break = 8;  // HIGH 일때 강제정지

//****************************************************//
//**** 위의 설정은 DC모터의 B쪽에 연결을 했을때입니다. ****//
//****************************************************//

void setup()
{
  myservo.attach(servoPin); // 서보모터를 5번핀에 연결
  myservo.write(90);        // 90'가 중심각도 여기서 +,- 30~31'가 조향임.
                            // 서보모터 회전정도를 정함 30~31이 최고임.
  

  pinMode(motor_direction, OUTPUT);
  pinMode(motor_pwm, OUTPUT);
  pinMode(motor_break, OUTPUT);

  Serial.begin(38400);
}



void loop()
{
  if(Serial.available())
  {
    if(Serial.find("R30")){myservo.write(120);}
    if(Serial.find("R25")){myservo.write(115);}
    if(Serial.find("R20")){myservo.write(110);}
    if(Serial.find("R15")){myservo.write(105);}
    if(Serial.find("R10")){myservo.write(100);}
    if(Serial.find("R05")){myservo.write(95);}
    if(Serial.find("S")){myservo.write(90);
      }
    if(Serial.find("L05")){
      myservo.write(85);
      }
    if(Serial.find("L10")){
      myservo.write(80);
      }
    if(Serial.find("L15")){
      myservo.write(75);
      }
    if(Serial.find("L20")){
      myservo.write(70);
      }
    if(Serial.find("L25")){
      myservo.write(65);
      }
    if(Serial.find("L30")){
      myservo.write(60);
      }


    if(Serial.find("F")){  // 앞으로 가는 문자열 넣어주면 된다.
      digitalWrite(motor_pwm, 200);
      digitalWrite(motor_direction, HIGH);
      digitalWrite(motor_break, LOW);
      }
    if(Serial.find("H")){  // 정지하는 문자열 넣어주면 된다.
      digitalWrite(motor_pwm, 0);
      digitalWrite(motor_direction, HIGH);
      digitalWrite(motor_break, HIGH);
    
      }
    if(Serial.find("B")){ // 뒤로가는 문자열 넣어주면 도니다.
      digitalWrite(motor_pwm, 200);
      digitalWrite(motor_direction, LOW);
      digitalWrite(motor_break, LOW)
      }
    } 
}

I don't think you can use Serial.find() like that. For example it starts listening for "R30" but it it won't find it because "R25" was actually sent. However by the time it realizes that it will have consumed the 'R' so it won't be able to find "R25" either.

Go back to the examples in the links you were given in Replies #1 and #2. You need to receive a message and only then look to see what it contains.

...R