Python Serial Port Commuincation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome to my channel and in this video we'll understand how to use serial port in python with the help of this tiny usb2 ttl board and what we are going to do is we are going to transmit something from our python code and as we are going to short this rx and tx pin so whatever we transmit we are going to get reverse uh the transmitted data and we are going to capture that in our python code right so let's understand how to do this so first of all i need to short this rx and tx pin so i have shorted this rxntx tx pin and first we'll understand how to uh check this whether this is working or not i have a special video for this to how to start using this link is in the description box you can check that check that out as well so in here i'll first connect it to my extended cable so here i connect my uh your this tiny board and uh let's see if if what comport does this gives me so for that we need device manager okay for in device manager we'll go let's let this open up so here i have i have this com port right and the number is comp one so in this way i know that uh if i connect this let's see if if it refreshes yes so serial port went away yeah so we have again com port one so com port one is my uh you know com port number so i must be using this comp one so i i'll keep this connected and uh let's start with our program here the use of this is complete so let me close this and uh let's start using our pycharm so pycharm let this open up let's create a new project and here we have this new project button new project and let's give it a path here i have this option no i don't want to use here let's give python serial port serial port test so i'm going to give this name i've created a new project new folder here let's select that folder perfect so i have my interpreter selected which is at different location awesome so let's create our project okay so our project is created and main dot pi is generated and uh anyway i'm not going to use this uh anything here so i i'm going to delete this and uh while handling serial port you need to you know to have a few attributes to be set in serial uh port of po port within the python and those attributes are these four and these five okay that is port byte size uh baud rate byte size timeout and stop bit and where we are going to set this attribute in a serial serial port library okay so do we have serial port library installed let's check that out so here we have this option called terminal and in terminal let this open up okay let this open up and let's see do we have a peep list do we have serial port installed no we don't have serial port installed so we need to install it first so how can we install this let's type this command peep install pi serial perfect so we have successfully installed our pi serial library and we can start using this uh this library so what we can do very first comma very first is to uh import that library okay import serial and uh now we can start creating our serial port object so serial port object m is going to get created within this variable called sir so serial dot serial dot serial this is and this is a method which is going to create you the serial port and as i said we need these these five attributes to be set so that the first attribute is port okay port and here we need to mention our com ports right com1 so common was our com port that we have tested and then border it border it we are going to set this as 9600 you can keep anything uh likewise then uh byte size what is going to be our bite size bite size is going to be you know eight uh uh eight bits okay next is timeout we need to mention uh time out to come out of the read loop because uh many time it happens if the data doesn't come on the serial port how long does a serial port should wait to read that data and that's nothing but the timeout value that is two seconds it is going to wait for two seconds next is stop bit we need to mention our stop bit and stop it value is also provided by this uh by this library okay the stop bit is going to be a one right so first thing we need to do is let's create a loop while now we are going to create right infrared loop but we need to make sure that we should come out of this loop as well and to come out of this loop we are going to use keyboard uh key press uh as as a note notification for our program to come out of that loop okay and for that we must be using in uh keyboard library but do we have uh keyboard library installed no we don't have because otherwise it could have directly shown us the value so for that we need to install that as well so here i come and i'm going to install let's now i need to go on to terminal let's go on to terminal and on terminal i am going to peep install keyboard so we'll install okay peep install keyboard let's install our keyboard as well keyboard library we are going to need that right perfect so keyboard library is installed so i am good to go to use that library and uh how i i'm going to do is if keyboard you know if keyboard is pressed is pressed so here i have this method and in this method i'm going to check whether whether queue queue as a key is pressed or not okay let's give some message to message to user okay print some some message so user needs to quit the application okay we can say say like this so you use only to come out of this loop so how to come out of this loop is by breaking this loop break perfect so this way we have already you know uh taken care how to come out of this infinite loop perfect now very first thing that we need to do is we need to close the serial object because what will happen is if we okay if we keep this open and if you come back here and we want to use this program again and if it doesn't find the resource which means it is not a in the previous session it wasn't close properly so it is always good practice to close any resources that you are using so here we have created our serial port and here i'm going to release that serial port perfect so in here we can start using our serial port so first thing what i'm going to do is i'm going to write something on serial port and as our rxt experience are shorted let's see if it comes back perfect so here i am going to write something on serial port serial dot right is the function and right is the function and within right i am going to press this a now a is a uh string value that's what the the notation is in the it is in the python but serial port expect it to be a byte by byte value so to do that we have to encode that value so encode as you can keep this as encode empty or otherwise normal practice is this ascii to mention it is an ascii now what we are going to do is as i'm going to transmit this and rxt explains a shorted so this a is going to get reversed so i need to read that value so so i'm going to receive that value in this function and this variable with this function that is read so whatever uh a as a character is going to get transmitted it gets received as well and this read function is the function which is going to read only one character at a time okay this is not going to read complete line so we have read line function to read complete line perfect so what we can do is now what happened now let's print that as well first now in print what we we can print is this this receive value right receive value but receive is in is in the encoded format of the ascii so we need to decode it to display it on on python you know console and how to decode it again decode function with same ascii value perfect so with this we have transmitted one value and uh we are going to get that receive receive that value and we have displayed that value now this is going to happen infinite time right but it will it will happen very very fast so we need to give some delay as well okay time dot sleep we have this function uh okay so within that function we need to mention what how much delay do we want but we also need to import time library okay perfect so we have this time uh already uh so imported so time dot time dot sleep with this what is happening at every second it's going to transmit that value will receive that value and as we press keep on pressing q whenever this function executes if it and if it checks q is pressed it is going to come out of that loop so let's check this out and uh shortcut is shift f10 perfect so we have transmitted as a value and i have received that a value perfect and it is going to display and here here we have this one second delay so you can see that with one second it is executing so we need to come out of this loop so i am going to press this q button now and we need to keep keep pressing that q button right so keep pressing that q button until this function executes so uh yeah so we we press this queue user needs to quit this application so it has come out of this loop perfect so we have successfully tested our um one character at a time right so now let's let's see how can we read complete line so let's put some message over here this is the okay this is the message now what we'll do we we have written complete message but i i'm going to read only one value let's let's see how how this is functioning now let's shift f10 now what happened right it is receiving one byte at a time it is receiving one byte at a time perfect so so to read complete line we have this function called line okay read line read line so let's keep this as it is here let's and let's come let's comment it out and now it is going to read complete line right it has read complete line this is the message but what is happening that it is waiting for one second and then again it transmits the data but but it is again going to wait for another two seconds why because we have mentioned our time out to be two seconds here but what happened because this read function actually expects you know there is there's some extra character as carrier written and uh line feed values so carrier written and line feed values can be written as control r control slash n right this is line feed and uh carry this is carrier return and line feed so with this if i let's let's check this out you can see the difference i'm going to comment this out and i am not going to give any a carry written line feed let's comment this out as well so that so that we could notice that though we don't we have not given any delay we are not given any delay even then there is a delay okay and that delay is nothing but time out delay so let's run this out okay stop that and run perfect so here we have transmitted the data uh it is waiting for a line to complete and how does it know line is completed with this with these two characters but there is no two characters over here which means which means it need to time out and then come out of that read function so what i'm doing now i'm going to give a read line and carry a return and new line message and then we need to give this as well right let's quit out of this okay user pressed quick quit so let's run this shift f10 now you would notice that is it is repeating every one second every one second because carry rate and line feed is successfully read over here so it is not going to wait for a timeout okay it is not going to wait for a timeout perfect awesome so with this we have tested we have successfully tested our serial port with python with the help of this usb to serial usb to ttl board perfect that is it in this video you have link in the description box to download this code and type in the comment box what did you learn out of this and what did you feel about this video so see you in the next video bye
Info
Channel: Amit Dhanawade
Views: 41,741
Rating: undefined out of 5
Keywords: pyserial tutorial, pyserial read data from serial port, python for beginners, serial communication, serial port, pyserial projects, python, reading serial data python, pyserial, using pyserial with python 3, using pyserial on windows, python tutorial, serial communication protocols, python read serial data from arduino
Id: VQCiEQockwE
Channel Id: undefined
Length: 16min 12sec (972 seconds)
Published: Sat Jan 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.