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");
}
}
求各位大神! 展开
展开全部
你的意思是serialPort1_DataReceived事件的
if (str == "关机")这个判断条件里的代码没有执行
还是serialPort1_DataReceived事件里的代码没有执行?
if (str == "关机")这个判断条件里的代码没有执行
还是serialPort1_DataReceived事件里的代码没有执行?
更多追问追答
追问
serialPort1_DataReceived整个事件里的都没执行
追答
哦,通过什么方式,给Com1发送数据的?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
大雅新科技有限公司
2024-11-19 广告
2024-11-19 广告
这方面更多更全面的信息其实可以找下大雅新。深圳市大雅新科技有限公司从事KVM延长器,DVI延长器,USB延长器,键盘鼠标延长器,双绞线视频传输器,VGA视频双绞线传输器,VGA延长器,VGA视频延长器,DVI KVM 切换器等,优质供应商,...
点击进入详情页
本回答由大雅新科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询