/// <summary> /// Event handler for incoming XML messages; raised by XmlSocketIO class. /// Currently only scans for digital output events and passes them to phidgets /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void xmlIO_XmlIn(object sender, XmlInEventArgs e) { Console.WriteLine("Msg in: {0}",e.Text); // XmlTextReader is a simple forward-only XML parser XmlTextReader reader = new XmlTextReader(e.Text,XmlNodeType.Element, null); // scan through received XML from beginning to end: while(reader.Read()) { if(reader.Name == "event") //this is the element name { if(reader.GetAttribute("type").Equals("digital_out")) { // load values passed in attributes byte index = Convert.ToByte(reader.GetAttribute("index")); bool val = Convert.ToBoolean(reader.GetAttribute("value")); phidgetsKit.Outputs[index].State=val; } // add code for other outputs (e.g., servo motor) here } } }
/// <summary> /// When a complete message (\0 terminated?) has been received, /// rasie the XmlIn event and pass the message in the event /// </summary> /// <param name="asyncResult"></param> void OnCompletedRead(IAsyncResult asyncResult) { int bytesRead = stream.EndRead(asyncResult); if (bytesRead > 0) { // careful when passing non-ascii strings! String s = Encoding.ASCII.GetString(readBuffer, 0, bytesRead); // raise XmlIn event XmlInEventArgs e = new XmlInEventArgs(s); XmlIn(this,e); // go back to reading the port stream.BeginRead(readBuffer, 0, readBuffer.Length, readCallBack, null); } }