EdwinJ85
Posts: 269
Joined: Wed Feb 01, 2012 4:44 pm
Contact: Website

Re: Mono (C#) anyone?

Fri Aug 24, 2012 1:19 pm

andyclap2 wrote:> I've changed it a little myself to make it a static class with static methods, I think this will help people use it a little more easily.

Careful with static classes, while they're easy to use, they make testing difficult.

When developing it's v. useful to be able to run your code against a "fake" version that acts differently - e.g. it might display the output on the screen, write to a log, check for some expected output, whatever you need.
That way when you come to plug in your circuitry you know the code works - so if there are any problems it makes them easier to find.

To make this easy, in the c# object oriented world you generally describe an interface to the thing you're going to depend on, and "code to an interface not an implementation".
The interface (or abstract base class, as per aanderson's code) describes what you can do, but not what/how it gets done, so you can implement it in several different ways. Your code asks for an implementation to be provided at runtime, which your Main() sets up and passes in. Something like:

Code: Select all

public static void Main(string[] args)
{ 
  var gpio=args.Length>0 && args[0]=="test"
    ?new GPIOFake()
    :new GPIOFile(); 
  var amazingIOFlashingThing=new AmazingIOFlashingThing(gpio); 
  amazingIOFlashingThing.DoYourStuff(); 
}
(note - you would generally have a separate test harness, or use a general purpose test framework like nunit rather than do it with an arg like this; and of course I've not provided the interface/base or either implementation here...)

This may of course be overkill for a lot of projects, but when you start to build big complex systems you really need to be able to test each part in isolation. And here endeth chapter 0 of "So, you want to be a software engineer!"
I think it depends on scale - your way is far better for building the next curiosity drone but for my goals of lighting up one or two LEDs, my class gets you up running faster. I think you could put both methods into a library or even just have the class contain a static instance of itself for working purposes and instances for testing.

I like your interface idea, that's pretty neat. I might try a ABC to implement some of the very basics though to save time when creating new implementations. Beats cut and pasting 'throw new NotImplementedException()' all the time.

PS: Only a true software engineer would start a chapter index at 0. Kudos to you good sir.
Hello!

christopher.landress
Posts: 5
Joined: Sat Aug 25, 2012 7:03 am

Re: Mono (C#) anyone?

Sat Aug 25, 2012 7:12 am

I've been using Mono exclusively to write a data acquisition service that connects to a USB GPS adapter as well as a USB 3G modem (both of which are standard serial devices) and collects, sends, and receives commands remotely. Not counting USB current issues, it has worked perfectly. Connecting to a serial port with c# is incredibly easy and the language supports a lot of features that aren't available in other languages.

Open a Port

Code: Select all

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.ComponentModel;
using RaspberryDAD.COM;

namespace RaspberryDAD.GPS
{

    public delegate void GPSDataReceivedEventHandler(object sender, GPSDataReceivedEventArgs e);

    public class GPSDataReceiver : System.Object
    {
        private SerialPort serialPort;
        private BackgroundWorker backgroundWorkerMain;
        private bool _Running = false;
        private bool _Runnable = false;
        private string _LastErrorMessage { get; set; }
        private Int64 _MessagesReceived { get; set; }
        
        public COMPortSettings PortSettings { get; set; }
        public event GPSDataReceivedEventHandler GPSDataReceived;
        public bool Running { get { return this._Running; } }
        public bool Runnable { get { return this._Runnable; } }
        public string LastErrorMessage { get { return this._LastErrorMessage; } }
        public Int64 MessagesReceived { get { return this._MessagesReceived; } }

        public GPSDataReceiver(COMPortSettings PortSettings)
        {
            this.PortSettings = PortSettings;
            this.InitializeComponents();
        }

        private void InitializeComponents()
        {
            this.serialPort = new SerialPort(this.PortSettings.ComPort, this.PortSettings.BaudeRate, this.PortSettings.Parity, this.PortSettings.DataBits, this.PortSettings.StopBits);
            try
            {
                this.serialPort.Open();
                if (this.serialPort.IsOpen)
                {
                    this.serialPort.Close();
                    this._Runnable = true;
                    this.backgroundWorkerMain = new BackgroundWorker();
                    this.backgroundWorkerMain.WorkerReportsProgress = true;
                    this.backgroundWorkerMain.WorkerSupportsCancellation = true;
                    this.backgroundWorkerMain.DoWork += new DoWorkEventHandler(backgroundWorkerMain_DoWork);
                    this.backgroundWorkerMain.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorkerMain_RunWorkerCompleted);
                }
                else
                {
                    this._LastErrorMessage = "Unable to open serial port with the supplied port settings.";
                    this._Running = false;
                    return;
                }
            }
            catch (System.Exception e)
            {
                this._LastErrorMessage = "Unable to open serial port with the supplied port settings : \n" + e.Message;
                this._Running = false;
                return;
            }
        }

        void backgroundWorkerMain_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this._Running = false;
        }

        void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e)
        {
            this.serialPort.Open();
            while (this._Running && this.serialPort.IsOpen)
            {
                string output = this.serialPort.ReadLine();
                NMEA.Sentence s = new NMEA.Sentence(output.Substring(0,output.Length-1));
                this._MessagesReceived++;
                this.GPSDataReceived(this, new GPSDataReceivedEventArgs(s));
            }
            this.serialPort.Close();
        }


        public void Start()
        {
            if (this._Runnable)
            {
                if (this.backgroundWorkerMain.IsBusy)
                {
                    this.backgroundWorkerMain.CancelAsync();
                }
                this.backgroundWorkerMain.RunWorkerAsync();
                if (this.backgroundWorkerMain.IsBusy)
                {
                    this._Running = true;
                }
            }
        }

        public void Stop()
        {
            if (this._Running)
            {
                this._Running = false;
            }
        }
    }
}
A few NEMA docs and DataContracts away from a perfectly functioning GPS receiver.

contractorwolf
Posts: 13
Joined: Wed Jun 13, 2012 10:52 pm

Re: Mono (C#) anyone?

Mon Aug 27, 2012 6:23 pm

Wow, this is great. I have been working mostly in the .NET MF tools such as Netduinos and FEZ chips (fez domino, et al.) but have tried a few projects with my Raspberry Pi in the hopes of one day making something that can do OpenCV on a small machine like the RPi.

What I am starting to wonder after reading all of this is if it could be possible to just a button on the RPi and run a simple python script that would look for a named EXE file on a USB drive and run that file. If so you could use the RPi just like I do my netduinos (albeit without debugging and breakpoints).

Create the program you want to run in visual studio (visual c# is free), deploy the EXE to a thumbdrive, put the thumbdrive in the RPi and hit the execute button. Simple c# with serial, i2c and more ram, not to mention Wifi (which mine already has installed). The possibilities are endless and it would mean I could use probably the most elegant and powerful languages out there: c# (feel free to flame me). That 13 year old kid is really the reason the Raspberry Pi is out there, way to go!

MK3424
Posts: 1
Joined: Mon Oct 01, 2012 8:07 pm

Re: Mono (C#) anyone?

Mon Oct 01, 2012 8:12 pm

I am learning for my professional bachelor networks/ICT and our program language that we are using is C# and i must say it is one off my favourites because it's easy to learn and adaptable.

orthogonal3
Posts: 1
Joined: Mon Oct 22, 2012 5:17 am

Re: Mono (C#) anyone?

Mon Oct 22, 2012 5:32 am

Hey guys,

Did anyone get anywhere with I2C interfacing or have any pointers?

I'm at pains to start poking around in Python as it will be a new language, but I'm sure I could cobble together a few scripts with quick2wire and call them out of c# but that just doesn't seem the way to do things properly, nor do I really want to go bit-banging the I2C port as GPIOs! :)

I can see (if I read correctly) that you guys magically ported the GPIO code and built up from there, yes, I bet it was effort! Kudos to you guys!

I've got an electronics project coming together building a controller for an aquarium and its modular built on the I2C bus, then I find that bus isn't really going in C# yet for the Pi :(

I've got the Pi to fire up the busses and I can i2c-detect the components but its how to talk to them?

Writing to the device file would be an option 5hz would be lightning fast for this job, things would just get polled every few seconds but I can't find any examples of how to talk to the device file /dev/i2c_0 to actually do anything!

Any tips would be appreciated!

Thanks.

mshmelev
Posts: 1
Joined: Tue Nov 27, 2012 4:56 am

Re: Mono (C#) anyone?

Tue Nov 27, 2012 5:00 am

After long search of any library on C# to use I2C and finding nothing, I had to come up with my own library. In case anybody else needs it I put it on GitHub: https://github.com/mshmelev/RPi.I2C.Net

3gws
Posts: 49
Joined: Sat Oct 20, 2012 10:32 am

Re: Mono (C#) anyone?

Tue Nov 27, 2012 1:05 pm

mshmelev , thank you for taking the time to put this Library together and sharing this with us Mono C# users. I hope this along with other contributions will bring more 'C#' programmers to the Raspberry Pi.

There is also a very good R-Pi GPIO Library for use with C# on this blog http://www.codehosting.net/blog/BlogEngine/?tag=/Mono.

Now all we need is the Hardware Floating Point Patch for Mono on the R-Pi !

3gws

Tup
Posts: 6
Joined: Mon Jul 09, 2012 5:56 am

Re: Mono (C#) anyone?

Tue Nov 27, 2012 9:55 pm

I have followed David's blog for a while and while I've managed to blink led's in various fashions I've not been able to convert any of the code to enable pwm on the gpio ports through lack of understanding. Has anyone managed to get pwm working with c#?

sonite
Posts: 14
Joined: Fri Aug 17, 2012 3:31 pm

Re: Mono (C#) anyone?

Sat Jan 12, 2013 6:28 pm

Here is a small tutorial with mono and a .net project (scroll down to mono):
http://www.gruffman.se/raspberry-pi/

exstud
Posts: 12
Joined: Mon Dec 10, 2012 12:00 pm

Re: Mono (C#) anyone?

Wed Feb 20, 2013 9:11 am

>> Now all we need is the Hardware Floating Point Patch for Mono on the R-Pi !

Is anyone working on this? I'm having issues with mono on archlinux ... what OS should I use to have a stable mono (now and after the next update)?

amigarulez
Posts: 42
Joined: Wed Jul 18, 2012 10:16 am

Re: Mono (C#) anyone?

Wed Feb 20, 2013 9:44 am

exstud wrote:>> Now all we need is the Hardware Floating Point Patch for Mono on the R-Pi !

Is anyone working on this? I'm having issues with mono on archlinux ... what OS should I use to have a stable mono (now and after the next update)?
Don't know if anyone is actively working on it, have seen some patches but haven't been able/had time to compile them. Would love to see some more Mono support for RPi, perhaps an Kickstarter.

Personally I use Archlinux arm 2012-09-16, which is the latest soft float release afaik.
http://downloads.raspberrypi.org/images ... 012-09-16/
packages are still updated so a "pacman -Syyu" will bring you pretty up to date.

I compile Mono from source, currently I use Mono 3.0.3 which has worked very well for me.

exstud
Posts: 12
Joined: Mon Dec 10, 2012 12:00 pm

Re: Mono (C#) anyone?

Thu Feb 21, 2013 12:54 pm

Do you build mono on the RPi? I just tried it and it failed with an out of memory exception...

amigarulez
Posts: 42
Joined: Wed Jul 18, 2012 10:16 am

Re: Mono (C#) anyone?

Thu Feb 21, 2013 1:04 pm

exstud wrote:Do you build mono on the RPi? I just tried it and it failed with an out of memory exception...
I create an temporarily swap while compiling, also Mono 3.0.4 had an Jit issue, 3.0.5 compiled great, takes a few hours though:)

In arch that's achived by below commands
fallocate -l 128M /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

pauldy
Posts: 54
Joined: Tue Jun 12, 2012 3:34 pm

Re: Mono (C#) anyone?

Tue Mar 05, 2013 4:34 pm

Anyone care to share steps to get it compiled. I followed the steps listed earlier and received asm errors regarding the selected processor does not support ARM mode 'dmb'. I downloaded mono using git, and then updated as follows.

Code: Select all

git clone git://github.com/directhex/mono-1.git
cd mono-1
git checkout remotes/origin/armhf-port
./autogen.sh --disable-mcs-build
make CFLAGS=-DARM_FPU_VFP_HARD


Return to “Other programming languages”