3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Gtk3アプリ NodeRedとC#を連携する 入門編

Last updated at Posted at 2021-10-27

###Gtk3アプリ NodeRedとC#を連携する

####前提
Node.jsをインストールする
Node-Redをインストールする

###NodeRedを入門動画で学ぶ

###Nodeを配置する

以下のノードをFlowに配置します。

使用するノード
Inject node
exec node
Debug node
Screenshot from 2021-10-27 15-59-05.png

####配置

Screenshot from 2021-10-27 15-58-50.png

####execノードにパラメーターを追加
コマンドと引数を追加します。
Screenshot from 2021-10-27 15-59-28.png

####C#コンソールアプリ側

Rider画面 コンソールアプリを選択する

Screenshot from 2021-10-27 16-00-28.png

コンソールアプリ実行時に取得した引数を出力するような内容です。

using System;

namespace nodeRedApp
{
    class Program
    {
        static void Main(string[] args)
        {

            foreach (string str in args)
            {
                Console.WriteLine(str);
            }            
        }
    }
}

デプロイ 実行する

インジェクションノードから実行します。
C#のコンソールアプリで出力された文字列をmsg.payloadに取得できます。

このサンプルの例ではタイムスタンプと引数を出力させています。

Screenshot from 2021-10-27 15-58-34.png

node red exec 実行時にエラーが出た場合はパーミッションを変更します。

node red exec node permission deny

chmod 755 アプリ名

###func-execノードを使う
functionの中でexecコマンドが使えます。

Screenshot from 2021-10-29 13-17-58.png
[{"id":"18fa45e.dd993ba","type":"debug","z":"ce212a3e.f97778","name":"","active":true,"console":"false","complete":"false","x":560,"y":380,"wires":[]},{"id":"d649825e.ab13d","type":"inject","z":"ce212a3e.f97778","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":300,"wires":[["3197e4b9.85addc"]]},{"id":"3197e4b9.85addc","type":"func-exec","z":"ce212a3e.f97778","name":"","func":"var exec = child_process.exec('ls -la', (error, stdout, stderr) => { \n    if (error) { \n    console.error(`exec error: ${error}`); \n    return; \n    } \n    console.log(`stdout: ${stdout}`); \n    console.log(`stderr: ${stderr}`); \n    msg.payload = stdout;\n    callback(msg);\n}); ","outputs":1,"noerr":0,"x":360,"y":340,"wires":[["18fa45e.dd993ba"]]}]
var command = '/nodeRedApp test1 test2';

var exec = child_process.exec(command, (error, stdout, stderr) => { 
    if (error) { 
    console.error(`exec error: ${error}`); 
    return; 
    } 
    console.log(`stdout: ${stdout}`); 
    console.log(`stderr: ${stderr}`); 
    msg.payload = stdout;
    callback(msg);
}); 
return msg;

Gtk3アプリ NodeRedで仮想通貨を表示する その1に続く

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?