Operating environment: Visual Studio 2017
How to achieve embedded development using C#?
Although .NET Core allows for cross-platform development, it is not feasible to use C# everywhere, just as no one uses SQL to develop Android apps. Each language has its strengths and limitations.
For sensors, 32-bit, or 16-bit embedded devices with possibly only a few KB of memory, serial ports, pins, circuits, and signals, they can only be developed using C language. Speaking of Linux, it is developed in C language, which provides rich interface support and compatibility. Many places can only use C language.
Bottom layer applications can be accomplished using C language. But what if we need to develop a website or a large application?
We can first write the underlying API interface in C, generate a .SO dynamic link library, and then use C# to import the C dynamic link library. By developing the bottom layer interfaces in C and using .NET Core/C# to implement cross-platform application development, we can have control over the entire system through pre-defined APIs.
1. Install C/C++ support on Visual Studio 2017
This step is to set up a development environment in VS 2017, but you can also use other tools to develop C programs or even use vim in Linux to write and compile.
2. Write a simple C program and generate a dynamic link library
After creating the project, click on properties and choose to generate .so.
Then create a new test.cpp file, right-click - Add New Item, the C code is as follows
extern "C" {}
It indicates that the methods and functions inside the braces can be used externally.
extern "C" { int Sum(int a,int b) { return a + b; } }
Right-click - Build
It will then prompt you to add a Linux host connection. This step is actually unnecessary, but VS requires it to build and compile; it does not affect our subsequent operations.
Next, follow the instructions on the screen
Click on the menu bar Build - Only Build Project - Only Link xxx
Then the output window will show errors; ignore them, and then right-click on the project and build again.
Afterward, a .so file will be generated; copy the .so file and store it properly.
3. Use this dynamic link library in a C# project
Create a .NET Core project, copy the .so file into it, and remember to set it to "Copy Always."
Create a new class; the class name is not important, but the method name and parameters must match.
Add the following at the top:
using System.Runtime.InteropServices;
The class code is as follows:
Then use this class library:
public class Test { [DllImport("./libProject2.so",CallingConvention =CallingConvention.Cdecl)] public static extern int Sum(int a,int b); } class Program { static void Main(string[] args) { Console.WriteLine(Test.Sum(666,666)); while (true) { Console.WriteLine("Enter two numbers, separated by space, e.g., 666 666"); string[] vs = Console.ReadLine().Split(" "); int a = Convert.ToInt32(vs[0]); int b = Convert.ToInt32(vs[1]); Console.WriteLine(Test.Sum(a,b)); } } }
After publishing, package it and run it in Linux.
The author is currently interning, learning about IoT, embedded Linux, cloud computing, and .NET Core.
The above is just a simple example of using C language projects within a C# project. The author has implemented a .NET Core development project to manage devices and integrate with Alibaba Cloud IoT, and is in the process of writing an article on it.
文章评论