Using C# Interactive Shell

Amongst other tools, Mono provides a very interesting C# Interactive Shell that may be very useful on a Raspberry Pi.

Installing C# Shell

C# Shell requires the Mono Framework to be installed on your Raspberry Pi.

Once done, you have to install the shell itself

$ sudo apt-get update
$ sudo apt-get install mono-csharp-shell

Starting C# Shell

Once installed, launch the C# Shell:

$ csharp

Or, for elevated privileges

$ sudo csharp

The shell displays the following lines:

Mono C# Shell, type "help;" for help

Enter statements below.

Let’s play!

Typing Code

Every sentence of C# code may be typed.

A sentence is evaluated when the final ; character is typed (or the closing } characted for a block of code).

var i = 10;

If the sentence evaluates to a non-void value, the value is displayed:

i;
10

All the Mono/.NET framework is available, either directly (for System namespace):

Environment.OSVersion;
Unix 3.2.27.0

… or by using the required namespaces:

using System.Globalization;
new CultureInfo("hy-am").DisplayName;
"Armenian (Armenia)";

… or even by loading your own assemblies:

LoadAssembly("Raspberry.IO.GeneralPurpose");
using Raspberry.IO.GeneralPurpose;
var cnx = new GpioConnection();

Exiting C# Shell

You want to quit C# Shell ?

quit;

And it’s over.

Use a webcam plugged in a Raspberry pi with Mono in C# using RaspberryCam

RaspberryCam is a Mono/.NET assembly providing access to cameras plugged in your Raspberry Pi.

This program is based on v4l API and some fswebcam source headers.

With RapberryCam, you can take pictures and videos from a webcam pluged in your Raspberry Pi.

For the moment, the video stream is not a MPEG stream, but just a succession of compressed frames.

Source code is here : https://bitbucket.org/rflechner/raspberrycam/wiki/Home

Or you can just install the NuGet RaspberryCam in your project. (cf. https://nuget.org/packages/RaspberryCam)

In a future version, a real MPEG format should be implemented.

Before your first usage

Just copy RaspberryCam.so into /Lib

Or :

Copy C sources files on your Rapsberry Pi and type:

$sudo make
$sudo make install

Try it

Run ServerExample on your Rapsberry Pi

mono RaspberryCam.ServerExample.exe

And run RaspberryCam.VideoViewer.exe on your PC

Take a simple picture from C# code:

var cameras = Cameras.DeclareDevice()  
  .Named("Camera 1")
  .WithDevicePath("/dev/video0")  
  .Memorize();

cameras.Get("Camera 1").SavePicture(new PictureSize(640, 480), "Test1.jpg", 20);

// Or

cameras.Default.SavePicture(new PictureSize(640, 480), "Test2.jpg", 20);

// Best practice: cameras should be a single instance in your program  

Host a video and pictures server in your application

var videoServer = new TcpVideoServer(8080, cameras);  
Console.WriteLine("Server strating ...");  
videoServer.Start();  
Console.WriteLine("Server strated.");  

Download pictures from your Raspberry Pi

var videoClient = new TcpVideoClient(serverHostIp, serverPort);  
videoClient.StartVideoStreaming(new PictureSize(imageWidth/2, imageHeight/2)); //Open hardware  
var data = videoClient.GetVideoFrame(compressionRate); //data contains a simple jpeg frame  
videoClient.StopVideoStreaming(); //Close Hardware

Installing Mono on Raspberry Pi

Now, how do I use Mono on my Raspberry Pi ?

Installing Mono

First, you have to install the Mono framework, which is pretty easy :

$ sudo apt-get update
$ sudo apt-get install mono-runtime

This will update the package list and install the Mono runtime.

Running Programs

Once done, you’re ready to run Mono/.NET programs.

Usually, a Test.exe program must be started as follow:

mono Test.exe

Or, if your program requires elevated rights:

sudo mono Test.exe

Writing Programs

If you’re familiar with Visual Studio, the best you can do is… to use Visual Studio! Once compiled, programs created with Visual Studio will run on Mono (well, you will soon discover that some restrictions apply).

In order to test your Mono setup, create a new Console application (target it for .NET Framework 2.0 or .NET Framework 4.0):

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Running on {0}", Environment.OSVersion);

            Console.WriteLine("Press a key to continue");
            Console.ReadKey(true);
        }
    }
}

Compile, copy the .exe file to your Raspberry Pi, run the program (mind the case!)

$ mono Test.exe

Enjoy the result!

Running on Unix 3.2.27.0
Press a key to continue

You will soon discover that there’s a problem with DateTime on hard-float ABI systems. You may choose to use a ‘Soft-float’ Raspbian until a fixed release of Mono is available… but that’s another story!