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

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

24567

Comments

  • Jorge PJorge P Posts: 385
    edited 2011-05-08 10:40
    Heater. wrote: »
    Jorge P,

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

    lol, I felt the same way at first. It does seem sorta dumb. But if you use libs/dll's that you did not create, from someplace else, they pup up a more descriptive feedback of what they do, from an automatic code completion point of view they are great if you don't have the source code.

    I outline a a small sample of that, for a reason, in the main source file I posted in the zip. Using C# you can replicate the code completion part in spin, as soon as you type a ( . ) dot/period charactor, all the public/private functions/properties that are available will display. I think something like that would be helpfull to beginners in SPIN and PASM. All the docs are there to incorporate that, when you install the msdn express help files durring C# install. I will read them over again to freshen my memory and setup another project, Project_2 as an example using a parser that will detect the pub and pri in an spin driver. Its been about 2 years since I played with doing that but it is nice.

    And the XML comments dont need to be there at all, or any special way, VB and C# are fairly easy to read without them unless you are doing advanced encryption and such.

    I'll keep my XML code comments shorter from now on.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 11:28
    I totally understand the idea of the XML comments as an aid to enabling IDEs to provide assistance, for automatically generating documentation etc.

    I just wish they had found/devised a less monstrous way to do it. Source code should be clear for humans to read. XML, despite its intentions is not.

    As Phil said one could use any user friendly syntax for comments into the comments of your language and the parse that out into your XML. What would have been so hard about that.

    A better approach would have been to define a syntax for such comments into C# itself.

    As someone said "XML is like violence , if it does not work use more":)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-05-08 11:38
    heater wrote:
    As Phil said one could any user friendly syntax for comments into the comments of your language and the parse that out into your XML. What would have been so hard about that.
    I didn't mean to imply that that bridge has been burnt, but to suggest -- for those who don't like to write XML -- that regular comments could easily be formatted in an unambiguous and less wordy fashion for an XML markup generator to recognize and replace with XML comments. This could be done now, without redesigning the language. ('Just trying to keep the tone of this thread solution-oriented, as the OP requested, rather than problem-oriented. :) )

    -Phil
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 12:19
    OK Phil, on a solution oriented note I am pleased to report that Jorge P's serial port form works just fine when compiled under monodevelop on Debian. Correctly detects ttyS0 to ttyS3 and ttyUSB0. I have to try this on my ARM board and Ubuntu tomorrow.
  • jazzedjazzed Posts: 11,803
    edited 2011-05-08 12:43
    Heater. wrote: »
    OK Phil, on a solution oriented note I am pleased to report that Jorge P's serial port form works just fine when compiled under monodevelop on Debian. Correctly detects ttyS0 to ttyS3 and ttyUSB0. I have to try this on my ARM board and Ubuntu tomorrow.

    I'm afraid that detecting the port is not the hard part. Have you tried using it to communicate yet?
    That is a very key question. I'm sure it's possible, but do we have code that will do it?

    @Phil, thanks for identifying this thread as requesting "solution oriented" discussion.

    My questions above identify a problem in need of a solution :)
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 13:04
    Heater. wrote: »
    OK Phil, on a solution oriented note I am pleased to report that Jorge P's serial port form works just fine when compiled under monodevelop on Debian. Correctly detects ttyS0 to ttyS3 and ttyUSB0. I have to try this on my ARM board and Ubuntu tomorrow.

    Awesome. The .NET code now needs to determine which OS it is running on at startup so we can determine how to access the variouse port names on different systems. How about mono on MAC anyone?? What does the combo box populate with???

    Thanks Heater, this is where the compatibility issues arise, I forgot that *NUX uses tty's and the .NET code needs to call them as such to work wheras MS uses COM1, COM2, etc...
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 13:09
    Forgot to mention that as a workaround you could create a symbolic link called COM1, COM2, etc... to the TTY's or create some new device descriptors in the /dev directory that can be distributed with any software created.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 14:23
    On another solution oriented note I am pleased to report that using serial ports from C# and mono under Debian works just fine.

    I added the following method to Jorge P's SerialPortsLib class:
             public void testSerialPort(string name) 
            { 
                SerialPort p  = new SerialPort(); 
                p.PortName = name; 
                p.BaudRate = 115200; 
                p.Parity = System.IO.Ports.Parity.None; 
                p.DataBits = 8; 
                p.StopBits = System.IO.Ports.StopBits.Two; 
                p.Handshake = System.IO.Ports.Handshake.None; 
                p.ReadTimeout = 500;     
                p.WriteTimeout = 500; 
                p.Open();             
                for (int i = 1; i < 1000; i++) 
                { 
                    p.Write("Hello World!\n"); 
                }             
                p.Close(); 
            } 
    

    Then I call it from the frmMain button click handler like so:
    ports.testSerialPort("/dev/ttyUSB0");
    

    The I run the thing and hit the button, lo and behold the blue LED on my Prop Plug lights up!!
    OK I have not actually received the data and checked it's OK but it seems likely it is just fine.
    This is good news, no messing with symbolic links or such for the ports.

    The combo box shows all the serial ports on my system like so:

    /dev/ttyS0
    /dev/ttyS1
    /dev/ttyS2
    /dev/ttyS3
    /dev/ttyUSB0
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-05-08 14:37
    Jorge P wrote:
    ... wheras MS uses COM1, COM2, etc...
    Actually, that will get you into trouble at higher com port numbers. Use \\.\COMxx, instead.

    -Phil
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 14:55
    Actually, that will get you into trouble at higher com port numbers. Use \\.\COMxx, instead.

    -Phil

    Noted, thanks for that tip. A quick question, I have only ever used RedHat and S.u.S.E., should I automaticaly assume that all versions of *NUX uses /dev/ for devices or is there a few versions where the directory is different?
  • tonyp12tonyp12 Posts: 1,951
    edited 2011-05-08 14:55
    I wrote some simple code with quicksharp
    http://quicksharp.sourceforge.net/

    I recommend it, as if it was not for it's easy and fast way to
    get started in C# I probably would never even tried.
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 15:20
    Heater. wrote: »
    ...
    ...
    Then I call it from the frmMain button click handler like so:
    ports.testSerialPort("/dev/ttyUSB0");
    

    Then I run the thing and hit the button, lo and behold the blue LED on my Prop Plug lights up!!
    OK I have not actually received the data and checked it's OK but it seems likely it is just fine.
    This is good news, no messing with symbolic links or such for the ports.

    ...
    ...

    Add another button to the form, name it "btnConnect" and change the Text field to "Connect" and call the code from there passing the variable from the combo box, once populated.

    in the new buttons click event handler in frmMain.cs (you may have to double click the button in the IDE to generate the event handler) add this code
    [SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] btnConnect_Click([/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]EventArgs[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] e)
    {
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]   //Declare a new instance of the SerialPortsLib class and give it a simple
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]   //name (ports) 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]   SerialPortsLib.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SerialPortsLib[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ports = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] SerialPortsLib.[/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]SerialPortsLib[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]();
    [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]   // the above line automaticaly disposes the created ports referance when
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]   // the functon completes[/COLOR][/SIZE][/COLOR][/SIZE]
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000] 
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]   // call Heaters code
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]   ports.testSerialPort(cboPortList.SelectedText);
    }
    [/SIZE]
    

    That should work the same way and keep the detection functions seperate from the actual access functions in the main form.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 15:23
    Jorge P
    should I automaticaly assume that all versions of *NUX uses /dev/ for devices

    Probably, not sure, I would never do that.

    Thing is under MSDOS and up there were devices COM1: COM2: etc for serial ports A: B: C. etc for disks and some others I forget. All inherited from good old CP/M. So you could probably be safe specifying just a number for com ports.

    Under Unix there is the idea that everything is a file. The top of the file system is "/". If you have more than one disk the others get mounted on directories under "/" somewhere. If you have serial ports, terminals, parallel ports etc they appear as files under "/dev". But as far as I can tell there is no reason not to have device nodes like that created elsewhere.

    So I would always specify ports and other devices with their full path name. Which seems to be what happens in most Unixy programs.
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 15:41
    Sorry, didn't debug before posting the above code, the event handles should read
    [SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]// call Heaters code
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]ports.testSerialPort([/SIZE][SIZE=2][COLOR=#2b91af][SIZE=2][COLOR=#2b91af]Convert[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].ToString(cboPortList.SelectedItem));
    [/SIZE]
    
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 16:26
    Jorge P,

    Ha, just turned back to this thread to say it did not work and there you have the fix:)

    Result: It works just fine. Hit "Get Serial Ports", select "/dev/tty/USB0" from the combo box, hit "Connect" and my Prop Plug lights up.

    Problem: monodevelop doesn't seem to give me any way to edit frmMain in a GUI. I had to hack the new button into the code by hand.

    In the monodevelop "hello world" I made with the GUI editor I ended up with a mainwindow sub classed from a gtkwindow or such.

    No idea how to deal with Forms here. Perhaps I have missed a point somewhere.
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 17:32
    Heater. wrote: »
    Problem: monodevelop doesn't seem to give me any way to edit frmMain in a GUI. I had to hack the new button into the code by hand.

    In the monodevelop "hello world" I made with the GUI editor I ended up with a mainwindow sub classed from a gtkwindow or such.

    No idea how to deal with Forms here. Perhaps I have missed a point somewhere.

    I am updating the project files to use a form with all the selectable options, like baud, handshaking, etc... Modified your code so none of the selectable values are hard coded, including the text. Working on it as we speak.... maybee an hour or more. Will update as soon as I'm finished.

    I will download Monodevelop tonight and give it a try to see what I can figure out for you...
  • Jorge PJorge P Posts: 385
    edited 2011-05-08 19:35
    Finaly got through that, the update, zip, is now called Project_2, Thought I'd keep it seperate from the first file since we are now communicating with the code. Form looks alot different for all the options.

    It took me a little longer due to the fact that I had to figure out how to add enums to a listbox and back, I think it can be cleaned up a bit in that respect.

    I will now download and install mono and monodevelop to see what others are using and maybee working with GTK if the need arises.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-08 23:58
    Jorge P,

    Nice looking form, better still it works as advertised:)

    Not as sexy looking as something one might get out of Qt. Perhaps that even doable with some extra work.

    I probably don't have much time for C# until next week end but that was a good intro as to what is possible now a days. It's been a few years since my last dabble with mono on Linux and there was no monodevelop or much GUI support back then.
  • jazzedjazzed Posts: 11,803
    edited 2011-05-09 00:30
    Heater. wrote: »
    I probably don't have much time for C# until next week end but that was a good intro as to what is possible now a days.
    Did you get a chance to receive any chars from the serial port?

    Here's a .net command line serial port console program I've been using for a couple of months on Windoze CMD shell.
    Give it a try if you can. I doubt it will work with mono without changes, but I could be wrong.

    Usage: uterm <port> <baud>
    (press escape to exit).


    The reason I like this is because I can edit, compile, run, interact, etc... all in one window.

    I use a batch file like this to do things:
    bstc -d COM11 -p0 -Ograux -L c:\bstc\spin %1
    uterm COM11 115200
    
  • Heater.Heater. Posts: 21,230
    edited 2011-05-09 00:34
    Jazzed,
    Did you get a chance to receive any chars from the serial port?

    Oh man, I was up all night tinkering with that in the dark because I had a guest sleeping on the couch.
    I'll see what I can do after a days work and snooze:)
  • Heater.Heater. Posts: 21,230
    edited 2011-05-09 01:44
    Something is up with that "connect" button.
    1) Mouse over button
    2) Click
    3) Prop Plug shows data going out.
    4) Press again
    5) Nothing happens
    6) Press again, data goes out twice!

    Or when pressing the button and nothing happens move the mouse away from the button and then it happens!

    Correction: Just moving the mouse a tiny bit causes the click event to come through.
  • Jorge PJorge P Posts: 385
    edited 2011-05-09 03:02
    @Heater,

    I just downloaded and installed GTK# and MonoDevelop, it seems you need to download http://www.mono-project.com/WinForms_Designer to get the windows forms designer. I dont know how big it is or how long it takes. I don't have SVN to download the files yet.
    Something is up with that "connect" button

    I noticed that, it is because I haven't used threading yet. I accidently called the test with a baud of 100 and had to wait forever for it to finish. I will update the project to use threading here in a moment and repost Project_2.

    For Project_3 - I think I will make a new Form from scratch and set it as a container to hold other forms, with threading enabled. I will make an options menu and a settings form with all the settings. I will try and make some read/write functions that send and recieve data. If all goes well, I will try to set a test form to allow a prop to check a checkbox by sending a sequence of char's from the prop.

    I will try to populate the menu bar, with things I think will be needed and just through a "Not Impliment Exception" untill this starts to move along more. I would like to download a java based software breadboard that is open sourced and see if I can convert it to C#. I may need help with that, but then again maybee not. I'll save that for Project 4 or 5.

    Remember, if anyone makes some .NET libs/dll's to perform some wizardry, I dont see why we couldnt add it to these projects. I know I will not be much help with a compiler or Parser since I've always managed to avoid learning how to do it so .NET lib's in that respect will be needed. NOTE: all functional lib's will/should be kept as seperate projects within the solution to allow it to be updated independantly from everything else.

    Should I sourceforge all this under MIT License??? I never realy used SVN on sf.net to upload changes nor do I know what it entails... Or should I use something like www.codeproject.com for this? Any preferances? What should I call it?

    I would appreciate any feedback from everyone since I don't think I should be hogging all this as my own projects. Just thought I'd throw out a few samples... now my mind is racing.
  • Jorge PJorge P Posts: 385
    edited 2011-05-09 04:27
    Heater. wrote: »
    Correction: Just moving the mouse a tiny bit causes the click event to come through.

    Here is an updated Project_2 file, I didn't get the threading going yet but the button should now only accept a single click until the function called finishes.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-09 04:37
    No. I don't think threading is the issue here.
    I hit the button it transmits, causing my PropPlug to light up. That comes to an end quickly and I click the button again. No go. Wiggle the mouse a few pixels and the event fires and the Prop Plug lights up again. Seems to be an issue with the that button on mono.

    Of course uncoupling the UI from the serial port activity with threads is always desirable to stop things freezing up.
  • Jorge PJorge P Posts: 385
    edited 2011-05-09 04:52
    Heater. wrote: »
    Of course uncoupling the UI from the serial port activity with threads is always desirable to stop things freezing up.

    I think it would allow access to multiple ports too.

    I won't be able to do any more till I get another day off or some more time to readup on threading again. Readup on things like System.IO.Ports or whaterver on MSDN if you havn't already. I am not sure yet how MonoDevelop is with the documentation on .NET.
  • Heater.Heater. Posts: 21,230
    edited 2011-05-09 05:04
    MSDN is exactly where I went to get that serial port working. I feel kind of dirty having to get all my info from a MicroSoft web site:)

    I did play with C# threading some years back. It went quite easily for a console app at least.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-05-09 12:24
    There's a good article here on C# threading: http://www.albahari.com

    For anybody interested in C#, the "C# in a NUtshell" books by the above autors are very good. The threading article is from the book.
  • Jorge PJorge P Posts: 385
    edited 2011-05-09 12:36
    Thanks for the link, I was having problems passing data to heaters test function so this section, http://www.albahari.com/threading/#_Passing_Data_to_a_Thread helps out alot. Thanks for posting that.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-05-09 13:53
    No problem.
    Previews of the C# nutshell books on google books...

    C# 3.0: http://oreilly.com/catalog/9780596527570/preview#preview

    C# 4.0: http://oreilly.com/catalog/9780596800963/preview#preview
  • Heater.Heater. Posts: 21,230
    edited 2011-05-09 19:44
    Jazzed,

    Can't build uterm here on Debian.

    Error: "Framework 'Mono / .NET 4.0' not installed"

    Which is Smile of course, if I cut and paste the code into an new console project it compiles just fine.

    Does not work though:

    $ ./uterm2.exe
    Usage: uterm <port> <baud>
    $ ./uterm2.exe /dev/ttyUSB0/ 115200
    Can't open /dev/ttyUSB0/.
Sign In or Register to comment.