Click here to Skip to main content
15,894,343 members
Home / Discussions / C#
   

C#

 
QuestionSERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Member 1368881630-May-18 22:19
Member 1368881630-May-18 22:19 
Hi all, I'm developing a serial monitor for my application in WPF, programming in C#.
I have trouble managing the DataReceived event, because i want a real time monitor like HyperTerminal or TeraTerm for example (I'm not using them because I want my terminal to be a part of an ethernet communication tool, wich I already developed using winPcap).

I have to read some data from my microcontroller, display it on the textBox (It just prints a menu and the list of commands available) and when it finishes the loading sequence I would like to interact with it, nothing special, just send a "flash-" command to program the fpga of the board.

My application goes in exception when I try to update the textbox.text with the data received. I tried to search everywhere but despite a lot of examples, I didn't catch something which is explaining the code properly.

Here is the code, thanks in advance

namespace WpfApplication1 {
    /// <summary>
    /// Interaction logic for SerialMonitor.xaml
    /// </summary>
    public partial class SerialMonitor : Window {
        
        //VARIABLES
        public SerialPort comPort = new SerialPort();

        public SerialMonitor() {
            //initialization
            InitializeComponent();
            scanPorts();
           
        }

        
        
        private void scanPorts() {
            textBoxIndata.Clear();
            string[] ports = SerialPort.GetPortNames();
            foreach (string port in ports) {
                comboBoxPorts.Items.Add(port);
            }
        }
              
      

        private void openComBtn_Click(object sender , RoutedEventArgs e) {

            comPort.Parity = Parity.None;
            comPort.DataBits = 8;
            comPort.ReadTimeout = 500;
            comPort.StopBits = StopBits.One;

            if (comboBoxPorts.SelectedItem != null && comboBoxPorts.SelectedItem != null) {

                comPort.PortName = comboBoxPorts.SelectedItem.ToString();
                comPort.BaudRate = Convert.ToInt32(comboBoxBaud.Text);

                try {
                    //Open port and add the event handler on datareceived
                    comPort.Open();                 
                    comPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                }
                catch (Exception ex) {
                    MessageBox.Show(ex.ToString());                    
                }              
            }
            if (comPort.IsOpen) {
                label1.Content = "COM PORT OPEN";              
            }
        }


        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
            
        }


        //function to update the textBox, didn't manage to get it working
        private void updateUI (string s) {

        }



        //CLOSE AND EXIT BUTTONS
        private void closeComBtn_Click(object sender , RoutedEventArgs e) {
            if (comPort.IsOpen) {
                comPort.Close();
                label1.Content = "COM PORT CLOSED";
            }
        }

        private void exitBtn_Click(object sender , RoutedEventArgs e) {
            if (comPort.IsOpen) {
                comPort.Close();
            }
            this.Close();
        }
    }
}

QuestionRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Richard MacCutchan30-May-18 23:00
mveRichard MacCutchan30-May-18 23:00 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Member 1368881630-May-18 23:29
Member 1368881630-May-18 23:29 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Jochen Arndt30-May-18 23:39
professionalJochen Arndt30-May-18 23:39 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Gerry Schmitz31-May-18 11:50
mveGerry Schmitz31-May-18 11:50 
AnswerRe: SERIAL MONITOR IN C#, DataReceivedHandler problem Pin
Member 1368881631-May-18 23:04
Member 1368881631-May-18 23:04 
QuestionIs there a JSON Library that works like the System.Xml.Linq classes? Pin
MAIsw30-May-18 21:02
MAIsw30-May-18 21:02 
AnswerRe: Is there a JSON Library that works like the System.Xml.Linq classes? Pin
Pete O'Hanlon30-May-18 22:31
mvePete O'Hanlon30-May-18 22:31 
QuestionCreate JSON object from string Pin
WillyBilly9030-May-18 10:27
WillyBilly9030-May-18 10:27 
AnswerRe: Create JSON object from string Pin
Mycroft Holmes30-May-18 14:17
professionalMycroft Holmes30-May-18 14:17 
GeneralRe: Create JSON object from string Pin
WillyBilly9031-May-18 9:57
WillyBilly9031-May-18 9:57 
AnswerRe: Create JSON object from string Pin
Richard Deeming31-May-18 10:08
mveRichard Deeming31-May-18 10:08 
GeneralRe: Create JSON object from string Pin
WillyBilly901-Jun-18 8:13
WillyBilly901-Jun-18 8:13 
QuestionReflect Generic Property Info Pin
Kevin Marois30-May-18 7:19
professionalKevin Marois30-May-18 7:19 
AnswerRe: Reflect Generic Property Info Pin
Gerry Schmitz30-May-18 9:46
mveGerry Schmitz30-May-18 9:46 
AnswerRe: Reflect Generic Property Info Pin
Richard Deeming30-May-18 9:51
mveRichard Deeming30-May-18 9:51 
GeneralRe: Reflect Generic Property Info Pin
Eddy Vluggen30-May-18 10:23
professionalEddy Vluggen30-May-18 10:23 
GeneralRe: Reflect Generic Property Info Pin
Kevin Marois30-May-18 10:30
professionalKevin Marois30-May-18 10:30 
GeneralRe: Reflect Generic Property Info Pin
Kevin Marois31-May-18 11:20
professionalKevin Marois31-May-18 11:20 
GeneralRe: Reflect Generic Property Info Pin
Richard Deeming31-May-18 11:52
mveRichard Deeming31-May-18 11:52 
QuestionUnable to connect to remote server error in iis express Pin
galba201830-May-18 3:07
galba201830-May-18 3:07 
AnswerRe: Unable to connect to remote server error in iis express Pin
Pete O'Hanlon30-May-18 5:31
mvePete O'Hanlon30-May-18 5:31 
GeneralRe: Unable to connect to remote server error in iis express Pin
galba201830-May-18 20:10
galba201830-May-18 20:10 
QuestionGet Outlook email attachments Pin
Raghu Kiran29-May-18 16:23
Raghu Kiran29-May-18 16:23 
AnswerRe: Get Outlook email attachments Pin
Pete O'Hanlon29-May-18 22:36
mvePete O'Hanlon29-May-18 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.