/// <summary> /// Read incoming data from the microcontroller stream /// </summary> public MMazeEvent ReadStream() { if (MicrocontrollerStream != null && MicrocontrollerStream.CanRead) { //Create a 64-byte buffer to read data from the microcontroller Byte[] arr = new Byte[64]; //Read data from the stream try { int bytes_read = MicrocontrollerStream.Read(arr, 0, arr.Length); //Byte 2 should indicate what kind of data is coming in byte data_type = arr[2]; if (data_type == 'S') { //Byte 3 indicates the kind of event that occurred MMazeEventNames e = MMazeEventNamesConverter.ConvertShortDescriptionToMMazeEventType(Convert.ToChar(arr[3])); //Return the new event that we have read in return(new MMazeEvent(DateTime.Now, e)); } else if (data_type == 'D') { //What is the D command? } else if (data_type == 'J') { //What is the J commmand? } } catch (TimeoutException e) { return(new MMazeEvent() { EventType = MMazeEventNames.Undefined }); } } //Return null in the case that no event was read from the stream return(null); }
/// <summary> /// Converts an event type to a string description. /// </summary> /// <param name="thresholdType">Event type</param> /// <returns>String description of the event type.</returns> public static string ConvertToDescription(MMazeEventNames thresholdType) { FieldInfo fi = thresholdType.GetType().GetField(thresholdType.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { return(attributes[0].Description); } else { return(thresholdType.ToString()); } }
/// <summary> /// Constructor that takes an event time and type /// </summary> /// <param name="t"></param> /// <param name="e"></param> public MMazeEvent(DateTime t, MMazeEventNames e) { EventTime = t; EventType = e; }