/// <summary> /// Method to handle STARTUP phase of Motion Board /// </summary> private void HandleACK() { var ack = new byte[2]; UInt16 ack_check = BitConverter.ToUInt16(StartCondition, 0); // We know that we have already received data --> READ it out var read_ret = mPort.Read(ack, 0, 2); // ACK must contain 2 bytes! if (read_ret != 2) { PrintInformation("ERROR: Did not receive correct amount of bytes for ACK!"); StartUpFailed?.Invoke(this, EventArgs.Empty); return; } // Check if we received the CORRECT ACK bytes if (BitConverter.ToUInt16(ack, 0) != ack_check) { PrintInformation("ERROR: Did not receive ACK from Board!"); StartUpFailed?.Invoke(this, EventArgs.Empty); return; } // SET GLOBAL FLAG IsConnected = true; return; }
/// <summary> /// CTOR /// </summary> /// <param name="newDataEvent"></param> public MotionData(int PortNumber) { // TODO: remove this --> the correct port number is given in as parameter // Open Serial Port to read from Board Console.WriteLine("Available COM Ports: {0}", SerialPort.GetPortNames().Length); int i = 1; foreach (var item in SerialPort.GetPortNames()) { Console.WriteLine("[{0}] Port Name: {1}", i++, item); } // Ask user to SELECT correct PORT Console.WriteLine("Select Port number and press ENTER!"); var input = Console.ReadLine(); int portnum = 0; if (!int.TryParse(input, out portnum) || (portnum > SerialPort.GetPortNames().Length)) { Console.WriteLine("ERROR: wrong PORT number!"); return; } // Create NEW SERIAL PORT mPort = new SerialPort(SerialPort.GetPortNames()[portnum - 1], 115200, Parity.None, 8, StopBits.One); //mPort = new SerialPort(SerialPort.GetPortNames()[PortNumber - 1], 115200, Parity.None, 8, StopBits.One); // Attach a method to be called when there // is data waiting in the port's buffer mPort.DataReceived += new SerialDataReceivedEventHandler(Port_DataReceived_Handler); mPort.ReadTimeout = 10000; // Exception if now data is received after 10 sec // Begin communications try { mPort.Open(); } catch (Exception) { PrintInformation("ERROR: could not open PORT!"); StartUpFailed?.Invoke(this, EventArgs.Empty); return; } // Check if port is really OPEN if (!mPort.IsOpen) { PrintInformation("ERROR: could not open PORT!"); StartUpFailed?.Invoke(this, EventArgs.Empty); return; } // Firstly: send port START condition try { mPort.Write(StartCondition, 0, 2); } catch (Exception) { PrintInformation("ERROR: could not send START condition!"); StartUpFailed?.Invoke(this, EventArgs.Empty); return; } timer = new Timer(); timer.Elapsed += new ElapsedEventHandler(OnTimedEvent); timer.Interval = 5000; // 1 sec timer.Enabled = true; timer.AutoReset = false; }