On 24.02.2012 09:38, salorob wrote:
Hi Robert,

I have to be honest, i don't have a clue where to start with this. At this
point my lack of deeper knowledge of C and linux made me decide to try mono
on an embedded device.

I've attached a sample of the recipe from my former post.
The code is untested because I don't have such a device.

Robert

using System;
using System.IO.Ports;
using System.Reflection;
using System.Runtime.InteropServices;

public static class SerialPortExtensions
{
	public static int GetHandle (this SerialPort self)
	{
		var streamField = typeof (SerialPort).GetField ("stream", BindingFlags.Instance | BindingFlags.NonPublic);

		object stream = streamField.GetValue (self);

		var fdField = stream.GetType ().GetField ("fd", BindingFlags.Instance | BindingFlags.NonPublic);

		object fd = fdField.GetValue (stream);
		return (int) fd;
	}

	public static int EnableRS485(this SerialPort self)
	{
		int handle = self.GetHandle ();
		var data = new SerialRS485Ioctl ();
		data.flags = SER_RS485_ENABLED;
		return ioctl (handle, TIOCSRS485, ref data);
	}


	const uint SER_RS485_ENABLED = 1;
	const int TIOCSRS485 = 0x542F;

	[DllImport("libc")]
	static extern int ioctl(int fd, int request, ref SerialRS485Ioctl data);
}

public struct SerialRS485Ioctl
{
	public uint flags;
	public uint delay_rts_before_send;
	public uint delay_rts_after_send;
	[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
	public uint[] padding;
}

class Test
{
	static void Main ()
	{
		var port = new SerialPort ("/dev/ttyS1");
		port.Open();
		Console.WriteLine (port.EnableRS485 ());
	}
}
_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to