前提・実現したいこと
フォームアプリ内でrapberry piにリモート接続し,その中でGPIOでswの値を取得,それらをアプリ内で表示するプログラムを作成しています.
アプリ内でstring型の変数にGPIOの値を格納し,それらをif文で比較し,1ならOK,0ならNGを表示する処理を書きたいのですが,うまくいきません.
発生している問題・エラーメッセージ
構文自体にエラーは発生しないのですが,実行をしたときにif文をずっと抜けられていないのか,値を表示することなく動作が重くなり止まってしまいます.
該当のソースコード
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using Renci.SshNet; 11using System.Diagnostics; 12 13 14namespace WindowsFormsApp2 15{ 16 public partial class Form1 : Form 17 { 18 public Form1() 19 { 20 InitializeComponent(); 21 } 22 23 // コネクション情報 24 25 private void button1_Click(object sender, EventArgs e) 26 { 27 try 28 { 29 int i = 0; 30 for (i = 0; i < 50; i++) 31 { 32 // 接続先のホスト名またはIPアドレス 33 var hostNameOrIpAddr = "raspberrypi.local"; 34 35 // 接続先のポート番号 36 var portNo = 22; 37 38 // ログインユーザー名 39 var userName = "pi"; 40 41 // ログインパスワード 42 var passWord = "xxxxxxxxx"; 43 44 // コネクション情報 45 ConnectionInfo info = new ConnectionInfo(hostNameOrIpAddr, portNo, userName, 46 new AuthenticationMethod[] { 47 new PasswordAuthenticationMethod(userName, passWord) 48 /* PrivateKeyAuthenticationMethod("キーの場所")を指定することでssh-key認証にも対応 */ 49 } 50 ); 51 52 // クライアント作成 53 SshClient ssh = new SshClient(info); 54 55 // 接続開始 56 ssh.Connect(); 57 58 if (ssh.IsConnected) 59 { 60 // 接続に成功した(接続状態である) 61 Debug.WriteLine("[OK] SSH Connection succeeded!!"); 62 } 63 else 64 { 65 // 接続に失敗した(未接続状態である) 66 Debug.WriteLine("[NG] SSH Connection failed!!"); 67 return; 68 } 69 // 送信したいコマンドを変数に入れる 70 var commandString = "gpio -g read 4 "; 71 var commandString2 = "gpio -g read 9"; 72 var commandString3 = "gpio -g read 27"; 73 74 75 // コマンドを作成する 76 SshCommand cmd = ssh.CreateCommand(commandString); 77 SshCommand cmd2 = ssh.CreateCommand(commandString2); 78 SshCommand cmd3 = ssh.CreateCommand(commandString3); 79 80 // コマンドを実行する 81 82 Console.WriteLine("[CMD] {0}", commandString); 83 Console.WriteLine("[CMD] {0}", commandString2); 84 Console.WriteLine("[CMD] {0}", commandString3); 85 cmd.Execute(); 86 cmd2.Execute(); 87 cmd3.Execute(); 88 // 結果を変数に入れる 89 var stdOut = cmd.Result; 90 var stdErr = cmd.Error; 91 var stdOut2 = cmd2.Result; 92 var stdErr2 = cmd2.Error; 93 var stdOut3 = cmd3.Result; 94 var stdErr3 = cmd3.Error; 95 96 97 // 終了コードを表示する 98 Console.WriteLine("終了コード:{0}", cmd.ExitStatus); 99 Console.WriteLine("終了コード:{0}", cmd2.ExitStatus); 100 Console.WriteLine("終了コード:{0}", cmd3.ExitStatus); 101 102 103 // 標準出力を表示する 104 if (stdOut != string.Empty) 105 { 106 if (stdOut == "1") 107 { 108 Console.WriteLine("標準出力:"); 109 Debug.WriteLine(stdOut); 110 label1.Text = "OK"; 111 Console.WriteLine("---------"); 112 } 113 else 114 { 115 Console.WriteLine("標準出力:"); 116 Debug.WriteLine(stdOut); 117 label1.Text = "NG"; 118 Console.WriteLine("---------"); 119 } 120 } 121 122 if (stdOut2 != string.Empty) 123 { 124 if (stdOut2 == "1") 125 { 126 Console.WriteLine("標準出力:"); 127 Debug.WriteLine(stdOut2); 128 label2.Text = "OK"; 129 Console.WriteLine("---------"); 130 } 131 else 132 { 133 Console.WriteLine("標準出力:"); 134 Debug.WriteLine(stdOut2); 135 label2.Text = "NG"; 136 Console.WriteLine("---------"); 137 } 138 } 139 if (stdOut3 != string.Empty) 140 { 141 if (stdOut3 == "1") 142 { 143 Console.WriteLine("標準出力:"); 144 Debug.WriteLine(stdOut3); 145 label3.Text = "OK"; 146 Console.WriteLine("---------"); 147 } 148 else 149 { 150 Console.WriteLine("標準出力:"); 151 Debug.WriteLine(stdOut3); 152 label3.Text = "NG"; 153 Console.WriteLine("---------"); 154 } 155 } 156 157 158 // エラー出力を表示する 159 if (cmd.ExitStatus != 0 && stdErr != string.Empty) 160 { 161 Console.WriteLine("標準エラー出力:"); 162 Debug.WriteLine(stdErr); 163 Console.WriteLine("----------------"); 164 } 165 if (cmd2.ExitStatus != 0 && stdErr != string.Empty) 166 { 167 Console.WriteLine("標準エラー出力:"); 168 Debug.WriteLine(stdErr2); 169 Console.WriteLine("----------------"); 170 } 171 if (cmd3.ExitStatus != 0 && stdErr != string.Empty) 172 { 173 Console.WriteLine("標準エラー出力:"); 174 Debug.WriteLine(stdErr3); 175 Console.WriteLine("----------------"); 176 } 177 178 179 // 接続終了 180 ssh.Disconnect(); 181 } 182 } 183 184 catch (Exception ex) 185 { 186 // エラー発生時 187 Console.WriteLine(ex); 188 throw ex; 189 } 190 } 191 192 193 } 194 195}
試したこと
比較をせずに,値が変数に入っていればokとすると,正常に動作します.
補足情報(FW/ツールのバージョンなど)
VS2017 C# raspberry pi3 ModelB です! raspberry piのswは3つタクトスイッチを使用しております. どなたかよろしくお願いいたします.
追記させていただきます!
比較をせずに値が変数に入っていればokとする時のコードは以下の通りです。
// 標準出力を表示する
if (stdOut != string.Empty)
{
Console.WriteLine("標準出力:");
Debug.WriteLine(stdOut);
label1.Text = "OK";
Console.WriteLine("---------");
}
標準出力の部分のif文を削除するだけになります。これで実行をすると
出力に以下のように表示されます。
標準出力:
1
ーーーーーーーーーーーーー