Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/11/2010, 15:47
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Comunicacion Serial Inquietudes

Código Python:
Ver original
  1. #Boa:Frame:Frame1
  2.  
  3. import wx
  4. import serial
  5.  
  6. ##PARA COM1 Puerto = 0
  7. Puerto = 0
  8. ser = serial.Serial(Puerto, 9600, timeout=1, stopbits=1)
  9.  
  10. def create(parent):
  11.     return Frame1(parent)
  12.  
  13. [wxID_FRAME1, wxID_FRAME1BOTAPAGAR, wxID_FRAME1BOTLED1, wxID_FRAME1BOTLED2,
  14.  wxID_FRAME1PANEL1,
  15. ] = [wx.NewId() for _init_ctrls in range(5)]
  16.  
  17. class Frame1(wx.Frame):
  18.     def _init_ctrls(self, prnt):
  19.         # generated method, don't edit
  20.         wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
  21.               pos=wx.Point(506, 173), size=wx.Size(214, 239),
  22.               style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
  23.         self.SetClientSize(wx.Size(206, 205))
  24.  
  25.         self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
  26.               pos=wx.Point(0, 0), size=wx.Size(206, 205),
  27.               style=wx.TAB_TRAVERSAL)
  28.         self.panel1.SetBackgroundColour(wx.Colour(0, 196, 196))
  29.  
  30.         self.botLed1 = wx.Button(id=wxID_FRAME1BOTLED1, label=u'Led1 On',
  31.               name=u'botLed1', parent=self.panel1, pos=wx.Point(64, 24),
  32.               size=wx.Size(75, 23), style=0)
  33.         self.botLed1.Bind(wx.EVT_BUTTON, self.OnBotLed1Button,
  34.               id=wxID_FRAME1BOTLED1)
  35.  
  36.         self.botLed2 = wx.Button(id=wxID_FRAME1BOTLED2, label=u'Led2 On',
  37.               name=u'botLed2', parent=self.panel1, pos=wx.Point(64, 64),
  38.               size=wx.Size(75, 23), style=0)
  39.         self.botLed2.Bind(wx.EVT_BUTTON, self.OnBotLed2Button,
  40.               id=wxID_FRAME1BOTLED2)
  41.  
  42.         self.botApagar = wx.Button(id=wxID_FRAME1BOTAPAGAR,
  43.               label=u'Apagar Leds', name=u'botApagar', parent=self.panel1,
  44.               pos=wx.Point(64, 104), size=wx.Size(75, 23), style=0)
  45.         self.botApagar.Bind(wx.EVT_BUTTON, self.OnBotApagarButton,
  46.               id=wxID_FRAME1BOTAPAGAR)
  47.        
  48.         self.txt = wx.TextCtrl(id=-1, parent=self.panel1, pos=wx.Point(64, 140))
  49.         self.btn = wx.Button(id=-1, label="Enviar", parent=self.panel1, pos=wx.Point(64, 165))
  50.         self.btn.Bind(wx.EVT_BUTTON, self.OnSend)
  51.  
  52.     def __init__(self, parent):
  53.         self._init_ctrls(parent)
  54.        
  55.  
  56.     def OnBotLed1Button(self, event):
  57.         event.Skip()
  58.         ser.write("A")  #ENVIA LA LETRA "A" AL MICROCONTROLADOR
  59.        
  60.  
  61.     def OnBotLed2Button(self, event):
  62.         event.Skip()
  63.         ser.write("B")  #ENVIA LA LETRA "B" AL MICROCONTROLADOR
  64.  
  65.     def OnBotApagarButton(self, event):
  66.         event.Skip()
  67.         ser.write("X")  #ENVIA LA LETRA "X" AL MICROCONTROLADOR
  68.    
  69.     def OnSend(self, event):
  70.         event.Skip()
  71.         txt = self.txt.GetValue()
  72.         md = wx.MessageDialog(self, txt, "Titulo", wx.OK)
  73.         md.ShowModal()
  74.         md.Destroy()
  75.         ser.write(txt)
  76.  
  77. class BoaApp(wx.App):
  78.     def OnInit(self):
  79.         self.main = create(None)
  80.         self.main.Show()
  81.         self.SetTopWindow(self.main)
  82.         return True
  83.  
  84. def main():
  85.     application = BoaApp(0)
  86.     application.MainLoop()
  87.  
  88. if __name__ == '__main__':
  89.     main()