C#中serialPort控件中DataReceived事件一直都无法触发,程序如下,应该怎么修改! 15
privatevoidbutton1_Click(objectsender,EventArgse){serialPort1.PortName="COM1";serialP...
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;//波特率
serialPort1.Parity = Parity.None;//奇偶校验位
serialPort1.DataBits = 8;//数据位
serialPort1.StopBits = StopBits.One;
serialPort1.ReadTimeout = 1000;
serialPort1.DtrEnable = true;
serialPort1.RtsEnable = true;
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
button1.Enabled = false;
button2.Enabled = true;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
string str = Encoding.Unicode.GetString(data);
serialPort1.Close();
if (str == "关机")
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(" shutdown /s");
p.StandardInput.WriteLine(" exit");
}
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "关闭计算机")
{
byte[] data = Encoding.Unicode.GetBytes("关机");
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
button2.Text = "取消关机";
}
else
{
button2.Text = "关闭计算机";
button1.Enabled = true;
button2.Enabled = false;
//取消关机
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(" shutdown /a");
p.StandardInput.WriteLine(" exit");
}
}
求各位大神! 展开
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;//波特率
serialPort1.Parity = Parity.None;//奇偶校验位
serialPort1.DataBits = 8;//数据位
serialPort1.StopBits = StopBits.One;
serialPort1.ReadTimeout = 1000;
serialPort1.DtrEnable = true;
serialPort1.RtsEnable = true;
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
button1.Enabled = false;
button2.Enabled = true;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
string str = Encoding.Unicode.GetString(data);
serialPort1.Close();
if (str == "关机")
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(" shutdown /s");
p.StandardInput.WriteLine(" exit");
}
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "关闭计算机")
{
byte[] data = Encoding.Unicode.GetBytes("关机");
string str = Convert.ToBase64String(data);
serialPort1.WriteLine(str);
button2.Text = "取消关机";
}
else
{
button2.Text = "关闭计算机";
button1.Enabled = true;
button2.Enabled = false;
//取消关机
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine(" shutdown /a");
p.StandardInput.WriteLine(" exit");
}
}
求各位大神! 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询