License GNU General Public License, version 3
Lines 158
Included in this Library
Permissions
Owner: wildintellect
Viewable by Everyone
Editable by All Siafoo Users
Hide
Writing an article is easy - try our reStructured Text demo Join Siafoo Now or Learn More

gui.py Atom Feed 0

# 's
  1#!/usr/bin/python
2# -*- coding: iso-8859-1 -*-
3#This version will only run on the Pocket PC with the ceserial ctypes wrapper for the serial port.
4#A desktop version should use pyserial
5#Alex Mandel tech@wildintellect.com
6#Under some type of Open Source license to be decided soon
7
8import Tkinter
9from Tkinter import *
10from ScrolledText import ScrolledText
11import sys, ctypes, winbase, ceserial
12import time
13from time import *
14#import Tix
15
16class simpleapp_tk(Tkinter.Tk):
17 def __init__(self,parent):
18 Tkinter.Tk.__init__(self,parent)
19 self.parent = parent
20 self.initialize()
21
22 def initialize(self):
23 #self.grid()
24
25 self.port = Tkinter.StringVar()
26 self.port.set("COM1:")
27 self.path = Tkinter.StringVar()
28 self.cntV= Tkinter.StringVar()
29 self.tag = Tkinter.IntVar()
30 self.tag.set(23)
31
32 self.label3=Tkinter.Label(self,text="Save Path")
33 self.label3.place(x =10,y=2,width=60,height=16)
34 self.textbox3=Tkinter.Entry(self,textvariable=self.path)
35 self.textbox3.place(x= 10,y= 20,width= 220,height= 20)
36 self.textbox3.insert(0,'\\CF Card\\rfid\\tmp1.txt')
37 self.label4=Tkinter.Label(self,text="Port")
38 self.label4.place(x =10,y=40,width=30,height=20)
39 #self.textbox4 = Tkinter.Entry(self,textvariable=self.port)
40 #self.textbox4.place(x =60,y=40,width=50,height=20)
41 #self.textbox4.insert(0,'COM1:')
42 self.dropdown4 = Tkinter.OptionMenu(self,self.port,"COM1:","COM7:","COM4:","COM5:")
43 self.dropdown4.place(x =40,y=40,width=70,height=20)
44 self.label5=Tkinter.Label(self,textvariable=self.cntV)
45 self.label5.place(x =105,y=40,width=20,height=20)
46 self.button1 = Tkinter.Button(self,text=u"Start",command=self.OnStart)
47 self.button1.place(x= 10,y= 60,width= 100,height= 20)
48 self.button2 = Tkinter.Button(self,text=u"Stop",command=self.OnStop)
49 self.button2.place(x= 130,y= 60,width= 100,height= 20)
50 self.review = ScrolledText(self)
51 self.review.place(x=10,y=80,height=180,width=220)
52 self.label6=Tkinter.Label(self,text="Len of TAG")
53 self.label6.place(x=130,y=40,width=70,height=20)
54 self.textbox6=Tkinter.Entry(self,textvariable=self.tag)
55 self.textbox6.place(x=200,y=40,width=30,height=20)
56 self.button7 = Tkinter.Button(self,text=u"1 Read",command=self.SingleRead)
57 self.button7.place(x=170,y=0,width=60,height=20)
58 self.button8 = Tkinter.Button(self,text=u"Raw",command=self.RawRead)
59 self.button8.place(x=100,y=0,width=50,height=20)
60 self.geometry("240x320+0+25")
61
62
63 #Keep track of the running loop
64 self.stop = False
65 #Keep track of the open port and file
66 self.flag = False
67
68 def OnStart(self):
69 if self.flag == False:
70 #s=ceserial.Serial(port="COM7:",baudrate=4800)
71 self.s=ceserial.Serial(port=self.port.get(),baudrate=4800,timeout=1)
72 self.s.open()
73 print "open"
74 #of=open('\\CF Card\\tmp1.txt',"w")
75 self.of=open(self.path.get(),"w")
76 self.cnt=0
77 self.c2=0
78 self.flag = True
79 else:
80 print "already open"
81
82 try:
83 if self.stop == False and (self.c2<100):
84 self.label5.configure(bg="green")
85 #Only read what's necessary, default is 23
86 #rv=self.s.read((self.tag.get())*2)
87 rv1 = self.s.readline()
88 rv = self.s.readline()
89 if (len(rv)>0):
90 stamp = strftime(",%d %b %Y %H:%M:%S",localtime())
91 outline1 = rv.replace("\r\n",stamp+'\n')
92 outline = rv.replace("\r\n","")
93 print "read"+str(len(rv))
94 #self.of.write(outline+'\n')
95 #self.review.insert(END,outline+'\n')
96 self.of.write(outline1)
97 self.review.insert(END,outline)
98 self.review.insert(END,stamp+'\n')
99 self.review.yview("moveto",1.0)
100 self.cnt+=1
101 #Time delay between reads in milli seconds
102 self.cntV.set(str(self.cnt))
103 self.after(900,self.OnStart)
104 elif self.stop == True:
105 self.of.close()
106 self.s.close()
107 self.flag = False
108 self.stop = False
109 self.label5.configure(bg="red")
110 print "stopped"
111
112 except Exception, e:
113 self.c2+=1
114 print "%d. exception:"%self.c2
115 print e
116 self.label5.configure(bg="yellow")
117 if (self.c2<100):
118 self.after(500,self.OnStart)
119
120 def OnStop(self):
121 self.stop = True
122 self.c2 = 0
123 print "stop"
124
125 def SingleRead(self):
126 #Read from the scanner once
127 self.s=ceserial.Serial(port=self.port.get(),baudrate=4800,timeout=1)
128 self.s.open()
129 #Test of controlled scan mode
130 #self.s.write("R")
131 #rv=self.s.read(50)
132 rv=self.s.readline()
133 self.review.delete(0.0,END)
134 rv=self.s.readline()
135 self.review.insert(END,rv)
136 self.s.close()
137
138 def RawRead(self):
139 #Read exactly what the scanner wants to give
140 self.s=ceserial.Serial(port=self.port.get(),baudrate=4800,timeout=1)
141 self.s.open()
142 #Test of controlled scan mode
143 #self.s.write("R")
144 rv=self.s.read(1024)
145 rvsplit = rv.splitlines()
146 self.of=open(self.path.get(),"w")
147
148 for x in rvsplit:
149 if len(x) >= 23:
150 cleanx = CleanRFID(x)
151 self.of.write(cleanx+'\n')
152 self.of.close()
153 self.s.close()
154
155 def CleanRFID(self,input):
156 #Tool for trying to clean non standard characters out of the input, not sure if its necessary
157 try:
158 output = input.encode('utf-8')
159 return output
160 except:
161 return ""
162
163if __name__ == "__main__":
164 app = simpleapp_tk(None)
165 app.title('Serial Logger')
166 app.mainloop()