Foros del Web » Programando para Internet » Python »

Comunicacion Serial Inquietudes

Estas en el tema de Comunicacion Serial Inquietudes en el foro de Python en Foros del Web. Hola amigos. Tengo dos inquietudes sobre transmision y recepcion de datos por el bus serial RS232. Utilizando Windows, Boa Constructor y la libreria pyserial hice ...
  #1 (permalink)  
Antiguo 08/11/2010, 09:32
 
Fecha de Ingreso: noviembre-2010
Mensajes: 1
Antigüedad: 13 años, 5 meses
Puntos: 0
Comunicacion Serial Inquietudes

Hola amigos. Tengo dos inquietudes sobre transmision y recepcion de datos por el bus serial RS232. Utilizando Windows, Boa Constructor y la libreria pyserial hice una pequeña aplicacion con tres botones. Con cada boton envio un caracter diferente a un microcontrolador conectado al puerto serial COM1. Este microcontrolador esta programado para responder al evento de llegada de datos por USART.

La primera inquietud es:
Si agrego una caja de texto, Cómo hago para transmitir por serial un caracter o un texto introducido en la caja de texto cada vez que presione un boton determinado ?

La segunda inquietud es:
Como hago para recibir datos por el puerto serial y el programa responda a ese evento ?

Estos son los archivos:



Código Python:
Ver original
  1. #!/usr/bin/env python
  2. #Boa:App:BoaApp
  3.  
  4. import wx
  5.  
  6. import FrameTxSerial1
  7.  
  8. modules ={u'FrameTxSerial1': [1, 'Main frame of Application', u'FrameTxSerial1.py']}
  9.  
  10. class BoaApp(wx.App):
  11.     def OnInit(self):
  12.         self.main = FrameTxSerial1.create(None)
  13.         self.main.Show()
  14.         self.SetTopWindow(self.main)
  15.         return True
  16.  
  17. def main():
  18.     application = BoaApp(0)
  19.     application.MainLoop()
  20.  
  21. if __name__ == '__main__':
  22.     main()


--------------------------------
Este es el otro:

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.     def __init__(self, parent):
  49.         self._init_ctrls(parent)
  50.        
  51.  
  52.     def OnBotLed1Button(self, event):
  53.         event.Skip()
  54.         ser.write("A")  #ENVIA LA LETRA "A" AL MICROCONTROLADOR
  55.        
  56.  
  57.     def OnBotLed2Button(self, event):
  58.         event.Skip()
  59.         ser.write("B")  #ENVIA LA LETRA "B" AL MICROCONTROLADOR
  60.  
  61.     def OnBotApagarButton(self, event):
  62.         event.Skip()
  63.         ser.write("X")  #ENVIA LA LETRA "X" AL MICROCONTROLADOR

GRACIAS POR SU COLABORACION.

Última edición por AlvaroG; 09/11/2010 a las 10:16 Razón: resaltado de código
  #2 (permalink)  
Antiguo 09/11/2010, 10:18
AlvaroG
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: Comunicacion Serial Inquietudes

Supongo que para enviar lo que esté en la caja de texto, habrás de crear un método que responda al evento click del botón y que haga ser.write(contenido de la caja de texto)

Para obtener el contenido de la caja de texto, busca en la API de wxPython (nunca usé wx, así que no te puedo indicar exactamente cómo hacerlo)


Saludos.
  #3 (permalink)  
Antiguo 09/11/2010, 15:47
Avatar de 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()

Etiquetas: comunicacion, serial
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:14.