|
VS2005不晓得出啥毛病了,断点调试无效。
我用MessageBox的方式调试过。串口是打开了的,我把代码都贴出来,各位帮我看看。
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(Add_Received));
}
public void Add_Received(object sender, EventArgs e)
{
string str = string.Empty;
MessageBox.Show("in event");
if (sp.IsOpen)
{
str = sp.ReadExisting();
MessageBox.Show(str);
}
else
{
MessageBox.Show("串口还未打开!");
}
}
private void OpenPort()//打开串口
{
try
{
if (sp.IsOpen)
{
MessageBox.Show("port is open,then close it!");
sp.Close();
sp.Open();
MessageBox.Show("port is open!");
}
else
{
MessageBox.Show("port is closed,then open it!");
sp.Open();
MessageBox.Show("port is open!");
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("in 2");
if (sp.IsOpen)
{
sp.WriteLine("AT\r");
MessageBox.Show("send AT command!");
}
else
{
MessageBox.Show("please open the Port,first!");
}
MessageBox.Show("out 2");
}
|
|