Source

radio-data / radio-data

Full commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Created on Wed Sep 13 09:29:42 2010

@author: alfonsodg
"""

import sys
import os
import Tkinter
import tkFileDialog
import tkMessageBox
try:
    from serial9k import Serial9k
except:
    print "Oops! No module available for your platform"



PORTS = ('COM1','COM2','COM3','COM4','COM5','COM6','COM7','COM8','COM9')
DATA_TYPES = ('TXT','IMG','VID')
BAUD_RATES = (1200,9600,115200)
FileN = None



def Write():
    comm = Serial9k(port=VarCOM.get(), baudrate=VarBAUD.get())
    if VarTYPE.get()=='TXT':
        if FileN:
            Content = open(FileN).read()
        else:
            Content = MessageT.get()
    else:
        Content = open(FileN, 'rb').read()
    comm.write(Content)
    comm.close()
    return


def Read():
    comm = Serial9k(port=VarCOM.get(), baudrate=VarBAUD.get())
    Content = comm.read()
    comm.close()
    #print Content
    if VarTYPE.get()=='TXT':
        open('demo.txt','w+').write(Content)
        MessageW(Content)
    elif VarTYPE.get()=='IMG':
        open('demo.jpg','wb+').write(Content)
    else:
        open('demo.avi','wb+').write(Content)
    return


def FileO():
    global FileN
    FileN = tkFileDialog.askopenfilename(title=u'Choose a file')
    FileLabel = Tkinter.Label(Ventana)
    FileLabel["text"] = u"Selected : %s"%(FileN)
    FileLabel.pack()


def MessageW(Content=u'Transmission Status'):
    tkMessageBox.showinfo(title=u'Done',message=Content)


def TransF():
    print Modo.get()
    print VarCOM.get()
    print VarBAUD.get()
    print FileN
    print VarTYPE.get()    
    if Modo.get()==0:
        Read()
    else:
        Write()
    MessageW()


Ventana = Tkinter.Tk()
Ventana.title(u"Radio Communications")
Ventana.geometry('400x400+200+200')
Ventana["padx"] = 40
Ventana["pady"] = 20
MessageT = Tkinter.IntVar()
Modo = Tkinter.IntVar()
Mode = Tkinter.Checkbutton(Ventana, text="Sender", variable=Modo)
Mode.pack()
Area=Tkinter.Frame(Ventana)
optionLabel = Tkinter.Label(Area)
optionLabel["text"] = u"Port:"
optionLabel.pack()
VarCOM = Tkinter.StringVar()
comboBox = apply(Tkinter.OptionMenu, (Area, VarCOM) + PORTS)
VarCOM.set("COM1")
comboBox["width"] = 15
comboBox.pack()
optionLabel = Tkinter.Label(Area)
optionLabel["text"] = u"Baud Rate:"
optionLabel.pack()
VarBAUD = Tkinter.StringVar()
comboBox = apply(Tkinter.OptionMenu, (Area, VarBAUD) + BAUD_RATES)
VarBAUD.set("9600")
comboBox["width"] = 15
comboBox.pack()
optionLabel = Tkinter.Label(Area)
optionLabel["text"] = u"Type:"
optionLabel.pack()
VarTYPE = Tkinter.StringVar()
comboBox = apply(Tkinter.OptionMenu, (Area, VarTYPE) + DATA_TYPES)
VarTYPE.set("TXT")
comboBox["width"] = 15
comboBox.pack()
Area.pack()
optionLabel = Tkinter.Label(Ventana, text=u"______")
optionLabel.pack()
optionLabel = Tkinter.Label(Ventana, text=u"Message")
optionLabel.pack()
MessageT = Tkinter.Entry(Ventana)
MessageT.pack()
FileB = Tkinter.Button(Ventana, text=u"File", command=FileO)
FileB.pack()
GoB = Tkinter.Button(Ventana, text=u"Go", command=TransF)
GoB.pack()
Ventana.mainloop()