I want to use ser(Serial communication) which is define in Testing.py file. The code for the same is mentioned below. Whaatever you mentioned above i tired but getting error for that also. Posting my code below
Testing.py
from tkinter import *
import tkMessageBox
import Tkinter
from PIL import Image, ImageTk
import tkinter as Tkinter
import tkinter.messagebox as box
from tkMessageBox import *
from random import randint
import serial
from serial import tools
from serial.tools import list_ports
import serial.tools.list_ports
import binascii
import sys, string, os,subprocess
import time
import tkFileDialog
import glob
from tkFileDialog import askopenfilename
from tkFileDialog import *
import PIL
from ResultAuto_ITOM import AutomationTestingPage_ITOM
from ResultAuto_ITOM import ResultPage_Auto_ITOM
ser = ''
class MenuBar(Tkinter.Menu):
def __init__(self, parent):
Tkinter.Menu.__init__(self, parent)
self.text = Text()
self.text.pack()
fileMenu = Tkinter.Menu(self, tearoff=False)
self.add_cascade(label="File", underline=0, menu=fileMenu)
fileMenu.add_command(label="Open Config File", underline=1, command=self.OpenFile)
fileMenu.add_command(label="Save", underline=1, command=self.file_save)
fileMenu.add_command(label="Exit", underline=1, command=self.quit)
editMenu = Tkinter.Menu(self, tearoff=False)
self.add_cascade(label="Edit", underline=0, menu=editMenu)
editMenu.add_command(label="Undo Ctrl+z ", underline=1)
editMenu.add_command(label="Redo Ctrl+Shift+z ", underline=1)
editMenu.add_command(label="Cut Ctrl+x ", underline=1)
editMenu.add_command(label="Copy Ctrl+c ", underline=1)
editMenu.add_command(label="Paste Ctrl+v ", underline=1)
settingMenu = Tkinter.Menu(self, tearoff=False)
self.add_cascade(label="Setting", underline=0, menu=settingMenu)
submenu1 = Tkinter.Menu(settingMenu)
submenu1.add_command(label="Start Communication", underline=1,command=self.Start_Communication)
submenu1.add_command(label="Stop Communication", underline=1,command=self.Stop_Communication)
settingMenu.add_cascade(label="System Settings",menu=submenu1, underline=0)
helpMenu = Tkinter.Menu(self, tearoff=False)
self.add_cascade(label="Help", underline=0, menu=helpMenu)
#helpMenu.add_command(label="Help", underline=1)
helpMenu.add_command(label="About", underline=1, command=self.About)
def Start_Communication(self):
#start = TestingPage(self)
#start.clicked()
#TestingPage.clicked
pass
def Stop_Communication(self):
#TestingPage.disconnect
pass
def quit(self):
sys.exit(0)
def OpenFile(self):
#name = askopenfilename()
file = tkFileDialog.askopenfilename()
os.system(file)
def file_save(self):
f = asksaveasfile(mode='w', defaultextension=".txt")
text2save = str(self.text.get(0.0, END))
#f.write(text2save)
#f.close()
def About(self):
box.showinfo('About', 'Version 1.1 \n'
'An Application to test the product using Serial Communication Protocol')
## Its the Testing page which contains six function such as init, thirdpage,automation, manual, resultauto and previousprg ##
## a. The init function where the child frame is initialized ##
## b. Third page function contains all the button and text button which appear in Testing page ##
## c. Automation function used to call the Automation testing page which contains the test related to automation ##
## d. Manual function used to call the Manual testing page which includes the test related to manual ##
## e. Resultauto function used to call ResultAUto page and hide the TestingPage ##
## f. Previousprg function used to call the ProgrammingPage window and hide the TestingPage ##
class TestingPage(Tkinter.Tk):
def __init__(self, child):
Tkinter.Tk.__init__(self, child)
self.child = child
menubar = MenuBar(self)
self.config(menu=menubar)
self.thirdpage()
def thirdpage(self):
self.grid()
self.title(
" TEST BENCH ")
self.geometry("1280x800")
self.labelframe = Tkinter.LabelFrame(self)
self.labelframe.pack(fill="both", padx=60, pady=40, expand="yes")
self.lbl = Tkinter.Label(self.labelframe, text="TESTING", width=28, font=("Arial Bold", 18))
self.lbl.place(x=320, y=10)
self.labelframe1 = Tkinter.LabelFrame(self.labelframe)
self.labelframe1.place(x=50, y=60, height=570, width=1060)
self.lbl5 = Tkinter.Label(self.labelframe1, text="SERIAL COMMUNICATION :", width=22, font=("Arial Bold", 12))
self.lbl5.place(x=35, y=70)
self.lbl6 = Tkinter.Label(self.labelframe1, text="COM PORT", width=10, font=("Arial Bold", 11))
self.lbl6.place(x=35, y=150)
ports = self.serial_ports()
self.dropVar = StringVar(self.labelframe1)
self.dropVar.set("")
self.dropMenu = OptionMenu(self.labelframe1, self.dropVar, *ports, command=self.func)
self.dropMenu.pack(padx=8, pady=8, side=LEFT)
self.dropMenu.configure(width=8, font=("Arial Bold", 12))
self.dropMenu.place(x=160, y=145)
self.lbl7 = Tkinter.Label(self.labelframe1, text="BAUD RATE", width=10, font=("Arial Bold", 11))
self.lbl7.place(x=310, y=150)
self.dropVar1 = StringVar(self.labelframe1)
self.dropVar1.set("")
self.dropMenu1 = OptionMenu(self.labelframe1, self.dropVar1, 4800, 9600, 19200, 38400, 115200,command=self.func1)
self.dropMenu1.pack(padx=8, pady=8, side=LEFT)
self.dropMenu1.configure(width=8, font=("Arial Bold", 12))
self.dropMenu1.place(x=435, y=145)
self.btn8 = Tkinter.Button(self.labelframe1, text="CONNECT", width=10, font=("Arial Bold", 11),
command=self.clicked)
self.btn8.place(x=590, y=145)
self.btn9 = Tkinter.Button(self.labelframe1, text="DISCONNECT", width=12, font=("Arial Bold", 11),
command=self.disconnect)
self.btn9.place(x=730, y=145)
self.lbl3 = Tkinter.Label(self.labelframe1, text="PRODUCT SELECTION :", width=20, font=("Arial Bold", 12))
self.lbl3.place(x=30, y=230)
product_selection = ['ITOM', 'SMARTSTAT_EXTENSION']
self.variable = StringVar(self.labelframe1)
self.w = OptionMenu(self.labelframe1, self.variable, *product_selection,command=self.product_sel)
self.w.pack(padx=8, pady=8, side=LEFT)
self.w.configure(width=22, font=("Arial Bold", 11))
self.w.place(x=35, y=290)
self.variable.set("")
self.btn6 = Tkinter.Button(self.labelframe, text="PREVIOUS", width=12, font=("Arial Bold", 14),
command=self.previousprg)
self.btn6.place(x=750, y=650)
self.btn7 = Tkinter.Button(self.labelframe, text="NEXT", width=12, font=("Arial Bold", 14),
command=self.resultauto)
self.btn7.place(x=950, y=650)
def serial_ports(self):
ports = ['COM%s' % (i + 1) for i in range(256)]
result = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
result.append(port)
except (OSError, serial.SerialException):
pass
return result
def clicked(self):
global ser
COM_Port = self.port
Baud_Rate = self.baud
self.btn8.config(state="disable")
self.btn9.config(state="active")
ser = serial.Serial(COM_Port, Baud_Rate)
ser.isOpen()
#return ser
def product_sel(self, value):
self.product = value
print self.product
def func(self, value):
self.port = value
def func1(self, value):
self.baud = value
def disconnect(self):
self.btn8.config(state="active")
self.btn9.config(state="disable")
ser.close()
def product_sel(self, value):
self.product = value
if self.product == "ITOM":
self.lbl4 = Tkinter.Label(self.labelframe1, text="TESTING :", width=8, font=("Arial Bold", 12))
self.lbl4.place(x=38, y=370)
self.CheckVar1 = IntVar()
self.C1 = Tkinter.Checkbutton(self.labelframe1, text="Automation Testing", variable=self.CheckVar1,onvalue=1, offvalue=1, height=5, width=20, command=self.auto_itom)
self.C1.pack(padx=8, pady=8, side=LEFT)
self.C1.configure(width=20, font=("Arial Bold", 10))
self.C1.place(x=60, y=400)
self.C2 = Tkinter.Checkbutton(self.labelframe1, text="Manual testing", onvalue=0,variable=self.CheckVar1,state="disable", offvalue=1, height=5, width=20)
self.C2.pack(padx=8, pady=8, side=LEFT)
self.C2.configure(width=18, font=("Arial Bold", 10))
self.C2.place(x=53, y=450)
elif self.product == "SMARTSTAT_EXTENSION":
self.lbl4 = Tkinter.Label(self.labelframe1, text="TESTING :", width=8, font=("Arial Bold", 12))
self.lbl4.place(x=38, y=370)
self.CheckVar1 = IntVar()
self.C1 = Tkinter.Checkbutton(self.labelframe1, text="Automation Testing", variable=self.CheckVar1,onvalue=1,offvalue=1, height=5, width=20, command=self.auto_smart)
self.C2 = Tkinter.Checkbutton(self.labelframe1, text="Manual testing", onvalue=0, variable=self.CheckVar1,offvalue=0, height=5, width=20,command=self.manual_smart)
self.C1.pack(padx=8, pady=8, side=LEFT)
self.C1.configure(width=20, font=("Arial Bold", 10))
self.C1.place(x=60, y=400)
self.C2.pack(padx=8, pady=8, side=LEFT)
self.C2.configure(width=18, font=("Arial Bold", 10))
self.C2.place(x=53, y=450)
else:
pass
def auto_itom(self):
AutomationTestingPage_ITOM(self)
def auto_smart(self):
AutomationTestingPage_SMART(self)
def manual_smart(self):
ManualTestingPage_SMART(self)
def resultauto(self):
ResultPage_Auto_ITOM(self)
self.withdraw()
def previousprg(self):
ProgrammingPage(self)
self.withdraw()
I'm using the serial port in AutomationTesting.py file. The code for same is also mentioned below
AutomationTesting.py
from tkinter import *
import tkMessageBox
import Tkinter
from PIL import Image, ImageTk
import tkinter as Tkinter
import tkinter.messagebox as box
from tkMessageBox import *
from random import randint
import serial
from serial import tools
from serial.tools import list_ports
import serial.tools.list_ports
import binascii
import sys, string, os,subprocess
import time
import tkFileDialog
import glob
from tkFileDialog import askopenfilename
from tkFileDialog import *
import PIL
import Testing
#from Testing import TestingPage
#from Testing import ser
#import Testing
#from Testing import *
output = ''
serial_no = ''
## Its the Automation Testing Page which contains five function such as init, fourthpage, data, clicked, and out function ##
## a. The init function where the child frame is initialized ##
## b. Fourth page function contains all the button and text button which appear in Automation testing page ##
## c. Data function where the packet structure is defined for particular button ##
## d. Clicked function which call the data function and pass the result to out fucntion ##
## e. Out function where all the output value is received from the clicked function and stored in doc format ##
class AutomationTestingPage_ITOM(Tkinter.Frame):
def __init__(self, child):
Tkinter.Frame.__init__(self, child)
autotest = Testing.TestingPage()
value = autotest.value
self.child = child
self.fourthpage()
def fourthpage(self):
self.grid()
self.fourthpage = Tkinter.Toplevel(self)
self.fourthpage.wm_geometry("1280x800")
self.fourthpage.title(
" TEST BENCH ")
self.lbl = Tkinter.Label(self.fourthpage, text="AUTOMATION TESTING - ITOM", width=28, font=("Arial Bold", 18))
self.lbl.place(x=450, y=30)
self.labelframe1 = Tkinter.LabelFrame(self.fourthpage)
self.labelframe1.pack(fill="both", padx=90, pady=90, expand="yes")
self.label = Tkinter.Label(self.labelframe1, text="SERIAL NUMBER", width=15, font=("Arial Bold", 12))
self.label.place(x=40, y=30)
sv = StringVar(self.labelframe1)
sv.trace("w", lambda name, index, mode, sv=sv: self.part(sv))
self.txt = Tkinter.Entry(self.labelframe1, textvariable=sv, width=22, font=("Arial Bold", 14))
self.txt.place(x=240, y=25)
self.CheckVar1 = IntVar()
self.C1 = Tkinter.Checkbutton(self.labelframe1, text="DAUGTHER BOARD DISPLAY TEST", variable=self.CheckVar1,
onvalue=1, offvalue=0, height=5, width=35, command=self.clicked)
self.C1.pack(padx=10, pady=10, side=LEFT)
self.C1.configure(width=30, font=("Arial Bold", 13))
self.C1.place(x=140, y=60)
self.txt1 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt1.place(x=530, y=95)
self.btn6 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn6.place(x=780, y=92)
self.CheckVar2 = IntVar()
self.C2 = Tkinter.Checkbutton(self.labelframe1, text="SPI FLASH TEST", variable=self.CheckVar2,
onvalue=1, offvalue=0, height=5, width=20)
self.C2.pack(padx=10, pady=10, side=LEFT)
self.C2.configure(width=20, font=("Arial Bold", 13))
self.C2.place(x=115, y=130)
self.txt2 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt2.place(x=530, y=163)
self.btn7 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn7.place(x=780, y=162)
self.CheckVar3 = IntVar()
self.C3 = Tkinter.Checkbutton(self.labelframe1, text="ITOM-RS485 TEST", variable=self.CheckVar3,
onvalue=1, offvalue=0, height=5, width=20)
self.C3.pack(padx=10, pady=10, side=LEFT)
self.C3.configure(width=20, font=("Arial Bold", 13))
self.C3.place(x=122, y=200)
self.txt3 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt3.place(x=530, y=233)
self.btn8 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn8.place(x=780, y=230)
self.CheckVar4 = IntVar()
self.C4 = Tkinter.Checkbutton(self.labelframe1, text="BLUETOOTH TEST", variable=self.CheckVar4,
onvalue=1, offvalue=0, height=5, width=20)
self.C4.pack(padx=10, pady=10, side=LEFT)
self.C4.configure(width=20, font=("Arial Bold", 13))
self.C4.place(x=124, y=270)
self.txt4 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt4.place(x=530, y=301)
self.btn9 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn9.place(x=780, y=298)
self.CheckVar5 = IntVar()
self.C5 = Tkinter.Checkbutton(self.labelframe1, text="SENSOR TEST", variable=self.CheckVar5,
onvalue=1, offvalue=0, height=5, width=20)
self.C5.pack(padx=10, pady=10, side=LEFT)
self.C5.configure(width=15, font=("Arial Bold", 13))
self.C5.place(x=132, y=340)
self.txt5 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt5.place(x=530, y=369)
self.btn10 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn10.place(x=780, y=368)
self.CheckVar6 = IntVar()
self.C6 = Tkinter.Checkbutton(self.labelframe1, text="LED's TEST", variable=self.CheckVar6,
onvalue=1, offvalue=0, height=5, width=20)
self.C6.pack(padx=10, pady=10, side=LEFT)
self.C6.configure(width=10, font=("Arial Bold", 13))
self.C6.place(x=145, y=410)
self.txt6 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt6.place(x=530, y=437)
self.btn11 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn11.place(x=780, y=438)
self.CheckVar7 = IntVar()
self.C7 = Tkinter.Checkbutton(self.labelframe1, text="RS-485 TEST", variable=self.CheckVar7,
onvalue=1, offvalue=0, height=5, width=20)
self.C7.pack(padx=10, pady=10, side=LEFT)
self.C7.configure(width=10, font=("Arial Bold", 13))
self.C7.place(x=145, y=480)
self.txt7 = Tkinter.Entry(self.labelframe1, width=15, font=("Arial Bold", 16))
self.txt7.place(x=532, y=507)
self.btn12 = Tkinter.Button(self.labelframe1, text="PASS", width=8, font=("Arial Bold", 13))
self.btn12.place(x=780, y=508)
def part(self, sv):
global part
part = sv
return part.get()
def data_tx(self):
element = [0x4D, 0x08, 0xFF, 0x06, 0x1A, 0x52]
data = bytearray(element)
ser.write(data)
receivedata = (ser.read(7))
data1 = binascii.b2a_hex(receivedata)
data2 = ord(receivedata[6])
return data2
def clicked(self):
global data2
self.txt1.insert(END, self.data_tx())
self.txt1.delete(2, END)
text1 = self.data_tx()
if text1 == 18:
self.btn6.configure(bg="green", fg="black", text="PASS", command=self.out)
return "TEST PASS"
else:
self.btn6.configure(bg="red", fg="black", text="FAIL", command=self.out)
return "TEST FAIL"
def out(self):
global output,serial_no
output = self.clicked()
serial_no = part.get()
file = open('Automation_Test_Report.doc', 'w')
file.write(" TEST RESULT - AUTOMATION ITOM \n")
file.write(" ------------------------------- \n")
file.write("\n")
file.write("\n")
file.write("TEST SUMMARY:\n")
file.write("\n")
file.seek(0, 1)
file.write("SERIAL NO:")
file.seek(3, 1)
file.write(serial_no)
file.write("\n")
file.write("\n")
file.write("********************************************************************************\n")
file.write(" TEST NUMBER TEST NAME TEST RESULT \n")
file.write("********************************************************************************\n")
file.write("\n")
file.seek(7, 1)
file.write("1")
file.seek(20, 1)
file.write("DAUGTHER BOARD DISPLAY TEST")
file.seek(10, 1)
file.write(output)
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("\n")
file.write("********************************************************************************\n")
file.close()
The error which i recieved is
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\Testing.py", line 245, in auto_itom
AutomationTestingPage_ITOM(self)
File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\ResultAuto_ITOM.py", line 44, in __init__
autotest = Testing.TestingPage()
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\ResultAuto_ITOM.py", line 178, in clicked
self.txt1.insert(END, self.data_tx())
File "D:\PyCharm\Main_Project_75F\Separate_GUI_Class\ResultAuto_ITOM.py", line 170, in data_tx
ser.write(data)
NameError: global name 'ser' is not defined