And now let's see how to make your python program retry.
To connect to serial after it has failed.
That can be super useful.
So basically when you start the program, so any program where you're going to connect to the Arduino
with serial communication, you can choose what you want to do if the communication is not successfully
working.
So you can leave it like that, of course.
And if it's not working, you're just going to get an error.
And then you need to start the program again.
Or you can add a mechanism.
Okay.
Just on that line here to try again if it can't connect.
And that's what I'm going to show you here.
So how to do that?
Well, first, so I'm going to unplug my Arduino so the Arduino is not connected.
Let's see again, what error do we have?
We have a serial exception here.
Okay.
So we are going to get this exception.
All right.
So I'm going to do try with this.
And then except.
And the exception is serial dot serial exception.
You can use that.
Okay.
So if you have a serial exception, which means that you have a problem when you connect, which can
be, for example, that the board is not connected or that the port is already busy, then instead of
exiting the program right now, you're going to go inside this code to handle the exception.
And so what you want to do is that if you reach that, you want to go back to here, and if you want
to go back to here, then I'm going to use a while loop.
So let's do while true, I'm going to put that inside the while loop, of course, with a colon here.
So while true means that first we're going to enter this anyway, we're going to execute this code if
this is successful, which means that we don't have the exception, then in that case, what we want
to do is we want to break out of the loop.
Okay.
So I'm going to add break.
So if this is successful, we directly go after the while loop and we go to time.sleep.
And if it is not successful, we are going to get the exception.
And in the exception, well, let's print something.
Let's say print could not connect to serial trying again.
And let's add, let's say time dot sleep one.
So if it's not working, we are going to print something, sleep for one second and then come back to
the beginning of the loop.
So we try again every one second.
Okay.
I'm just going to add another print here so we can know directly after we have successfully connected
that the connection is okay, so let's say success fully connected to Serial.
Okay?
And that should be it.
So now I'm going to run this code.
The Arduino is still disconnected, so you can see what we're going to have is could not connect to
trying again.
And now I'm going to connect the Arduino.
The program is still running and you can see successfully connected to serial.
Serial.
Okay.
And then we have the application which is starting.
I press Ctrl C to kill the application.
Okay, so with this code, it's going to try again and again and again until the connection can be established
and that can be very useful.
We are going to use that actually in the final project of this course.
I'm not going to use it in every activity.
Okay, because that adds a bit of code.
If we want to just focus on 1 or 2 functionalities.
But for the final project we are going to use that and actually as an improvement that I'm not going
to code here, but you could try to do that.
You could try to add a maximum number of tries.
Okay, let's say you want to try for maximum ten times, which means that after 10s the problem is going
to exit anyway.
So you could add a mechanism here with another variable that will keep the number of tries and continue
to try again until you reach the maximum number of tries.