// method to connect to any open serial ports and attempt handshake. // On success, method exits and logs successful baud rate and port name public Boolean connect() { if (serialPort.IsOpen) { return true; } else { // set properties that we know will be always true serialPort.Parity = Parity.None; serialPort.DataBits = 8; serialPort.StopBits = StopBits.One; serialPort.Handshake = Handshake.None; serialPort.ReceivedBytesThreshold = 3; serialPort.ReadTimeout = 500; string[] AvailableSerialPorts = null; // clear out // Populate the list box with currently available COM ports AvailableSerialPorts = SerialPort.GetPortNames(); foreach (string s in AvailableSerialPorts) // put each available port in as an item { foreach (int i in baudValues) { try { serialPort.PortName = s; serialPort.BaudRate = i; serialPort.Open(); // open serial port on a COM and a baud from the baud values array txPacket = new TxPacket(TxPacket.INSTRUCTION_HANDSHAKE, TxPacket.DATA1_HANDSHAKE, TxPacket.DATA2_HANDSHAKE); // send the packet as an array of bytes serialPort.DiscardOutBuffer(); serialPort.DiscardInBuffer(); serialPort.Write(txPacket.asByteArray(), 0, 3); rxPacket = new RxPacket(); // make a new incoming packet to store data in. while ((serialPort.BytesToRead < 3) && (serialTimeoutCounter < connectTimeout)) { Thread.Sleep(1); serialTimeoutCounter++; } serialTimeoutCounter = 0; if (dataStorage.getVerbosity()) { form1.appendToRichTextBox1("Attempting connection on " + serialPort.PortName + " " + serialPort.BaudRate + " baud\r"); } if (serialPort.BytesToRead >= 3) { rxPacket.setInstruction((Byte)serialPort.ReadByte()); rxPacket.setData1((Byte)serialPort.ReadByte()); rxPacket.setData2((Byte)serialPort.ReadByte()); if ((rxPacket.getInstruction() == RxPacket.RX_INSTRUCTION_HANDSHAKE) && (rxPacket.getData2() == RxPacket.RX_DATA2_HANDSHAKE)) { incomingPackets.Enqueue(rxPacket); // put the handshake response packet in the incoming packets queue. parseData(incomingPackets); // parseData(rxPacket); serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); // start the handler for future packets. form1.appendToRichTextBox1("Connected to device on port " + s + " at " + i + " baud.\r"); } else { if (dataStorage.getVerbosity()) { form1.appendToRichTextBox1("An MCU at " + serialPort.PortName + ", " + serialPort.BaudRate + " baud replied with " + rxPacket.getInstruction().ToString("x") + " " + rxPacket.getData1().ToString("x") + " " + rxPacket.getData2().ToString("x") + "\r"); } serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); serialPort.Close(); } } else { serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); serialPort.Close(); if (dataStorage.getVerbosity()) { form1.appendToRichTextBox1("Failed to connect to " + serialPort.PortName + " at " + serialPort.BaudRate + " baud rate.\r"); } } } catch (Exception ex) { serialPort.DiscardInBuffer(); serialPort.DiscardOutBuffer(); serialPort.Close(); form1.appendToRichTextBox1("Error connecting to port " + serialPort.PortName + " at " + serialPort.BaudRate + " baud, message: " + ex.Message + "\r"); } if (serialPort.IsOpen) { return true; } } if (serialPort.IsOpen) { return true; } } } return false; }
// takes care of incoming data, depending on the last command sent. private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { serialDone = false; if (serialPort.IsOpen) { try { while (serialPort.BytesToRead >= 3) { rxPacket = new RxPacket(); rxPacket.setInstruction((Byte)serialPort.ReadByte()); rxPacket.setData1((Byte)serialPort.ReadByte()); rxPacket.setData2((Byte)serialPort.ReadByte()); // put the new input packet into the queue incomingPackets.Enqueue(rxPacket); } //if out of the while loop, then the byte inByte is 0xFF, EOF // serialPort.DiscardInBuffer(); // clear out the input buffer (seems to mess stuff up) } catch (IndexOutOfRangeException ex) { MessageBox.Show(ex.Message); Exception real = ex.GetBaseException(); MessageBox.Show(real.Message); } catch (Exception ex) { form1.appendToRichTextBox1(ex.Message); } parseData(incomingPackets); serialDone = true; } }