[英]Shutdown Windows 7 with C# using Visual Studio 2013 and Arduino serial/COM port
我正在嘗試創建一個程序,當從Arduino發送特定的字符串或字符時,該程序將關閉系統。 當我調試程序並從Arduino發送字符串“ S”時,控制台應用程序僅關閉而沒有打印輸出。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Diagnostics;
namespace Arduino_Test
{
class Program
{
static void Main(string[] args)
{
SerialPort myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = "COM5";
myport.Open();
string data_rx = myport.ReadLine();
string s = "S";
if(data_rx == s)
{
Console.WriteLine(data_rx);
var psi = new ProcessStartInfo("shutdown", "/s /f /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
}
}
}
}
我懷疑if語句中有問題。 此外,我需要有關Visual Studio 2013的幫助,我不熟悉C#和應用程序。 我的項目是控制台應用程序,是否可以使用相同的代碼創建Windows窗體應用程序? 我在這方面經驗不足,感謝您的耐心配合。
您將要定義一些新的終端狀態,但請假裝它應該永遠運行。 如果是這種情況,只需將讀取的代碼放入循環中即可。
while(true) {
string data_rx = myport.ReadLine();
string s = "S";
if(data_rx == s)
{
Console.WriteLine(data_rx);
var psi = new ProcessStartInfo("shutdown", "/s /f /t 0");
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process.Start(psi);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.