Serial Programming using Mono and C sharp (C#) on Linux
Mono is a free and opensource equivalent to the Microsoft's .Net Framework platform developed by Xamarin which can be used to run programs using the .Net framework on Linux and other Unix like systems.Mono Framework can be used to write cross platform programs that would run on both Windows and Linux like OS's with minimal or no change to the source code.
In this tutorial we will learn how to install a mono runtime and compiler on a Linux System (I am using Ubuntu here),Compile a simple hello world program and communicate with the external world ie a microcontroller board through serial port using a USB to Serial Converter (USB2SERIAL).
The Mono Project consists of an ECMA complaint Common Language Runtime called Mono Runtime for running the executables and a C# compiler for compiling your .CS files into executables called Mono Csharp Compiler or mcs.
All the CSharp (C#) source files used in this tutorial can be downloaded from our GitHub Page.
If you are new to Github Check this article to download code .
USB2SERIAL - Any easy to use multi protocol converter that can convert from USB to RS485/RS232/Serial.The Board comes with selectable voltage levels for interfacing with 5V and 3.3V logic families.
On most Linux systems mono is not installed by default, you can check whether mono is installed by typing mono on your terminal.If you are getting a response as shown below, mono is not installed on your system.
Mono runtime and compiler is available on Ubuntu's software repositories so you can directly install them from Ubuntu Software center.Just click on the Mono/CLI icon and install the two runtimes
You can also install the complete Mono framework using command line.
For Serial Programming it is recommended to install the complete Mono runtime and libraries.Open up your terminal window and type
$ sudo apt-get install mono-complete
provide the password for your account to install mono as shown in the figure below.This will install the complete Mono runtime and avoid any dependency issues.
After Mono is installed on your system,you can check them using the whereis command or by just typing mono or mcs on the terminal.
Now lets compile a simple "Hello World" program using mcs and run it on the mono runtime.Open up a text editor and type the following code in it.
Now save the file as "HelloMono.cs" on your home directory (or an other directory) and compile it by typing
$ mcs HelloMono.cs
if there are no errors the HelloMono.cs file will be compiled into a HelloMono.exe file, which you can view by using the ls command (as shown in the below figure).
You can then run the resulting exe file using the mono runtime by typing
$ mono HelloMono.exe
on the terminal.
In the above example we have used command line tools to compile and run the C# file ,If you like a proper IDE mono project also develops MonoDevelop which comes with features like automatic code completion, source control, a graphical user interface (GUI) and Web designer.It also supports several languages like Boo, C, C++, C#,F#,Java,Python,and Visual Basic.NET.
MonoDevelop on Windows 7
Now lets go to the main course,programming your serial port using Mono.If you have any experience building embedded systems you may have encountered situations where you want to communicate with an external microcontroller board to log data to your PC or send AT Commands to a GSM modem.
The easiest way to achieve that is to send your data through a Serial Port,Even though hardware serial ports are being replaced with USB ports, you can find cheap USB to Serial converters based on FTDI FT232 like USB2SERIAL for doing the conversion.
In Linux serial ports are named as ttyS1,ttyS2 for hardware ports and ttyUSB0 (FTDI based) or ttyACM0 (TUSB3410 based) for USB to Serial Converters,the port numbers may vary on your system.You can check this tutorial for finding out your serial port number.
Now lets write a small C# program to open up a connection with your Serial Port (Hardware Port or USB to Serial Converter).Open up a text editor,type out the code shown below and save it as "MonoSerial.cs" on your harddisk.
Serial_tty.PortName = "/dev/ttyUSB0"; //Assign the port name,
Please note that ttyUSB0 is the name of the USB to Serial converter chip detected by my system,Find out the name of you serial port and substitute that instead of ttyUSB0 in the above code before compiling . After saving the file ,compile it using mcs.On successful compilation it would create an executable named as " MonoSerial.exe " .
In Linux access to the serial port is only available to the root user ,so you will have to run the exe file with root privileges using "sudo" command as shown below.
$ sudo mono MonoSerial.exe
Now give the password to your system so that the code can run ,if all goes well it will print
" Serial Port /dev/ttyUSB0 Opened " on the terminal as shown in the below figure.
After we have opened a connection to the Serialport,let's write some characters to the serial port.Open up a text editor and type the following code to it ,save the file as "MonoSerialWrite.cs".
Connect a microcontroller board to your serial port using null modem connection in order to receive the transmitted character.
In Null modem connection TX pin of PC serialport is connected to the RX pin of Microcontroller and vice versa as shown in the below figure.
In case you don't have a hardware serial port use a USB to Serial Converter like USB2SERIAL.USB2SERIAL can also be used as an USB to RS485 converter or as an FT232 development Board.The Board can be purchased online from ebay.
Compile the code using mcs compiler and run it using mono runtime .Please remember to run your executable as root using sudo command.Screenshot shown below.
When the data is transmited the TX/RX LED on the USB2SERIAL board will blink indicating that a byte has been transmitted.Now on the microcontroller side you can write code to take some action like lighting up an LED or turning on a motor.
Here we will receive the data send from a microcontroller board(Transmitter) to the Linux PC through the serial port.The Microcontroller board (Here I am using MSP430 Launchpad from TI) will send a string "Hello from MSP430" serialy to the PC.You can use any microcontroller like 8051,PIC,ARM,STM32 etc instead of MSP430 on the transmitter side.
The code for receiving the data send from the microcontroller is shown below.Here we are using Console.ReadLine() function to read the data send from the MSP430.Please note that the timeout's for the serial port are not set so the program will wait indefinitely till the data is received.
Compile the program and run it.Enter the name of your serial port and the program will wait for data to be transmitted by the microcontroller as shown in the below figure.
Reset your microcontroller to send the string "Hello from MSP430".
The program will then print out the received string on the console window.
One thing to note that, is Console.ReadLine() will return only after encountering the end of line character "\n".Please make sure that the microcontroller sends the "\n" character otherwise the Console.ReadLine() will never return and program will hang.
Controlling the RTS and DTR pins can be accomplished by using RtsEnable and DtrEnable properties of the SerialPort object.
Serial_tty.Close(); // Close port
One use of RTS and DTR pins is used to control the RS485 chips during a USB to RS485 conversion.When you have to control some thing over several 100's of meters ,RS232 or USB will not be suitable.In such cases we have to use RS485 protocol, one problem with RS485 is that most PC's / Laptops will not have an RS485 port.You can solve that problem by using USB to RS485 converters like USB2SERIAL .In USB2SERIAL RTS pin is used to put the RS485 chip in receive mode and DTR pin is used to put theRS485 chip in transmit mode.
Please note that in FT232 based USB to Serial converters (like USB2SERIAL) the RTS and DTR pins are inverted internally so setting the pins high will make then low and vice versa.
Connect a couple of LED's to Visualize the logic levels of RTS and DTR pins of USB2SERIAL.
You can check out our other serial programming tutorials here.