99精品伊人亚洲|最近国产中文炮友|九草在线视频支援|AV网站大全最新|美女黄片免费观看|国产精品资源视频|精彩无码视频一区|91大神在线后入|伊人终合在线播放|久草综合久久中文

電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>使用System IO Ports SerialPort進(jìn)行串行讀/寫

使用System IO Ports SerialPort進(jìn)行串行讀/寫

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

資料介紹

描述

描述

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

筆記

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

開發(fā)設(shè)置

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

?
poYBAGN2-JmAKHkwAAAYnfElk1Q136.png
?

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

3. 在樹莓派上安裝.NET Core。如果你想在開發(fā)機(jī)器上構(gòu)建 C# 代碼并在 PI 上運行二進(jìn)制文件,你只需要安裝.NET Runtime. 如果要在 PI 上構(gòu)建和運行源代碼,則需要安裝.NET SDK其中還包括 .Net Runtime。請注意,您應(yīng)該linux arm32為您的 PI 使用構(gòu)建。為簡單起見,我將向您展示如何在 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 進(jìn)行驗證。你應(yīng)該看到這樣的安裝信息

?
poYBAGN2-JyAMGr3AAC8iO_mAlo628.png
?

4. 準(zhǔn)備任何啟用串行功能的設(shè)備以接收和發(fā)送串行消息。對我來說,這是一個 Arduino Uno。

你好串行端口

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

在您的開發(fā)機(jī)器上,使用以下命令啟動一個 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文件,將內(nèi)容替換為以下代碼:

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以在開發(fā)機(jī)器上運行代碼。

?
poYBAGN2-J6ALskGAAAtSlUGISE364.png
?

要為 RPi 構(gòu)建項目,請運行:

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

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

chmod +x hello-serialport
./hello-serialport

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

?
poYBAGN2-KCAAH-FAAAkGaodXZo036.png
?

.NET Core 應(yīng)用程序部署

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

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

串行讀取

打開 Arduino IDE,轉(zhuǎn)到 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);
}

該程序不斷發(fā)出模擬引腳的讀數(shù)。讓我們編寫一個 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 上構(gòu)建并運行:

?
poYBAGN2-KKAZE3rAAEg4pN-vg0324.png
?

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

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

進(jìn)一步的工作

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

參考

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


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

評論

查看更多

下載排行

本周

  1. 1DD3118電路圖紙資料
  2. 0.08 MB   |  1次下載  |  免費
  3. 2AD庫封裝庫安裝教程
  4. 0.49 MB   |  1次下載  |  免費
  5. 3PC6206 300mA低功耗低壓差線性穩(wěn)壓器中文資料
  6. 1.12 MB   |  1次下載  |  免費
  7. 4網(wǎng)絡(luò)安全從業(yè)者入門指南
  8. 2.91 MB   |  1次下載  |  免費
  9. 5DS-CS3A P00-CN-V3
  10. 618.05 KB  |  1次下載  |  免費
  11. 6海川SM5701規(guī)格書
  12. 1.48 MB  |  次下載  |  免費
  13. 7H20PR5電磁爐IGBT功率管規(guī)格書
  14. 1.68 MB   |  次下載  |  1 積分
  15. 8IP防護(hù)等級說明
  16. 0.08 MB   |  次下載  |  免費

本月

  1. 1貼片三極管上的印字與真實名稱的對照表詳細(xì)說明
  2. 0.50 MB   |  103次下載  |  1 積分
  3. 2涂鴉各WiFi模塊原理圖加PCB封裝
  4. 11.75 MB   |  89次下載  |  1 積分
  5. 3錦銳科技CA51F2 SDK開發(fā)包
  6. 24.06 MB   |  43次下載  |  1 積分
  7. 4錦銳CA51F005 SDK開發(fā)包
  8. 19.47 MB   |  19次下載  |  1 積分
  9. 5PCB的EMC設(shè)計指南
  10. 2.47 MB   |  16次下載  |  1 積分
  11. 6HC05藍(lán)牙原理圖加PCB
  12. 15.76 MB   |  13次下載  |  1 積分
  13. 7802.11_Wireless_Networks
  14. 4.17 MB   |  12次下載  |  免費
  15. 8蘋果iphone 11電路原理圖
  16. 4.98 MB   |  6次下載  |  2 積分

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935127次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關(guān)降壓/升壓雙向直流/直流轉(zhuǎn)換器 PCB layout 設(shè)計
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233089次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191390次下載  |  10 積分
  9. 5十天學(xué)會AVR單片機(jī)與C語言視頻教程 下載
  10. 158M  |  183342次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81588次下載  |  10 積分
  13. 7Keil工具M(jìn)DK-Arm免費下載
  14. 0.02 MB  |  73815次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65989次下載  |  10 積分