2024年12月1日日曜日

Visual Studio C# Arduino LED

Visual Studio 2022 Community C# Windowsアプリを作成してArduinoNano LED制御を行いました。

●同ブログ関連記事:【Visual Studio 2022 Community C#  セットアップ】 














参考サイト
●工学部生の生プレス
https://kogakubu.com/?p=3015
●C#フォームプログラムでのシリアル通信の仕方【https://qiita.com/mag2/items/d15bc3c9d66ce0c8f6b1



プロジェクトの作成

【新しいプロジェクトの作成】
【Windows フォームアプリケーション (.NET Framework)】選択
【次へ】


【プロジェクト名】
【場所】
【作成】


デザイン

【button1】➨【LED 点灯】
【button2】➨【LED 消灯】
プロパティ変更





【ツールボックス】【SerialPort】を【form1】にドラッグ&ドロップします。

















デバイスマネージャー


Arduino Nano接続ポートを調べて
【serialPort1.PortName = "COM3";】追加しました。



アプリの実行






Arduino プログラム
#define LED 13
int R_data = 0; //受信データ

void setup() {
  pinMode(LED,OUTPUT);
  Serial.begin(9600);
  digitalWrite(LED,LOW);
}

void loop() {
  if(Serial.available() > 0){    //シリアルポートにデータがあるか 
    R_data = Serial.read();    // データを読み込む 
  if (R_data == 49) {          //点灯 ASCIIコード「1」10進
    digitalWrite(LED,HIGH);
  }  
  if (R_data == 48) {          // 消灯 ASCIIコード「0」10進
    digitalWrite(LED,LOW);
  }
  delay(10);
 }
}


Visual Studio 2022 Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM3";//ポート設定
            serialPort1.Open(); //シリアルポートを接続
            serialPort1.Write("1");   //LED点灯
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0"); //LED消灯
            serialPort1.Close();    //接続中のシリアルポートを切断
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

0 件のコメント:

コメントを投稿

FST3253 - 直交ミキサ製作

製造終了のFST3253を検索中にアマゾンの口コミ(SDRトランシーバーに使用出来る)を見てFST3253MTCとピッチ変換基板を購入。FST3253 直交ミキサ基板を製作してPi Pico Rx-SDRで受信確認できました。  ●Pi Pico Rx  【 https://10...