Running .NET Core on Raspberry Pi 3B+

Introduction

I don’t know why, but seeing .NET Core makes me feel great; it can truly run cross-platform, and I personally really like it. So, I decided to use a Raspberry Pi to validate my idea. At the time of writing this article, I had just installed core 2.1 on the Raspberry Pi, so let’s start with the results.

Running .NET Core on Raspberry Pi 3B+

1. Basic Preparation

It’s been three days since I got the board… I’ve gone through many pitfalls. I initially thought I could install Windows 10 IoT Core, but it seems that 3B+ is not yet supported. There were some issues, so I chose the official system, Raspbian.

Running .NET Core on Raspberry Pi 3B+

Prepare a monitor (only VGA), HDMI to VGA adapter, mouse, keyboard, and most importantly: an SD card. I used a SanDisk 64G SD card from an old phone, and I bought a card reader for 5 yuan.

Tool preparation link: http://shumeipai.nxez.com/download#os

Okay, once prepared, the next step is to install the system. Installing the system is also fraught with pitfalls. First, download the system and write it to the SD card. There were issues like the SD card not writing, verification errors, formatting the SD card and starting over, writing, failing, formatting, and writing again. This repeated about a dozen times… Finally, the SD card was written successfully, I put it into the board and powered it on, but it didn’t boot. Thinking it was a system issue, I redid it. The power supply was also an issue; initially, plugging it into USB prompted low voltage. I switched the data cable and then changed the power adapter. A 5V2A adapter was more stable and did not prompt low voltage anymore.

Running .NET Core on Raspberry Pi 3B+

Finally, the system was installed successfully, and I saw the Raspberry Pi running interface (the screenshot later was taken remotely, not using the monitor). I felt a great sense of accomplishment since it was my first time doing this.

Running .NET Core on Raspberry Pi 3B+

Next, let’s connect to the internet. Yes, that’s right, the 5G wifi works great.

2. Environment Preparation

What should we prepare? Of course, we need to prepare the core environment.

1. First, change the source to a domestic source. I chose Tsinghua’s, just randomly selected. Reference link: http://shumeipai.nxez.com/2013/08/31/raspbian-chinese-software-source.html

2. Remote login: Every time I have to find a monitor, then plug in the mouse and keyboard, which is troublesome, especially since I usually use a laptop, and the keyboard is old and dusty. I chose two remote methods: Windows remote login and SSH.

1) Windows remote: sudo apt-get install xrdp, then open remote desktop access on the Windows side. The default account is pi, password is raspberry.

2) SSH: sudo raspi-config to enter the configuration interface.

Running .NET Core on Raspberry Pi 3B+

Enter Interfacing Options.

Running .NET Core on Raspberry Pi 3B+

Then enable SSH and confirm.

Back to the Windows system, install Putty, enter the IP, and log in.

3. Install the .NET Core 2.1 environment.

1) Update the source: sudo apt-get update

2) Install dependencies: sudo apt-get install curl libunwind8 gettext

3) Download and install SDK or Runtime: Official address: https://www.microsoft.com/net/download/linux

Or execute directly: curl -sSL -o dotnet.tar.gz https://download.microsoft.com/download/1/f/7/1f7755c5-934d-4638-b89f-1f4ffa5afe89/dotnet-runtime-2.1.2-linux-arm.tar.gz

Running .NET Core on Raspberry Pi 3B+

4) Extract contents: sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet

5) Set executable permissions for DotNet: sudo ln -s /opt/dotnet/dotnet /usr/local/bin

6) Test: dotnet –help or dotnet –info. If successful, it will look like the first image I showed.

Running .NET Core on Raspberry Pi 3B+

Reference link for core environment installation: https://www.cnblogs.com/magicbowie/p/9320737.html

Note: I encountered a pitfall in the middle. I unplugged the adapter and monitor and powered on the Raspberry Pi, which automatically connected to wifi. At this point, due to DHCP, I didn’t know the Raspberry Pi’s IP address and couldn’t access it remotely. What should I do?

1) Connect the Raspberry Pi to the PC via Ethernet, set a fixed IP on the Raspberry Pi, and then connect via Ethernet.

2) Query the Raspberry Pi’s automatically assigned IP.

[1] If you have the router password, you can log in to the router to check.

[2] If you don’t have the router password and can’t log in, what should you do? Use IP scanning… then detect the IP. I tested it on the company network, and there were dozens of devices, making it hard to distinguish. First, power off the Raspberry Pi, scan, save the results, then power on the Raspberry Pi, connect to wifi, scan again… and then guess.

3. Developing Programs

To avoid scanning the IP every time (as mentioned above), the Raspberry Pi connects to wifi, and the Ethernet connects to the computer, which needs to be on the same subnet. So, the Raspberry Pi needs to be set with a static IP address to avoid IP issues.

Upload files: The Raspberry Pi has SFTP, which can be used directly with the FileZilla client.

Program: .NET Core program, compile using dotnet publish -r linux-arm in the program directory.

Currently tested, the program runs normally, and I also tested the Modbus slave program, which runs normally!!! Detailed information will be organized and added later…

Original article link:https://www.cnblogs.com/Iamsorry/p/9372910.html

.NET Community News, in-depth articles, welcome to visit the public account article collection http://www.csharpkit.com

Leave a Comment

Your email address will not be published. Required fields are marked *