Note that there are some explanatory texts on larger screens.

plurals
  1. POC# says "The port is already opened." Fixing it, then "The port is closed." What is wrong with my code?
    text
    copied!<p>I'm trying to put a Serial DataReceivedListener to automate the reading of data from the serial port. Before adding this event, everything works fine.</p> <p>Certain commands cannot continue because C# complains that "The port is already opened." If I comment out "port.Open();", C# then complains that "The port is closed."</p> <p>Here is the code that I think is covered by this whole thing.</p> <pre><code> SerialPort port; String buffer; String[] bufferArray; String tellArduino; // any random one-byte character to "ping" Arduino for a data point Queue&lt;String&gt; date = new Queue&lt;String&gt;(); Queue&lt;String&gt; time = new Queue&lt;String&gt;(); Queue&lt;double&gt; dcGen = new Queue&lt;double&gt;(); public Form1() { // // IDE created this thing // InitializeComponent(); } void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { getDataPoint(); } private void selectPort() { // // Creates new instance of SerialPort with the same // settings as that of the Arduino. Checks if the selected // port can be opened. Catches common exceptions found while debugging // try { port = new SerialPort((string)ports.SelectedItem); port.BaudRate = 9600; port.Parity = Parity.None; port.StopBits = StopBits.One; port.Open(); // check if port can be opened port.Close(); portUsedLabel.Text = (string)ports.SelectedItem; port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); getDataPoint(); // called here for testing. Values successfully split } // gotta catch 'em all catch { stuff here } } private void getDataPoint() { /* still incomplete due to debugging phase */ // // Method for getting a data point from the Arduino unit // Pings Arduino unit for a data point. Program then reads // entire serial output until new line (\n) is found. // Program splits the entire buffer in to the bufferArray // and proceeds to distribute each element in to their // respective Queues // buffer = ""; bufferArray = new String[] { "" }; tellArduino = "m"; // any random one-byte string to "ping" Arduino for a data point // it |must| remain exactly one byte to avoid duplicate data port.Open(); port.WriteLine(tellArduino); for (int x = 0; x &lt; tellArduino.Length; x++) { buffer += port.ReadLine(); } port.Close(); // Split each value then add to respective queues bufferArray = buffer.Split(','); date.Enqueue(bufferArray[0]); time.Enqueue(bufferArray[1]); dcGen.Enqueue(double.Parse(bufferArray[2], System.Globalization.CultureInfo.InvariantCulture)); // Start debugging code // adding to ListBox for debugging and checking reasons ports.BeginUpdate(); foreach (String value in bufferArray) { ports.Items.Add(value); } // for testing type double printing ports.Items.Add(dcGen.ElementAt(0)); // ports.EndUpdate(); // End debugging code } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload