Puerto serie RS232

Iniciado por Meta, 23 Febrero 2009, 21:39 PM

0 Miembros y 1 Visitante están viendo este tema.

Meta

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 (csharp) [Seleccionar]
mySerial new SerialPort("/dev/ttyS0", 9600, None, 8, Two);

La otra manera con comillas así:

Código (csharp) [Seleccionar]
mySerial new SerialPort("/dev/ttyS0", 9600, "None", 8, "Two");

Código completo:

Código (csharp) [Seleccionar]
// MainWindow.cs created with MonoDevelop
// User: metaconta at 17:46 23/02/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using Gtk;
using System.IO.Ports;

public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
{
mySerial new SerialPort("/dev/ttyS0", 9600, None, 8, Two);
}
}


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 (csharp) [Seleccionar]

// MainWindow.cs created with MonoDevelop
// User: metaconta at 17:46 23/02/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using Gtk;
using System.IO.Ports;

public partial class MainWindow: Gtk.Window
{
private SerialPort mySerial;

public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
{
mySerial = new SerialPort("/dev/ttyS0", 9600, Parity.None, 8, StopBits.Two);

if(mySerial == null) return;

mySerial.Open();

if(!mySerial.IsOpen)
{
label1.Text = "Error abriendo";
mySerial.Dispose();
return;
}

byte[] buffer = new byte[1];
buffer[0] = 0x74;
mySerial.Write(buffer, 0, 1);

try
{
mySerial.Write(buffer, 0, 1);
}
catch(Exception ex)
{
//label1.Text = ex.Message;// + " " + ex.StackTrace;
mySerial.Close();
mySerial.Dispose();
return;
}

mySerial.Close();
}
}


Un cordial saludo.
Tutoriales Electrónica y PIC: http://electronica-pic.blogspot.com/

Meta

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 (csharp) [Seleccionar]

// MainWindow.cs created with MonoDevelop
// User: metaconta at 20:59 23/02/2009
//
// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
using System;
using Gtk;
using System.IO.Ports;

public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}

public SerialPort mySerial;

protected virtual void OnButtonTClicked (object sender, System.EventArgs e)
{
mySerial = new SerialPort("/dev/ttyS0", 9600, Parity.None, 8, StopBits.Two);
mySerial.Open();
byte[] mBuffer = new byte[1];
mBuffer[0] = 0x74; // LA letra t.
mySerial.Write(mBuffer, 0, mBuffer.Length);
mySerial.Close();
}
}


En el mensaje de error me da esto:
Código (csharp) [Seleccionar]


Marshaling clicked signal
Exception in Gtk# callback delegate
  Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.IOException: Permiso denegado
  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
  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
  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)
  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
  at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort:Open ()
  at MainWindow.OnButtonTClicked (System.Object sender, System.EventArgs e) [0x00018] in /home/metaconta/Prueba_02/Prueba_02/MainWindow.cs:28
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
  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
  --- End of inner exception stack trace ---
  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
  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
  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000b4] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System/Delegate.cs:422
  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System/MulticastDelegate.cs:71
  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /usr/src/packages/BUILD/mono-2.0.1/mcs/class/corlib/System/Delegate.cs:394
  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
  at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x0000c] in /usr/src/packages/BUILD/gtk-sharp-2.12.6/glib/SignalClosure.cs:118
  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
   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
   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
   at Gtk.Application.gtk_main()
   at Gtk.Application.Run() in /usr/src/packages/BUILD/gtk-sharp-2.12.6/gtk/generated/AboutDialog.cs:line 1
   at Prueba_02.MainClass.Main(System.String[] args) in /home/metaconta/Prueba_02/Prueba_02/Main.cs:line 18


Tutoriales Electrónica y PIC: http://electronica-pic.blogspot.com/