elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Puerto serie RS232
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Puerto serie RS232  (Leído 2,608 veces)
Meta


Desconectado Desconectado

Mensajes: 3.441



Ver Perfil WWW
Puerto serie RS232
« en: 23 Febrero 2009, 21:39 pm »

Hola:

Estoy con MonoDevelop con el openSUSE 11.1. Mirando esta web http://www.mono-project.com/HowToSystemIOPorts quiero configurar el puerto serie como a mi me de la gana.

Lo he puesto así:

Código
  1. mySerial new SerialPort("/dev/ttyS0", 9600, None, 8, Two);

La otra manera con comillas así:

Código
  1. mySerial new SerialPort("/dev/ttyS0", 9600, "None", 8, "Two");

Código completo:

Código
  1. // MainWindow.cs created with MonoDevelop
  2. // User: metaconta at 17:46 23/02/2009
  3. //
  4. // To change standard headers go to Edit->Preferences->Coding->Standard Headers
  5. //
  6. using System;
  7. using Gtk;
  8. using System.IO.Ports;
  9.  
  10. public partial class MainWindow: Gtk.Window
  11. {
  12. public MainWindow (): base (Gtk.WindowType.Toplevel)
  13. {
  14. Build ();
  15. }
  16.  
  17. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  18. {
  19. Application.Quit ();
  20. a.RetVal = true;
  21. }
  22.  
  23. protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
  24. {
  25. mySerial new SerialPort("/dev/ttyS0", 9600, None, 8, Two);
  26. }
  27. }

Me da error en la línea 25. ¿Qué puede ser?

Estoy intentando pasar este código de Visual C# a Mono, puedes ver el código en las páginas 153 y 154 en este manual en PDF.

http://www.abcdatos.com/tutoriales/tutorial/z9521.html

Saludos.

EDITO:
He logrado abrir el puerto pero no he sido enviar el código al puerto serie.
Código
  1. // MainWindow.cs created with MonoDevelop
  2. // User: metaconta at 17:46 23/02/2009
  3. //
  4. // To change standard headers go to Edit->Preferences->Coding->Standard Headers
  5. //
  6. using System;
  7. using Gtk;
  8. using System.IO.Ports;
  9.  
  10. public partial class MainWindow: Gtk.Window
  11. {
  12. private SerialPort mySerial;
  13.  
  14. public MainWindow (): base (Gtk.WindowType.Toplevel)
  15. {
  16. Build ();
  17. }
  18.  
  19. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  20. {
  21. Application.Quit ();
  22. a.RetVal = true;
  23. }
  24.  
  25. protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
  26. {
  27. mySerial = new SerialPort("/dev/ttyS0", 9600, Parity.None, 8, StopBits.Two);
  28.  
  29. if(mySerial == null) return;
  30.  
  31. mySerial.Open();
  32.  
  33. if(!mySerial.IsOpen)
  34. {
  35. label1.Text = "Error abriendo";
  36. mySerial.Dispose();
  37. return;
  38. }
  39.  
  40. byte[] buffer = new byte[1];
  41. buffer[0] = 0x74;
  42. mySerial.Write(buffer, 0, 1);
  43.  
  44. try
  45. {
  46. mySerial.Write(buffer, 0, 1);
  47. }
  48. catch(Exception ex)
  49. {
  50. //label1.Text = ex.Message;// + " " + ex.StackTrace;
  51. mySerial.Close();
  52. mySerial.Dispose();
  53. return;
  54. }
  55.  
  56. mySerial.Close();
  57. }
  58. }

Un cordial saludo.


« Última modificación: 23 Febrero 2009, 22:35 pm por Meta » En línea

Meta


Desconectado Desconectado

Mensajes: 3.441



Ver Perfil WWW
Re: Puerto serie RS232
« Respuesta #1 en: 23 Febrero 2009, 23:33 pm »

Vamos a empezar desde el principio.

Utilizando el Mono con gtk#, he introducido un botón normal en el formulario. El código completo lo he hecho de nuevo y ahora es así. Ejecuta sin problemas pero al pulsar el botón hay errores que no se el motivo.

Código
  1. // MainWindow.cs created with MonoDevelop
  2. // User: metaconta at 20:59 23/02/2009
  3. //
  4. // To change standard headers go to Edit->Preferences->Coding->Standard Headers
  5. //
  6. using System;
  7. using Gtk;
  8. using System.IO.Ports;
  9.  
  10. public partial class MainWindow: Gtk.Window
  11. {
  12. public MainWindow (): base (Gtk.WindowType.Toplevel)
  13. {
  14. Build ();
  15. }
  16.  
  17. protected void OnDeleteEvent (object sender, DeleteEventArgs a)
  18. {
  19. Application.Quit ();
  20. a.RetVal = true;
  21. }
  22.  
  23. public SerialPort mySerial;
  24.  
  25. protected virtual void OnButtonTClicked (object sender, System.EventArgs e)
  26. {
  27. mySerial = new SerialPort("/dev/ttyS0", 9600, Parity.None, 8, StopBits.Two);
  28. mySerial.Open();
  29. byte[] mBuffer = new byte[1];
  30. mBuffer[0] = 0x74; // LA letra t.
  31. mySerial.Write(mBuffer, 0, mBuffer.Length);
  32. mySerial.Close();
  33. }
  34. }
  35.  

En el mensaje de error me da esto:
Código
  1.  
  2. Marshaling clicked signal
  3. Exception in Gtk# callback delegate
  4.  Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
  5. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.IOException: Permiso denegado
  6.  at System.IO.Ports.SerialPortStream.ThrowIOException () [0x00012] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/System/System.IO.Ports/SerialPortStream.cs:299
  7.  at System.IO.Ports.SerialPortStream..ctor (System.String portName, Int32 baudRate, Int32 dataBits, Parity parity, StopBits stopBits, Boolean dtrEnable, Boolean rtsEnable, Handshake handshake, Int32 readTimeout, Int32 writeTimeout, Int32 readBufferSize, Int32 writeBufferSize) [0x0001e] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/System/System.IO.Ports/SerialPortStream.cs:36
  8.  at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
  9.  at System.IO.Ports.SerialPort.Open () [0x00078] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/System/System.IO.Ports/SerialPort.cs:563
  10.  at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()
  11.  at MainWindow.OnButtonTClicked (System.Object sender, System.EventArgs e) [0x00018] in /home/metaconta/Prueba_02/Prueba_02/MainWindow.cs:28
  12.  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
  13.  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00057] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System.Reflection/MonoMethod.cs:157
  14.  --- End of inner exception stack trace ---
  15.  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00071] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System.Reflection/MonoMethod.cs:167
  16.  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System.Reflection/MethodBase.cs:110
  17.  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000b4] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System/Delegate.cs:422
  18.  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System/MulticastDelegate.cs:71
  19.  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System/Delegate.cs:394
  20.  at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x0004f] in /usr/src/packages/BUILD/gtk-sharp-2.12.6/glib/Signal.cs:195
  21.  at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x0000c] in /usr/src/packages/BUILD/gtk-sharp-2.12.6/glib/SignalClosure.cs:118
  22.  at GLib.SignalClosure.MarshalCallback (IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) [0x0007a] in /usr/src/packages/BUILD/gtk-sharp-2.12.6/glib/SignalClosure.cs:146
  23.   at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e, Boolean is_terminal) in /usr/src/packages/BUILD/gtk-sharp-2.12.6/glib/ExceptionManager.cs:line 58
  24.   at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) in /usr/src/packages/BUILD/gtk-sharp-2.12.6/glib/SignalClosure.cs:line 173
  25.   at Gtk.Application.gtk_main()
  26.   at Gtk.Application.Run() in /usr/src/packages/BUILD/gtk-sharp-2.12.6/gtk/generated/AboutDialog.cs:line 1
  27.   at Prueba_02.MainClass.Main(System.String[] args) in /home/metaconta/Prueba_02/Prueba_02/Main.cs:line 18
  28.  
  29.  
  30.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
HTML con CGI\'s : programar puerto serie RS232 segun norma IEEE1451.0
Programación C/C++
rayger2010 0 2,240 Último mensaje 16 Junio 2011, 21:26 pm
por rayger2010
rs232
Electrónica
juanjuan19 3 3,580 Último mensaje 10 Febrero 2012, 14:24 pm
por Meta
control rs232 via php
PHP
danny17 2 1,810 Último mensaje 21 Mayo 2014, 14:05 pm
por danny17
Puerto serial RS232 C# lectura de Pulsos
Programación General
_vicho_ 1 2,662 Último mensaje 19 Noviembre 2018, 14:06 pm
por rub'n
Puerto serie por Web
.NET (C#, VB.NET, ASP)
Meta 0 1,665 Último mensaje 9 Enero 2022, 12:58 pm
por Meta
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines