吴忠躺衫网络科技有限公司

電子發燒友App

硬聲App

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示
電子發燒友網>電子資料下載>電子資料>使用System IO Ports SerialPort進行串行讀/寫

使用System IO Ports SerialPort進行串行讀/寫

2022-11-21 | zip | 0.19 MB | 次下載 | 免費

資料介紹

描述

描述

.NET Core 3.0 剛剛在 2019 年 1 月發布了預覽版。正如微軟在他們的文檔中所說,Linux 現在支持 System.IO.Ports.SerialPort。我迫不及待地想用它弄臟我的手。在本文中,我將向您展示如何使用 System.IO.Ports.SerialPort 進行串行讀/寫,以及如何在 Windows 上構建源代碼并在 linux-arm (Raspbian) 上運行二進制文件。

筆記

  • 在我寫這篇文章的時候,.NET CoreSystem.IO.Ports已經處于預覽階段,當你閱讀這篇文章時,請檢查是否有任何新版本。使用最新的穩定版本運行您的代碼。
  • 我在 Windows 10 上構建和測試本文的代碼,并在 Raspberry Pi Raspbian 中運行它們。如果您使用 Mac/Linux 作為開發機器,SerialPort 庫也應該可以工作

開發設置

1. 在您的開發機器上下載并安裝.NET Core 3.0 SDK(非運行時)。安裝后,打開終端,輸入dotnet --version. 您應該會看到像 3.0.x 這樣的 dotnet 版本。

?
poYBAGN2-JmAKHkwAAAYnfElk1Q136.png
?

2. 安裝Visual Studio Code作為 C# 代碼編輯器。然后安裝C# 擴展

3. 在樹莓派上安裝.NET Core。如果你想在開發機器上構建 C# 代碼并在 PI 上運行二進制文件,你只需要安裝.NET Runtime. 如果要在 PI 上構建和運行源代碼,則需要安裝.NET SDK其中還包括 .Net Runtime。請注意,您應該linux arm32為您的 PI 使用構建。為簡單起見,我將向您展示如何在 PI 上安裝 .NET Core SDK:

# in raspberry pi terminal
sudo apt-get update
# install .net core dependencies
sudo apt-get install curl libunwind8 gettext
cd ~
# download .net core 3.0
wget 
mkdir -p $HOME/dotnet && tar vzxf dotnet-sdk-3.0.100-preview-010184-linux-arm.tar.gz -C $HOME/dotnet
echo "export PATH=$PATH:$HOME/dotnet" >> ~/.bashrc
echo "export DOTNET_ROOT=$HOME/dotnet" >> ~/.bashrc
export PATH=$PATH:$HOME/dotnet
export DOTNET_ROOT=$HOME/dotnet

安裝后,使用 dotnet --info 進行驗證。你應該看到這樣的安裝信息

?
poYBAGN2-JyAMGr3AAC8iO_mAlo628.png
?

4. 準備任何啟用串行功能的設備以接收和發送串行消息。對我來說,這是一個 Arduino Uno。

你好串行端口

設置好開發工具后,讓我們從一個簡單的 C# 項目開始我們的旅程,該項目將打印所有可用的串行端口。

在您的開發機器上,使用以下命令啟動一個 dotnet 項目:

mkdir hello-serialport && cd hello-serialport
dotnet new console
dotnet add package System.IO.Ports --version 4.6.0-preview.19073.11

打開program.cs文件,將內容替換為以下代碼:

using System;
using System.IO.Ports;
namespace hello_serialport
{ 
   class Program    {        
      static void Main(string[] args)        {             // Get a list of serial port names.             string[] ports = SerialPort.GetPortNames();             Console.WriteLine("The following serial ports were found:");             // Display each port name to the console.             foreach(string port in ports)             {                 Console.WriteLine(port);             }             Console.ReadLine();        }    }}

鍵入dotnet run以在開發機器上運行代碼。

?
poYBAGN2-J6ALskGAAAtSlUGISE364.png
?

要為 RPi 構建項目,請運行:

dotnet publish -r linux-arm --self-contained false

然后轉到{your_project_root}\bin\Debug\netcoreapp3.0\linux-arm,將文件夾復制publish 到您的 PI。在 Pi 上,轉到發布文件夾,運行:

chmod +x hello-serialport
./hello-serialport

您的 hello-serialport 正在 Rapsberry Pi 上運行!

?
poYBAGN2-KCAAH-FAAAkGaodXZo036.png
?

.NET Core 應用程序部署

在 hello serialport 項目中,我們在 windows 上進行開發,構建 linux-arm 二進制文件,然后在 raspberry pi 上運行二進制文件。根據微軟的文檔,我們剛剛制作了一個依賴于框架的可執行文件(FDE),這意味著該可執行文件只能在安裝了正確版本的 .NET Core Runtime 的樹莓派上運行。

您可以通過參考以下文檔來玩不同類型的部署:

串行讀取

打開 Arduino IDE,轉到 File-->Examples-->03.Analog-->AnalogInOutSerial 并將其上傳到 Arduino。在此處粘貼代碼:

/*
 Analog input, analog output, serial output
 Reads an analog input pin, maps the result to a range from 0 to 255 and uses
 the result to set the pulse width modulation (PWM) of an output pin.
 Also prints the results to the Serial Monitor.
 The circuit:
 - potentiometer connected to analog pin 0.
   Center pin of the potentiometer goes to the analog pin.
   side pins of the potentiometer go to +5V and ground
 - LED connected from digital pin 9 to ground
 created 29 Dec. 2008
 modified 9 Apr 2012
 by Tom Igoe
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/AnalogInOutSerial
*/
  
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
void setup() {
 // initialize serial communications at 9600 bps:
 Serial.begin(9600);
}
void loop() {
 // read the analog in value:
 sensorValue = analogRead(analogInPin);
 // map it to the range of the analog out:
 outputValue = map(sensorValue, 0, 1023, 0, 255);
 // change the analog out value:
 analogWrite(analogOutPin, outputValue);
 // print the results to the Serial Monitor:
 Serial.print("sensor = ");
 Serial.print(sensorValue);
 Serial.print("\t output = ");
 Serial.println(outputValue);
 // wait 2 milliseconds before the next loop for the analog-to-digital
 // converter to settle after the last reading:
 delay(2);
}

該程序不斷發出模擬引腳的讀數。讓我們編寫一個 C# 程序來讀取消息:

using System;
using System.IO.Ports;
namespace serial_read
{
class Program
{
  static SerialPort _serialPort;
  static void Main(string[] args)
  {
    Console.Write("Port no: ");
    string port = Console.ReadLine();
    Console.Write("baudrate: ");
    string baudrate = Console.ReadLine();
    // Create a new SerialPort on port COM7
    _serialPort = new SerialPort(port, int.Parse(baudrate));
    // Set the read/write timeouts
    _serialPort.ReadTimeout = 1500;
    _serialPort.WriteTimeout = 1500;
    _serialPort.Open();
    while (true)
    {
      Read();
    }
    _serialPort.Close();
  }

  public static void Read()
  {
    try
    {
      string message = _serialPort.ReadLine();
      Console.WriteLine(message);
    }
    catch (TimeoutException) { }
  }
}
}

在 Raspberry Pi 上構建并運行:

?
poYBAGN2-KKAZE3rAAEg4pN-vg0324.png
?

值得一提的是 SerialPort.ReadLine() 是一種阻塞方法。如果您不希望主線程被阻塞,請使用多線程。

請參考微軟提供的例子來學習如何進行串行寫入和多線程。系列活動也是值得探索的好東西。

進一步的工作

  • ASP.NET Core 從 .NET Core v1 開始可用。通過結合 SerialPort API 和 ASP.NET,我們可以構建一個 Web UI 來控制一些設備,比如移動機器人
  • Microsoft 開源了WPFWinForms ,它們都將從 .NET Core 3.0 開始提供。有一天,我們可以安全地將舊的 Windows 桌面串行應用程序移植到所有平臺,甚至可以在 Raspberry Pi 上編寫一個 winForm 串行通信應用程序!

參考

[1] 在 Raspberry Pi 上安裝 .NET Core 2.x SDK 并使用 System.Device.Gpio 閃爍 LED。


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數據手冊
  2. 1.06 MB  |  532次下載  |  免費
  3. 2RK3399完整板原理圖(支持平板,盒子VR)
  4. 3.28 MB  |  339次下載  |  免費
  5. 3TC358743XBG評估板參考手冊
  6. 1.36 MB  |  330次下載  |  免費
  7. 4DFM軟件使用教程
  8. 0.84 MB  |  295次下載  |  免費
  9. 5元宇宙深度解析—未來的未來-風口還是泡沫
  10. 6.40 MB  |  227次下載  |  免費
  11. 6迪文DGUS開發指南
  12. 31.67 MB  |  194次下載  |  免費
  13. 7元宇宙底層硬件系列報告
  14. 13.42 MB  |  182次下載  |  免費
  15. 8FP5207XR-G1中文應用手冊
  16. 1.09 MB  |  178次下載  |  免費

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234315次下載  |  免費
  3. 2555集成電路應用800例(新編版)
  4. 0.00 MB  |  33566次下載  |  免費
  5. 3接口電路圖大全
  6. 未知  |  30323次下載  |  免費
  7. 4開關電源設計實例指南
  8. 未知  |  21549次下載  |  免費
  9. 5電氣工程師手冊免費下載(新編第二版pdf電子書)
  10. 0.00 MB  |  15349次下載  |  免費
  11. 6數字電路基礎pdf(下載)
  12. 未知  |  13750次下載  |  免費
  13. 7電子制作實例集錦 下載
  14. 未知  |  8113次下載  |  免費
  15. 8《LED驅動電路設計》 溫德爾著
  16. 0.00 MB  |  6656次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉中文版)
  4. 78.1 MB  |  537798次下載  |  免費
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費
  11. 6電路仿真軟件multisim 10.0免費下載
  12. 340992  |  191187次下載  |  免費
  13. 7十天學會AVR單片機與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138040次下載  |  免費
天将娱乐城开户| 百家乐官网台布21点| 百家乐官网投法| 万龙百家乐的玩法技巧和规则| 海阳市| 网上百家乐官网赌博经历| 威尼斯人娱乐城安全吗| 网上百家乐官网官方网站| 宝马会百家乐现金网| 大发888游戏平台 df888ylc3403 | 百家乐官网建材| 棋牌百家乐有稳赚的方法吗 | 网上百家乐官网娱乐平台| 如何玩百家乐的玩法技巧和规则| 手游| 澳门百家乐的故事| 克拉克百家乐的玩法技巧和规则| 钟山县| 网上百家乐官网试玩网址| 青鹏棋牌游戏大厅v3.0| 涂山百家乐官网的玩法技巧和规则 | 浑源县| 百家乐群html| 百家乐官网翻天qvod| 百家乐制胜法宝| 有百家乐官网的游戏平台| 庄闲和百家乐桌布| 百家乐官网赌博娱乐城大全| 全讯网财运| 百家乐官网单机游戏下| 大发8880634| 立博百家乐官网的玩法技巧和规则| 皇冠现金网导航| 做生意门朝哪个方向好| 百樂坊娱乐| 澳门百家乐走势图怎么看| 百家乐官网视频看不到| 扑克王百家乐的玩法技巧和规则 | 百家乐技巧介绍| 大赢家百家乐官网66| 玩百家乐怎么能赢呢|