Adam S
Adam S

Reputation: 9235

Setting the DataReceived event of a SerialPort

Here's what I'm trying to do:

    private void DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
    }

    private void Button_ConnectSend_Click(object sender, RoutedEventArgs e)
    {
        System.IO.Ports.SerialPort SerialPort_Painter;
        SerialPort_Painter.DataReceived += DataReceived;
    }

However, I can't get this working. I get an error: Use of unassigned local variable 'SerialPort_Painter' on the line where I assign a new event handler. Can anyone shed some light on this?

Upvotes: 0

Views: 590

Answers (2)

Adam S
Adam S

Reputation: 9235

System.IO.Ports.SerialPort SerialPort_Painter =new System.IO.Ports.SerialPort();

Sorry guys, I'm just rusty. :(

Upvotes: 0

Chris Laplante
Chris Laplante

Reputation: 29658

This line:

System.IO.Ports.SerialPort SerialPort_Painter = new System.IO.Ports.SerialPort();

does not create a serial port (and not literally a serial port, but a way to access one... you know what I mean). It creates a variable that is capable of holding a Serial Port. You need to instantiate the variable. Here is a really good example of how to use it: http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx/#snippetGroup

Upvotes: 2

Related Questions