0

I'm trying to convert some VB.NET programs to C#. I'm quite familiar with VB and am just now getting started with C#. My first program seems to have been exported well enough. I used the MSDN to take care of a few errors after the conversion. BUT the two procedures below do not hit. The program has 0 errors in Visual Studios, and from what I can tell, they are structured just like the examples on the MSDN website. I'm using Visual Studio 2015.

It’s a simple program that opens a COM port and receives data; very basic. Works perfect in VB, and in C# everything but these 2 procedures fire. Any insight on what I’m doing wrong would be immensely appreciated.

    //catches incoming data from serial device.
private void SerialPort1_DataReceived(System.Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        try
        {
            ReceivedText(serialPort1.ReadExisting());
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error (4)", "Error", MessageBoxButtons.OK);
        }
    }
    //input from ReadExisting
    private void ReceivedText(string text)
    {
        try
        {
            if (this.txtOutput.InvokeRequired)
            {
                SetTextCallback x = new SetTextCallback(ReceivedText);
                this.Invoke(x, new object[] { (text) });
            }
            else
            {
                this.txtOutput.Text += text;
            }
            //append text
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error (5)", "Error", MessageBoxButtons.OK);
        }
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
SimpleJack
  • 33
  • 1
  • 4
  • 4
    `SerialPort1_DataReceived` looks like an event handler. Did you attached it to event? – ASh Apr 02 '18 at 13:06
  • @ASh it is not used again in the program if that is what you are asking. Yes, it is an event handler. In the VB.net program this event is hit when the assigned COM port is receiving data from the attached peripheral. – SimpleJack Apr 02 '18 at 13:10
  • 1
    [How to Read and Write from the Serial Port](//stackoverflow.com/q/1243070). In the example, note the line: `port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);` – 001 Apr 02 '18 at 13:11
  • 1
    Possible duplicate of [How to Read and Write from the Serial Port](https://stackoverflow.com/questions/1243070/how-to-read-and-write-from-the-serial-port) – ASh Apr 02 '18 at 13:13
  • The question doesn't contain where you are registering the event handler. –  Apr 02 '18 at 13:15
  • If you are adding the serial port through the designer, you can set the callback in the properties window, under events. – 001 Apr 02 '18 at 13:20
  • @JohnnyMopp Thanks for the tips! Im going to shoot into to work now to test them both. Ill let you know if they work! – SimpleJack Apr 02 '18 at 13:30
  • @JohnnyMopp Setting it in the designer worked for me. I looked at the VB.NET program and it was set there to. VS must have doe it automatically. If you post the answer I'l mark it correct for you. Thank you, I appreciate your help. – SimpleJack Apr 03 '18 at 13:16

1 Answers1

0

When adding a serial port through the designer, set the call back in the properties window under the events tab:

enter image description here

001
  • 13,291
  • 5
  • 35
  • 66