Shop OBEX P1 Docs P2 Docs Learn Events
C# Programming for those who like .net and/or mono — Parallax Forums

C# Programming for those who like .net and/or mono

jazzedjazzed Posts: 11,803
edited 2011-10-13 18:24 in General Discussion
Hi.

I'm a fan of C# and it is available on Linux and WinPCs (not sure about Mac - any help?)

In one of the other forums there was an emerging productive discussion of C# programming.

Some have suggested a C# GUI would be a nice project for developing code and communicating with various micro-controllers.

Is .net really cross-platform enough for that to work? Perhaps we can explore that here?

All C# ideas from young or old are welcome here. Please save .net bashing for another thread.

Thanks for your time.
«134567

Comments

  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2011-05-07 12:51
    Mono .NET runtime works on Windows, Linux, Mac OSX, Solaris, BSD, Nintendo Wii, Sony PS3, iPhone, and Android. The Mono project also has a C# compiler that runs on Windows, Linux, Mac, and others. MonoDevelop is an IDE for programming in .NET languages including C# on Windows, Mac, Linux, and more. You can also "cross develop" using Visual Studio on windows and build assemblies that run on any of the Mono .NET runtime variants.

    Just to point it out, so it's clear, the Mono .NET runtime is independent from any compiler or IDE, and you can run your C# (or other .NET language) compiled code on any of the above listed platforms. You can whatever method you desire to produce that compiled code. Whether it's the Mono command line tools, MonoDevelop, or the Visual Studio cross development route, the end result is a .NET assembly (exe) that can run in the Mono runtime (or the native windows .NET runtime assuming you didn't use a Linux or Mac specific library).

    Another thing to keep in mind, you can generally mix and match any .NET language code. So you can have some parts of your code be in C#, some in VB.NET, yet still more in C++/CLI, and so on. It all compiles down to the same CIL for the CLR. There are CLI variants of a lot of languages, including Python, Ruby, COBOL, Lisp, and more. So you can fairly easily bring over a lot of existing code that you might have and "glue" it together with C# code.

    Roy
  • Invent-O-DocInvent-O-Doc Posts: 768
    edited 2011-05-07 12:56
    I know that there is a .NET microcontroller implementation and have seen a board on sparkfun for it. I'm not sure that the prop is a good candidate for this given the memory and cog memory limitations, but it might very well be feasible on the prop II. I think I could envision something like use of .NET languages using LMM mode like Catalina C.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-05-07 13:23
    Roy,

    You mentioned ".exe's". Is a .NET-compatible .exe actual compiled code, or just a convenient packaging for bytecodes that get interpreted by the CLR? 'Just trying to get a handle on .exe's in the context of OS/X and Linux where .exe is not a native file type.

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2011-05-07 13:45
    No idea what these .exe files are exactly but on Linux "file" reports them as:
    "PE32 executable for MS Windows (GUI) Intel 80386 32-bit Mono/.Net assembly"

    Presumably they wrap up the byte codes that are to interpreted by the VM at run time.

    Run the byte codes on Linux with:
    $ mono HelloWorld.exe

    mono knows what to do with them. That HelloWorld is actually a GUI app with a few buttons that work. Created in monodevelop even without reading the manual.

    Linux can be configured to run mono programs automatically just by giving the file name as a command.
  • wjsteelewjsteele Posts: 697
    edited 2011-05-07 13:46
    Is a .NET-compatible .exe actual compiled code, or just a convenient packaging for bytecodes that get interpreted by the CLR?

    Technically, both, Phil. They are packaged 'bytecodes' or IL (Intermediate Language) but they are not interpreted by the CLR, instead, they are recompiled (called Jitting) at first execution into native executables for each platform and then executed.

    Bill
  • Roy ElthamRoy Eltham Posts: 3,000
    edited 2011-05-07 14:14
    Phil,
    Everyone answered for me. On Windows you can just run the exe directly, on other platforms you run it through a native app that implements the runtime. What wjsteele called Jitting is Just In Time Compiling (JITing). It's actually done as it runs in most cases, so you don't get a big delay every time you run the .NET exe as it compiles the whole thing to native.

    Also, the CLR does run bytecode too. It depends on the implementation how much get's JITed, and how much is just interpreting the bytecodes.

    Roy
  • Jorge PJorge P Posts: 385
    edited 2011-05-07 15:05
    Please, if you have the time, anyone releasing dll's using .NET that are intended to be used by other developers, use XML Code Comments on all public methods, properties, and functions. Set your projects to export these comments on the Project Properties build tab. Then supply that XML file with your dll when you release it.

    It helps so much when using .NET dll's the way they were intended. All experianced .NET developers will love you for it.
  • wjsteelewjsteele Posts: 697
    edited 2011-05-07 15:11
    Roy Eltham wrote: »
    Also, the CLR does run bytecode too. It depends on the implementation how much get's JITed, and how much is just interpreting the bytecodes.

    Just to clarify... in the full Microsoft .NET CLR platform, all code is jitted before execution... it just depends on when. There is no bytecode interpreter in the full .NET Framework. It does, however, both have the option of precompiling the code at runtime as well as dynamically compiling the needed functions/methods that are being called, which has the advantage of dynamic code (like the intrepreter does,) but the performance of native code. (Again, with the one time hit for compiling each method.) All compiled code is stored in the "Application Execution Cache" and executed directly from there.

    The .NET Compact Framework (used in Windows CE) also JITs the code, however, due to some special constraints (like small device memory) it can unload previously jitted code and recompile it when needed to free up memory.

    Mono does have the option for using an IL intrepreter for some of it's platforms, but not all.

    .NET MicroFramework, which was mentioned by Invent-o-Doc, always interprets due to very low memory available. There is no option to pre-jit the code.

    Bill
  • Jorge PJorge P Posts: 385
    edited 2011-05-07 15:31
    Roy Eltham wrote: »
    Mono .NET runtime works on Windows, Linux, Mac OSX, Solaris, BSD, Nintendo Wii, Sony PS3, iPhone, and Android. The Mono project also has a C# compiler that runs on Windows, Linux, Mac, and others. MonoDevelop is an IDE for programming in .NET languages including C# on Windows, Mac, Linux, and more. You can also "cross develop" using Visual Studio on windows and build assemblies that run on any of the Mono .NET runtime variants.

    ...

    Roy

    Dont forget that .NET also runs on XBox 360 called "XNA Framework", and .NET Compact runs on Windows CE and Zune.

    I will try and get some basic serial port communications libs uploaded to my site, these lib's/dll's will need to be called by your code but I will create some sample Windows Forms that use them so you can see how they work. All my project code will be in Visual C# 2008 Express. This will give anyone/everyone the ability to use them if they so desire. the direct link to where I will upload them will be http://whatsavailable.org/Propeller.aspx Give me a few days to put together some good samples.

    Unfortunately, I will not be able to do much IDE coding for a compiler, that is beyond my experiance, but I may able to incorporate the use of propellent or a similar tool for compiling.
  • Mike GMike G Posts: 2,702
    edited 2011-05-07 16:11
    This is my standard serial port create (wire up) handler. I'd be curious to know if the all serial port members function in Mono. Roy, do you know?
            #region CreatePort handler
            /// <summary>
            /// Create a serial port instance
            /// </summary>
            /// <param name="sender">Sender</param>
            /// <param name="e">Serial event arguments (port and baud)</param>
            public void CreateSerialPort(Object sender, SerialPortEventArgs e)
            {
                
    
                // Create the serial port
                serialPort = new SerialPort(e.Port, int.Parse(e.Baud));
                serialPort.WriteTimeout = 5;
                serialPort.ReadTimeout = 100;
    
                // Open the serial port
                serialPort.Open();
    
                // Wire the serial port data received handler
                serialPort.DataReceived += SerialPort_DataReceived;
    
                // Fire the port created event
                SerialPortCreated(this, e);
            }
            #endregion
    
  • wjsteelewjsteele Posts: 697
    edited 2011-05-07 16:12
    Jorge P wrote: »
    Dont forget that .NET also runs on XBox 360 called "XNA Framework", and .NET Compact runs on Windows CE and Zune.

    And Silverlight (which is itself it's own .NET Framework) runs on Windows, Mac OSX, Windows Phone and certain Nokia Phones!

    Most GPSs that use TrafficLink also are .NET based, as that service is provided through a technology called MSN/Direct which came out of the SPOT Watches (and is now the opensourced .NET MicroFramework!!!) But the GPSs are usually running CE.

    Bill
  • Jorge PJorge P Posts: 385
    edited 2011-05-07 16:43
    Mike G wrote: »
    This is my standard serial port create (wire up) handler. I'd be curious to know if the all serial port members function in Mono. Roy, do you know?
    #region CreatePort handler
    /// <summary>
    /// Create a serial port instance
    /// </summary>
    /// <param name="sender">Sender</param>
    /// <param name="e">Serial event arguments (port and baud)</param>
    public void CreateSerialPort(Object sender, SerialPortEventArgs e)
    {
     
     
    // Create the serial port
    serialPort = new SerialPort(e.Port, int.Parse(e.Baud));
    serialPort.WriteTimeout = 5;
    serialPort.ReadTimeout = 100;
     
    // Open the serial port
    serialPort.Open();
     
    // Wire the serial port data received handler
    serialPort.DataReceived += SerialPort_DataReceived;
     
    // Fire the port created event
    SerialPortCreated(this, e);
    }
    #endregion
    


    You can break it down a bit more if you just want to see what ports are currently connected, either at app startup or through a button, etc...
    //
    // filename="myPorts.cs"
    //
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO.Ports; // required for serial port access
    //
    // change the namespace to whatever you want
    namespace PropellerCommunication_CSharpApp
    {
    public class myPorts
    {
    ///<summary>
    /// Returns an array of current ports on a PC
    ///</summary>
    ///<returns>
    /// returns an array of strings indicating the currently opened serial ports
    ///</returns>
    ///<remarks>
    /// Make sure the device is connected so it's port is visable
    ///</remarks>
    public string[] getMyPorts()
    {
    // returns a string array containing all current serial ports
    string[] myPorts = SerialPort.GetPortNames();
    return myPorts;
    }
    }
    }

    populate a combo box with the following
    string[] items = myPorts.getMyPorts();
    //
    foreach (string port in items)
    {
    // I used a comboBox renamed to cboSerialPort.
    cboSerialPort.Items.Add(port);
    }
  • jazzedjazzed Posts: 11,803
    edited 2011-05-07 17:32
    Nice contributions so far. And such a health, happy conversation! :)

    Mike G. and JorgeP, I used similar serial port techniques posted before (maybe i used delegates) with mono 2.1 on Fedora a couple of years ago, and could not get anything to work. I'll try it again once i get Debian installed under a VM on my workstation ... been seriously putting that off :<

    I'm hoping to post a source version of Propalyzer here either Sunday or Monday. That works great on mono with everything except serial ports. Maybe someone can help solve the serial port riddle.

    Let's see if we can further lift the potential of C# in Parallax micro applications. Thanks.
  • Jorge PJorge P Posts: 385
    edited 2011-05-07 18:49
    I am attaching a simple C# Solution sample that uses code similar to what I just posted in the code I pasted previously. This can determine if ports are visible through mono, I added some leangthy comments to the source .cs files. A longer discription can be viewed at my website at http://whatsavailable.org/Propeller.aspx

    But I will post the zip here as well for those that dont want to go to my site. It needs to be compiled so you can use it to see if monodevelop will indeed compile the code without errors. If mono supports serial ports, it should run fine and display a list of serial ports in the dropdown box once the "Get Serial Ports" button is clicked.

    If it dont compile in Mono, I will compile a debug and retail version myself, and post thise files to see if they will run.

    After that, if they don't run, then serial is probably not supported yet in mono. OR mono needs to be configured.

    PM me once with errors under mono

    I will be gone for 2 days so I will get to the PMs on the 9th or 10th

    Hope it works fine....
  • Mike GMike G Posts: 2,702
    edited 2011-05-07 18:58
    I guess I should tinker. I have this extra box sitting here. At one time I had Debian loaded but I never could get the video driver installed or working properly. If I wanted to install Linux, is there a version that's kinda' idiot proof on install?
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-05-07 18:59
    Ah - a C# thread that is .net friendly. I think I'll hang out here a bit!

    Attached is a screenshot of my IDE front end for catalina. This is written in vb.net 2008 and enables development of C89 code.

    vb.net is very similar to C#. And I think C89 is a subset of C#.

    How cool would it be to have an IDE written in C# that can compile code in C# for running on the propeller?
    1024 x 768 - 146K
  • Mike GMike G Posts: 2,702
    edited 2011-05-07 19:06
    I'm glad this thread is here too... I was a little hesitant to reveal my .NET on the forum.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-05-07 19:08
    Mike G wrote:
    If I wanted to install Linux, is there a version that's kinda' idiot proof on install?
    Linux Mint has been the easiest install for me so far. It's Debian-based, so packages install easily, too. Mepis is another easy one.

    -Phil
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-05-07 19:15
    Re Jorge P, as an example of how similar vb.net and your C# code is, this is the serial bit of code from my IDE to put all the available ports in a drop down combo box
            Dim ports As String() = SerialPort.GetPortNames()
            Dim port As String
            ComboBox1.Items.Clear()
            For Each port In ports
                ComboBox1.Items.Add(port)
            Next port
            ComboBox1.Text = ComPort ' com port
    

    at the beginning of the program
    Imports System.IO.Ports
    
    and then
    Public Class Form1
        Dim WithEvents SerialPortName As New IO.Ports.SerialPort ' generic serial port
    

    This could be quite a fun little project.
  • Mike GMike G Posts: 2,702
    edited 2011-05-07 22:02
    Just when you think you have a handle on your craft, someone who shall remain nameless -- Phil -- points you to Linux Mint. Holy Smile... I know nothing... again.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 00:56
    Jorge P,

    <rant severity="high">
    I was starting to get a warm feeling about C# until you mentioned XML Code Comments.

    That is one of the ugliest and dumbest things I have seen in programming ever. This XML thing seems to have gotten out of hand.

    Now not only do you have the tedious repetition of the parameter names, in the C# method definition you also get the endless repetition of those XML tags "<param...> ...</param>"
    and attribute names.

    C# is syntax is derived from C which was supposed to concise. This is like bolting ADA or COBOL on top of it.

    I really don't want to end up having to read reams of that stuff in source code.

    C# syntax was looking quite clean to me, why oh why did anyone think soiling it with XML was a good idea?
    </rant>

    OK that won't put me off trying to pick up some C# know how but it really does make me wince.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-05-08 01:01
    Mike G,

    Don't feel bad. This whole .NET/Mono thing has me blindsided. And, although I'm loathe to admit it, my ignorance has been as much studied as accidental. Not that I regret concentrating on Perl instead, but sheesh!

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 01:24
    Mike G and Phil,

    I'm done for. In the space of a few days I have been "blind sided" by both this .NET development on the forums and Linux Mint (which I'd never heard of despite following Linux for years)
    my ignorance has been as much studied as accidental.
    Same here.
  • max72max72 Posts: 1,155
    edited 2011-05-08 04:36
    another dumbproof option is ubuntu.
    I have it running on my netbook with no effort. Moreover there is a dual boot installation option for windows machines which is rather nice (wubi).
    It installs ubuntu on a NTFS disk, and offers a dual boot option. If you don't like it simply uninstall as any other windows software.
    As a general alternative virtual box offer a nice place to experiment.
    Massimo
  • wjsteelewjsteele Posts: 697
    edited 2011-05-08 05:33
    Heater. wrote: »
    That is one of the ugliest and dumbest things I have seen in programming ever.

    Hmmm... I'm not sure how else you'd do this. It's not just about commenting the code... the real beauty is the fact that when you write a library, it allows your comments and instructions to the users to be exposed to the tools.

    For example, when you hover over an method, it'll popup a tooltip giving you the correct usage of that method. In fact, it'll also show you any overloaded methods as well.

    By using XML as the basis for it, it allows ANY tool that understands XML to parse the source code file directly.

    I think the benefits of this tech far outweigh the down side. Be sides... tools like VS can actually hide it as well via collapsing regions, etc.

    Bill
  • Invent-O-DocInvent-O-Doc Posts: 768
    edited 2011-05-08 06:07
    .NET on the prop2 would make it a very accessible thing with a powerful, full featured language. I'm thinking for those things where you want a IL interpreter running a large memory model. I've looked at the Catalina documentation. Catalin is powerful and full featured, an amazing accomplishmint, but the documentation is daunting and it isn't accessable and easy to start with like C on the Arduino is. Could a .NET based approach build on the technical approach of what Catalina can do and be something more accessible?
  • ctwardellctwardell Posts: 1,716
    edited 2011-05-08 08:21
    Heater. wrote: »
    That is one of the ugliest and dumbest things I have seen in programming ever. This XML thing seems to have gotten out of hand.

    This is Microsoft's answer to Javadoc, so not really unique to C# or VB.NET.

    It takes some getting used to, but it can be helpful for others using your libraries as wjsteele pointed out.

    C.W.
  • jazzedjazzed Posts: 11,803
    edited 2011-05-08 09:07
    Heater. wrote: »
    Now not only do you have the tedious repetition of the parameter names, in the C# method definition you also get the endless repetition of those XML tags "<param...> ...</param>" and attribute names.
    You know I usually skip big old stars and bars comments entirely so I'm not really annoyed by this kind of thing.

    Actually when I need to understand what a parameter means, some kind of tagging helps me to wade through the usual Smile such as 120 characters of *====... or whatever some maniac thinks I need to see to understand their program.

    I've used Javadoc tags for years and years as documentation markup for DOXYGEN. Javadoc is much more concise, but it's not "standardized" (for whatever that's worth) like XML. You will find this in almost all my published code because I think it's helpful to others AND i don't have to write some silly API document that has to be updated every time I add (maintain) to my code.

    This kind of thing generally helps to reduce errors in communication on several fronts. XML just happens to be the "channel" of communication that suits the documentation program.

    So in the spirit of better all around code quality, it's best to just be tolerant of the markup.
    (And I guess I should be tolerant of enormous and useless stars and bars lines).
  • jazzedjazzed Posts: 11,803
    edited 2011-05-08 09:09
    Dr_Acula wrote: »
    Re Jorge P, as an example of how similar vb.net and your C# code is, this is the serial bit of code from my IDE ....
    DR_A, The output of the 2 programs is identical. There are tools you can use to reverse engineer the code to the language of your choice :)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-05-08 10:28
    XML is okay, but I doubt that it was ever intended to be hand-written. I chose XML as the source file format for the S2 GUI because it's able to represent the graphical program structure so well, and because it's a text format, not some unreadable packed binary. But it's generated in Perl; I would hate to have to type it in. That said, it would not be difficult to come up with a more human-friendly commenting system that could be translated to XML in the same spirit that Perl's pod2html or my spin2html systems operate. There's nothing wrong with XML in principle, since it's a good representation for hierarchical structures. It just needs to be implemented in ways that obviate the need for humans to compose in it.

    -Phil
Sign In or Register to comment.