WEBVTT

00:00.080 --> 00:03.830
And now let's see how to make your python program retry.

00:03.860 --> 00:07.220
To connect to serial after it has failed.

00:07.430 --> 00:09.140
That can be super useful.

00:09.380 --> 00:15.470
So basically when you start the program, so any program where you're going to connect to the Arduino

00:15.500 --> 00:21.950
with serial communication, you can choose what you want to do if the communication is not successfully

00:21.950 --> 00:22.760
working.

00:22.760 --> 00:25.130
So you can leave it like that, of course.

00:25.130 --> 00:28.400
And if it's not working, you're just going to get an error.

00:28.400 --> 00:31.010
And then you need to start the program again.

00:31.010 --> 00:32.660
Or you can add a mechanism.

00:32.660 --> 00:32.940
Okay.

00:32.960 --> 00:38.570
Just on that line here to try again if it can't connect.

00:38.570 --> 00:40.970
And that's what I'm going to show you here.

00:40.970 --> 00:43.370
So how to do that?

00:43.370 --> 00:48.230
Well, first, so I'm going to unplug my Arduino so the Arduino is not connected.

00:48.230 --> 00:50.810
Let's see again, what error do we have?

00:50.840 --> 00:54.020
We have a serial exception here.

00:54.140 --> 00:54.560
Okay.

00:54.560 --> 00:59.030
So we are going to get this exception.

00:59.180 --> 00:59.690
All right.

00:59.690 --> 01:03.090
So I'm going to do try with this.

01:03.480 --> 01:06.330
And then except.

01:06.330 --> 01:12.180
And the exception is serial dot serial exception.

01:12.180 --> 01:12.930
You can use that.

01:12.930 --> 01:13.380
Okay.

01:13.380 --> 01:19.230
So if you have a serial exception, which means that you have a problem when you connect, which can

01:19.230 --> 01:25.770
be, for example, that the board is not connected or that the port is already busy, then instead of

01:25.890 --> 01:31.200
exiting the program right now, you're going to go inside this code to handle the exception.

01:31.200 --> 01:36.960
And so what you want to do is that if you reach that, you want to go back to here, and if you want

01:36.960 --> 01:40.830
to go back to here, then I'm going to use a while loop.

01:40.830 --> 01:49.530
So let's do while true, I'm going to put that inside the while loop, of course, with a colon here.

01:49.650 --> 01:58.050
So while true means that first we're going to enter this anyway, we're going to execute this code if

01:58.050 --> 02:03.420
this is successful, which means that we don't have the exception, then in that case, what we want

02:03.420 --> 02:06.690
to do is we want to break out of the loop.

02:06.780 --> 02:07.260
Okay.

02:07.260 --> 02:08.850
So I'm going to add break.

02:08.850 --> 02:15.450
So if this is successful, we directly go after the while loop and we go to time.sleep.

02:15.870 --> 02:20.310
And if it is not successful, we are going to get the exception.

02:20.310 --> 02:22.980
And in the exception, well, let's print something.

02:22.980 --> 02:34.530
Let's say print could not connect to serial trying again.

02:35.520 --> 02:41.640
And let's add, let's say time dot sleep one.

02:41.640 --> 02:50.160
So if it's not working, we are going to print something, sleep for one second and then come back to

02:50.160 --> 02:51.480
the beginning of the loop.

02:51.480 --> 02:54.870
So we try again every one second.

02:55.080 --> 02:55.560
Okay.

02:56.310 --> 03:03.290
I'm just going to add another print here so we can know directly after we have successfully connected

03:03.290 --> 03:13.580
that the connection is okay, so let's say success fully connected to Serial.

03:15.950 --> 03:16.370
Okay?

03:16.370 --> 03:18.950
And that should be it.

03:18.950 --> 03:22.250
So now I'm going to run this code.

03:22.310 --> 03:28.730
The Arduino is still disconnected, so you can see what we're going to have is could not connect to

03:29.150 --> 03:30.050
trying again.

03:30.050 --> 03:32.570
And now I'm going to connect the Arduino.

03:32.600 --> 03:38.390
The program is still running and you can see successfully connected to serial.

03:38.480 --> 03:39.110
Serial.

03:39.140 --> 03:39.530
Okay.

03:39.530 --> 03:42.320
And then we have the application which is starting.

03:42.320 --> 03:45.320
I press Ctrl C to kill the application.

03:46.190 --> 03:52.880
Okay, so with this code, it's going to try again and again and again until the connection can be established

03:52.880 --> 03:55.070
and that can be very useful.

03:55.070 --> 03:58.220
We are going to use that actually in the final project of this course.

03:58.220 --> 04:01.160
I'm not going to use it in every activity.

04:01.190 --> 04:03.890
Okay, because that adds a bit of code.

04:03.890 --> 04:07.280
If we want to just focus on 1 or 2 functionalities.

04:07.280 --> 04:13.190
But for the final project we are going to use that and actually as an improvement that I'm not going

04:13.190 --> 04:15.590
to code here, but you could try to do that.

04:15.620 --> 04:18.770
You could try to add a maximum number of tries.

04:18.800 --> 04:25.790
Okay, let's say you want to try for maximum ten times, which means that after 10s the problem is going

04:25.790 --> 04:26.750
to exit anyway.

04:26.750 --> 04:33.170
So you could add a mechanism here with another variable that will keep the number of tries and continue

04:33.170 --> 04:37.310
to try again until you reach the maximum number of tries.
