1

I have an Arduino board that wirelessly transmits some sensor readings via an Xbee module to a Serial USB module. I have written the following code to read that data:

    public partial class Debugger : Page
{
    public static string comportnum;
    public delegate void NoArgDelegate();
    public static SerialPort serialX;
    public Debugger()
    {
        InitializeComponent();
        comportnum = "";
    }

    private void ActualButton_Click(object sender, RoutedEventArgs e)
    {
        comportnum = "COM" + comport.Text;

        serialX = new SerialPort(comportnum);
        serialX.BaudRate = 9600;
        try
        {
            serialX.Open();

            serialX.DataReceived += new SerialDataReceivedEventHandler(serialX_DataReceived);
        }
        catch (Exception)
        {
            MessageBox.Show("Houston, we have a problem.");
            //throw;
        }
    }

    void serialX_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        MessageBox.Show("Ping");           
        readingStuff();

    }
    void readingStuff()
    {

        String comdata;
        base.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, (NoArgDelegate)delegate
        {
            DebugWindow.Text += "Data";
            comdata = serialX.ReadLine();
            DebugWindow.Text += "\n" + comdata + "\n";
        });
    }

}

This works as long as I have that MessageBox.Show("Ping"). Without it, the App freezes/crashes. When it freezes/crashes, there is no runtime error. Even while debugging, Visual Studio continues running, however I am unable to click on any other button the WPF App or even click the close button on the WPF app.

I need to figure out a way to make sure the data is read smoothly without any interruptions without having to use the MessageBox.

4

1 に答える 1