- Type python on the command promt.
- Import library for serial communication by typing "import serial".
- To check the serial communication of the board just type "ser=serial.Serial("COM3")". Where COM3 defines the communication port, if it's communicating we will get a error message. In my case it was working fine.
- To check the output from the ir sensor type "ser.readline()"
- To get continuous output on the command promt, type the following command line. Python is case sensitive and very much sensitive to indentation. So, utmost care should be taking while writing the code. In the picture shown below space should be gives at the red marked area.
- Now it is seen that my board is communicating fine on python platform also. So, it's time to make GUI. To develop GUI, I wrote following code, with the help of one of my student (Animesh, ECE Department, Final Year).
import re
import serial
from Tkinter import Tk, Label, Button, Scale
class MyFirstGUI:
def __init__(self, master):
self.master = master
master.title("A simple GUI")
self.ser = serial.Serial('COM3')
self.distance = 0
self.label = Label(master, text="Sensor data GUI")
self.label.pack()
self.w = Scale(master, from_=0, to=50)
self.w.pack()
self.read_sensor()
self.update_gui()
def greet(self):
print("Greetings!")
def update_gui(self):
self.w.set(self.distance)
self.master.update()
def read_sensor(self):
raw_data = self.ser.readline().strip()
try:
distance = int(raw_data[:2])
except ValueError:
pass
else:
print("Distance: {}".format(distance))
self.distance = distance
def run(self):
self.read_sensor()
self.update_gui()
root = Tk()
my_gui = MyFirstGUI(root)
while True:
my_gui.read_sensor()
my_gui.update_gui()
- I have saved this code with the name GUI.py
Therefore, to run the GUI following command line need to be used.
- After using the above command line and pressing enter, a GUI will appear showing continuous variation of the values.