Does serial communication between PC and Arduino has to be in a loop?

I am trying to send a few variables from my python script to my Arduino. I am using pySerial. I am able to send the variables to the Arduino, but my Arduino sketch runs only for a second and then stops. But if I put the ser.write() command in a while loop (which I want to avoid) the sketch runs fine.

For example, I wrote this script on my PC -

import serial
ser = serial.Serial('COM3',9600)
counter = 'Y'
ser.write(counter)

This script should pass a "Y" to the Arduino and should stop executing.

My Arduino code-

int flag = 0;
void setup() {
  Serial.begin(9600); // set the baud rate
  Serial.println("Ready"); // print "Ready" once
  pinMode(13, OUTPUT);
}

void loop() {
  char inByte = ' ';
  if(Serial.available()){
     inByte = Serial.read(); // read the incoming data
     Serial.println(inByte); 
     flag ++;
  }
  if(flag == 1){
     digtalWrite(13, HIGH);
  }
}

Now according to me the Arduino should receive the data and increment the flag variable to 1. Next the program checks whether flag is one and if it is turns the onboard LED on. However when I run the python script, the onboard LED flashes for just a second and then goes blank. I again have to run the python script to make it light up or I have to put ser.write in a loop. I can't put ser.write in a loop as my script won't run further.

So how can a value be passed to an Arduino without having to put the ser.write() in a loop.

Thanks!

EDIT :

I wrote a new sketch, but it still doesn't work. Here's the sketch -

int ledPin = 13;
char data = ' ';

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (Serial.available()>0) {
     data = Serial.read();
  }
  if (data == 'Y'){
     digitalWrite(ledPin, HIGH);
  }
  delay(500);
}

EDIT 2: I suspect that the Arduino gets auto reset whenever the script tries to send data to it. Is it correct? My program works fine when I send input through the serial monitor.

Have a look at this Python - Arduino demo

The Python program should keep the Serial connection open until it is completely finished with the Arduino.

And please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

…R

Robin2:
Have a look at this Python - Arduino demo

The Python program should keep the Serial connection open until it is completely finished with the Arduino.

And please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

…R

My apologies, post edited.

And about your code, i’m sorry but it didn’t work, my Arduino resets every time it recieves data from the serial port.

How about I try doing this from say linux (I have Kali) or another computer ( I have a Raspberry Pi)

YaddyVirus: And about your code, i'm sorry but it didn't work, my Arduino resets every time it recieves data from the serial port.

Please post the exact code that you used and which did not work.

My demo programs were developed on Linux - it's what I use all the time.

...R

put a delay of 2 second after python opens serial port. Does your python code run just once or many times by another program?