所以我做了一个项目,其中一个系统通过Arduino和Xbee传输数据,接收系统通过Arduino和Xbee接收数据。在接收端,我将信息发送到Visual Studios。因此,当我按停止按钮停止接收数据时,我得到IOException未处理。我该如何解决这个问题?
错误将指向我在
下面添加评论的行接收端的我的C#代码:
public Form1()
{
InitializeComponent();
}
private void start_btn_Click(object sender, EventArgs e)
{
myport = new SerialPort();
myport.BaudRate = 9600;
myport.PortName = port_name_tb.Text;
myport.Parity = Parity.None;
myport.DataBits = 8;
myport.StopBits = StopBits.One;
myport.DataReceived+=SerialDataReceivedEventHandler
(myport_DataReceived);
try
{
myport.Open();
data_tb.Text = "";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
void myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(displaydata_event));
in_data = myport.ReadLine(); //------> ERROR POINTED TO THIS LINE
}
private void displaydata_event(object sender, EventArgs e)
{
datetime = DateTime.Now;
string date = datetime.Day + "/" + datetime.Month;
string time = datetime.Hour + ":" + datetime.Minute + ":"
+ datetime.Second;
data_tb.AppendText(date +" " + time + "\t\t" + in_data + "\n");
}
private void stop_btn_Click(object sender, EventArgs e)
{
try
{
myport.Close();
}
catch (Exception ex2)
{
MessageBox.Show(ex2.Message, "Error");
}
}
private void save_btn_Click(object sender, EventArgs e)
{
datetime = DateTime.Now;
string date = datetime.Day + "/" + datetime.Month;
string time = datetime.Hour + ":" + datetime.Minute + ":"
+ datetime.Second;
try
{
string pathfile = @"C:\Users\User\Desktop\Cdata\";
string filename = "prototype.txt";
System.IO.File.WriteAllText(pathfile + filename, data_tb.Text);
MessageBox.Show("Data has been saved to " + pathfile);
}
catch (Exception ex3)
{
MessageBox.Show(ex3.Message, "Error");
}
}
}
答案 0 :(得分:0)
您没有正确捕获交换机中的异常,因此未处理的IOException。
您可以通过将捕获量从Exception
更改为IOException exX
&来解决此问题。然后用那个做点什么。