123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using ANT_Managed_Library;
- namespace ANT_NetDll_Demo
- {
- public partial class DemoWindow : Window
- {
-
- delegate void dAppendText(String toAppend);
-
- ANT_Device device0;
- ANT_Device device1;
- public DemoWindow()
- {
- InitializeComponent();
- }
-
- private void button_Start_Click(object sender, RoutedEventArgs e)
- {
- if (button_Start.Content.Equals("Start Demo"))
- {
- if (startUp())
- {
- comboBox_demoMode.IsEnabled = false;
- border_DemoControls.IsEnabled = true;
- button_DoAction.IsEnabled = true;
- button_Start.Background = System.Windows.Media.Brushes.Salmon;
- button_Start.Content = "Stop Demo";
- return;
- }
- }
-
- shutdown();
- comboBox_demoMode.IsEnabled = true;
- button_DoAction.IsEnabled = false;
- button_Start.Background = System.Windows.Media.Brushes.LightGreen;
- button_Start.Content = "Start Demo";
- }
-
- private bool startUp()
- {
-
-
- try
- {
-
-
-
-
- device0 = new ANT_Device();
-
-
-
- device0.deviceResponse += new ANT_Device.dDeviceResponseHandler(device0_deviceResponse);
- device0.getChannel(0).channelResponse += new dChannelResponseHandler(d0channel0_channelResponse);
- textBox_device0.Text = "Device 0 Connected" + Environment.NewLine;
-
- switch (comboBox_demoMode.SelectedIndex)
- {
- case 0:
- textBox_Display.Text = "Starting Mode: d0-Master, d1-Slave" + Environment.NewLine;
-
-
- try
- {
- device1 = new ANT_Device();
- }
- catch (Exception ex)
- {
- throw new Exception("Can not connect to second device, " + ex.Message);
- }
- textBox_device1.Text = "Device 1 Connected" + Environment.NewLine;
- textBox_device1.IsEnabled = true;
-
- device1.deviceResponse += new ANT_Device.dDeviceResponseHandler(device1_deviceResponse);
- device1.getChannel(0).channelResponse += new dChannelResponseHandler(d1channel0_channelResponse);
-
-
- setupAndOpen(device0, ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10);
- setupAndOpen(device1, ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00);
- break;
- case 1:
- textBox_Display.Text = "Starting Mode: d0 only - Master" + Environment.NewLine;
- textBox_device1.Text = "Device 1 Not Used" + Environment.NewLine;
- textBox_device1.IsEnabled = false;
- setupAndOpen(device0, ANT_ReferenceLibrary.ChannelType.BASE_Master_Transmit_0x10);
- break;
- case 2:
- textBox_Display.Text = "Starting Mode: d0 only - Slave" + Environment.NewLine;
- textBox_device1.Text = "Device 1 Not Used" + Environment.NewLine;
- textBox_device1.IsEnabled = false;
- setupAndOpen(device0, ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00);
- break;
- }
- }
- catch (Exception ex)
- {
- textBox_Display.AppendText("Error: " + ex.Message + Environment.NewLine);
- if (device0 == null)
- textBox_Display.AppendText("Could not connect to any devices, ensure an ANT device is connected to your system and try again." + Environment.NewLine);
- textBox_Display.AppendText(Environment.NewLine);
- return false;
- }
- return true;
- }
- private void setupAndOpen(ANT_Device deviceToSetup, ANT_ReferenceLibrary.ChannelType channelType)
- {
-
- try
- {
-
-
-
- ANT_Channel channel0 = deviceToSetup.getChannel(0);
-
-
-
-
-
-
-
-
-
-
- if (channel0.assignChannel(channelType, 0, 500))
- threadSafePrintLine("Ch assigned to " + channelType + " on net 0.", textBox_Display);
- else
- throw new Exception("Channel assignment operation failed.");
-
-
-
-
-
-
- if(channel0.setChannelID(12345, false, 67, 89, 500))
- threadSafePrintLine("Set channel ID to 12345, 67, 89", textBox_Display);
- else
- throw new Exception("Set Channel ID operation failed.");
-
-
- if(channel0.setChannelPeriod(16384, 500))
- threadSafePrintLine("Message Period set to 16384/32768 seconds per message", textBox_Display);
-
- if(channel0.openChannel(500))
- threadSafePrintLine("Opened Channel" + Environment.NewLine, textBox_Display);
- else
- throw new Exception("Channel Open operation failed.");
- }
- catch (Exception ex)
- {
- throw new Exception("Setup and Open Failed. " + ex.Message + Environment.NewLine);
- }
- }
-
- void device0_deviceResponse(ANT_Response response)
- {
- threadSafePrintLine(decodeDeviceFeedback(response), textBox_device0);
- }
-
- void d0channel0_channelResponse(ANT_Response response)
- {
- threadSafePrintLine(decodeChannelFeedback(response), textBox_device0);
- }
-
- void device1_deviceResponse(ANT_Response response)
- {
- threadSafePrintLine(decodeDeviceFeedback(response), textBox_device1);
- }
-
- void d1channel0_channelResponse(ANT_Response response)
- {
- threadSafePrintLine(decodeChannelFeedback(response), textBox_device1);
- }
-
- String decodeDeviceFeedback(ANT_Response response)
- {
- string toDisplay = "Device: ";
-
-
-
-
- if (response.responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40)
- {
-
- toDisplay += (ANT_ReferenceLibrary.ANTMessageID)response.messageContents[1] + ", Ch:" + response.messageContents[0];
-
- if ((ANT_ReferenceLibrary.ANTEventID)response.messageContents[2] != ANT_ReferenceLibrary.ANTEventID.RESPONSE_NO_ERROR_0x00)
- toDisplay += Environment.NewLine + ((ANT_ReferenceLibrary.ANTEventID)response.messageContents[2]).ToString();
- }
- else
- toDisplay += ((ANT_ReferenceLibrary.ANTMessageID)response.responseID).ToString();
-
- toDisplay += Environment.NewLine + "::" + Convert.ToString(response.responseID, 16) + ", " + BitConverter.ToString(response.messageContents) + Environment.NewLine;
- return toDisplay;
- }
- String decodeChannelFeedback(ANT_Response response)
- {
-
-
-
-
-
-
-
-
-
-
- StringBuilder stringToPrint;
- stringToPrint = new StringBuilder("Channel: ", 100);
-
-
-
- if (response.responseID == (byte)ANT_ReferenceLibrary.ANTMessageID.RESPONSE_EVENT_0x40)
- stringToPrint.AppendLine(((ANT_ReferenceLibrary.ANTEventID)response.messageContents[2]).ToString());
- else
- stringToPrint.AppendLine("Received " + ((ANT_ReferenceLibrary.ANTMessageID)response.responseID).ToString());
-
-
- stringToPrint.Append(" :: ");
- stringToPrint.Append(Convert.ToString(response.responseID, 16));
- stringToPrint.Append(", ");
- stringToPrint.Append(BitConverter.ToString(response.messageContents) + Environment.NewLine);
- return stringToPrint.ToString();
- }
- void threadSafePrintLine(String stringToPrint, TextBox boxToPrintTo)
- {
-
-
- boxToPrintTo.Dispatcher.BeginInvoke(new dAppendText(boxToPrintTo.AppendText), System.Windows.Threading.DispatcherPriority.Background, stringToPrint + Environment.NewLine);
- }
- private void shutdown()
- {
-
-
- ANT_Device.shutdownDeviceInstance(ref device0);
- ANT_Device.shutdownDeviceInstance(ref device1);
- }
-
- private void textBox_autoScroll(object sender, TextChangedEventArgs e)
- {
- ((TextBox)sender).ScrollToEnd();
- }
- private void button_DoAction_Click(object sender, RoutedEventArgs e)
- {
- switch ((String)((ComboBoxItem)comboBox_actionList.SelectedItem).Content)
- {
- case "Send Acknowledged Message (d0)":
- action_SendAcknowledged();
- break;
- case "Send Burst Message (d0)":
- action_SendBurst();
- break;
- case "Set Broadcast to random value (d0)":
- action_SetRandomBroadcast();
- break;
- case "Request Capabilities":
- action_ReqCapabilities();
- break;
- case "Request Version":
- action_ReqVersion();
- break;
- case "Request Channel ID":
- action_ReqChannelID();
- break;
- case "Request Status":
- action_ReqStatus();
- break;
- default:
- break;
- }
- threadSafePrintLine("", textBox_Display);
- }
- private void action_SendAcknowledged()
- {
-
-
-
- threadSafePrintLine("Sending Acknowledged Message from device 0", textBox_Display);
- if (device0.getChannel(0).sendAcknowledgedData(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, 500) != ANT_ReferenceLibrary.MessagingReturnCode.Pass)
- threadSafePrintLine("Acknowledged Message Failed", textBox_Display);
- else
- threadSafePrintLine("Acknowledged Message Succeeded", textBox_Display);
- }
- private void action_SendBurst()
- {
-
-
-
-
- byte[] byteArray = new byte[256];
- for (int i = 0; i < 256; ++i)
- byteArray[i] = (byte)i;
- threadSafePrintLine("Sending Burst Message from device 0", textBox_Display);
- if(device0.getChannel(0).sendBurstTransfer(byteArray, 3000) != ANT_ReferenceLibrary.MessagingReturnCode.Pass)
- threadSafePrintLine("Burst Message Failed", textBox_Display);
- else
- threadSafePrintLine("Burst Message Succeeded", textBox_Display);
- }
- private void action_SetRandomBroadcast()
- {
-
-
-
-
-
-
- threadSafePrintLine("Setting Device 0 Broadcast Buffer to Random Value", textBox_Display);
- byte[] randArray = new byte[8];
- Random generator = new Random();
- generator.NextBytes(randArray);
- if(!device0.getChannel(0).sendBroadcastData(randArray))
- threadSafePrintLine("Broadcast Message Failed", textBox_Display);
- else
- threadSafePrintLine("Broadcast Message Succeeded", textBox_Display);
- }
- private void action_ReqCapabilities()
- {
-
-
-
-
-
-
- threadSafePrintLine("Requesting Device 0's Capabilities", textBox_Display);
- try
- {
- ANT_DeviceCapabilities currentCaps = device0.getDeviceCapabilities(true, 500);
-
- threadSafePrintLine("Retrieved Device 0's Capabilites:", textBox_Display);
- threadSafePrintLine(currentCaps.ToString(), textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Capabilities of Device 0 Timed Out", textBox_Display);
- }
-
- if (device1 != null)
- {
- threadSafePrintLine("Requesting Device 1's Capabilities", textBox_Display);
- try
- {
- ANT_DeviceCapabilities currentCaps = device1.getDeviceCapabilities(true, 500);
- threadSafePrintLine("Retrieved Device 1's Capabilites:", textBox_Display);
- threadSafePrintLine(currentCaps.ToString(), textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Capabilities of Device 1 Timed Out", textBox_Display);
- }
- }
- }
- private void action_ReqVersion()
- {
-
-
-
- threadSafePrintLine("Requesting Device 0 Version", textBox_Display);
-
- try
- {
- ANT_Response version = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.VERSION_0x3E, 500);
- threadSafePrintLine("Device 0 ANT Version: " + new ASCIIEncoding().GetString(version.messageContents, 0, version.messageContents.Length - 1), textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Device 0 Version Timed Out", textBox_Display);
- }
-
- if (device1 != null)
- {
- threadSafePrintLine("Requesting Device 1 Version", textBox_Display);
- try
- {
- ANT_Response version = device1.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.VERSION_0x3E, 500);
- threadSafePrintLine("Device 1 ANT Version: " + new ASCIIEncoding().GetString(version.messageContents, 0, version.messageContents.Length - 1), textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Device 1 Version Timed Out", textBox_Display);
- }
- }
- }
- private void action_ReqChannelID()
- {
-
- threadSafePrintLine("Requesting Device 0 Channel ID", textBox_Display);
-
- try
- {
- ANT_Response IDresp = device0.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
- threadSafePrintLine("Device 0 Channel ID: " + "Dev#: " + ((IDresp.messageContents[2] << 8) + IDresp.messageContents[1]) + ", DevType: " + IDresp.messageContents[3] + ", TransType: " + IDresp.messageContents[4], textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Device 0 Channel ID Timed Out", textBox_Display);
- }
-
- if (device1 != null)
- {
- threadSafePrintLine("Requesting Device 1 Channel ID", textBox_Display);
- try
- {
- ANT_Response IDresp = device1.requestMessageAndResponse(ANT_ReferenceLibrary.RequestMessageID.CHANNEL_ID_0x51, 500);
- threadSafePrintLine("Device 1 Channel ID: " + "Dev#: " + ((IDresp.messageContents[2] << 8) + IDresp.messageContents[1]) + ", DevType: " + IDresp.messageContents[3] + ", TransType: " + IDresp.messageContents[4], textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Device 1 Channel ID Timed Out", textBox_Display);
- }
- }
- }
- private void action_ReqStatus()
- {
-
- threadSafePrintLine("Requesting Device 0 Status", textBox_Display);
- try
- {
- ANT_ChannelStatus chStatus = device0.getChannel(0).requestStatus(500);
-
- threadSafePrintLine("Device 0 Channel 0 Status: " + chStatus.BasicStatus + ", Network Number: " + chStatus.networkNumber + ", Type: " + chStatus.ChannelType, textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Device 0 Status Failed", textBox_Display);
- }
-
- if (device1 != null)
- {
- threadSafePrintLine("Requesting Device 1 Status", textBox_Display);
- try
- {
- ANT_ChannelStatus chStatus = device1.getChannel(0).requestStatus(500);
-
- threadSafePrintLine("Device 1 Channel 0 Status: " + chStatus.BasicStatus + ", Network Number: " + chStatus.networkNumber + ", Type: " + chStatus.ChannelType, textBox_Display);
- }
- catch (Exception)
- {
- threadSafePrintLine("Requesting Device 1 Status Failed", textBox_Display);
- }
- }
- }
- }
- }
|