1
0

UsbExample.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using ANT_Managed_Library;
  5. using System.Collections.Generic;
  6. using System;
  7. using System.IO;
  8. /*
  9. * UsbExample
  10. *
  11. * you can specify the USB number in init_Device(int USBnum);
  12. * OpenChannel last argument is the device number, default to 0
  13. */
  14. public class UsbExample : MonoBehaviour {
  15. uint nDeviceConnected = 0;
  16. void Start() {
  17. //INIT HR Display on usb device 0 and 1
  18. init_Device(0);
  19. AntChannel c = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0, 0, 120, 0, 57, 8070, false, 0);
  20. c.onChannelResponse += OnChannelResponse;
  21. c.onReceiveData += USB0;
  22. c.hideRXFAIL = true;
  23. init_Device(1);
  24. AntChannel d = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, 0, 0, 120, 0, 57, 8070, false, 1);
  25. d.onChannelResponse += OnChannelResponse;
  26. d.onReceiveData += USB1;
  27. d.hideRXFAIL = true;
  28. }
  29. void USB0(Byte[] data) {
  30. Debug.Log("USB 0 received data");
  31. }
  32. void USB1(Byte[] data) {
  33. Debug.Log("USB 1 received data");
  34. }
  35. bool firstDeviceInitialized = false;
  36. void init_Device(byte USBDeviceNum) {
  37. if (AntManager.Instance.devices[USBDeviceNum] == null) {
  38. AntManager.Instance.Init(USBDeviceNum);
  39. if (firstDeviceInitialized == false) { // device events need to be added only once
  40. firstDeviceInitialized = true;
  41. AntManager.Instance.onDeviceResponse += OnDeviceResponse;
  42. AntManager.Instance.onSerialError += OnSerialError; //if usb dongle is unplugged for example
  43. }
  44. }
  45. }
  46. void OnDeviceResponse(ANT_Response response) {
  47. ANT_Device device = response.sender as ANT_Device;
  48. Debug.Log("device (usb " + device.getOpenedUSBDeviceNum() + ") : " + response.getMessageID().ToString());
  49. }
  50. void OnSerialError(SerialError serialError) {
  51. Debug.Log("Error:" + serialError.error.ToString());
  52. //attempt to auto reconnect if the USB was unplugged
  53. if (serialError.error == ANT_Device.serialErrorCode.DeviceConnectionLost) {
  54. foreach (AntChannel channel in AntManager.Instance.channelList) {
  55. if (channel.device == serialError.sender)
  56. channel.PauseChannel();
  57. }
  58. nDeviceConnected = ANT_Common.getNumDetectedUSBDevices();
  59. StartCoroutine("Reconnect", serialError.sender.getSerialNumber());
  60. }
  61. }
  62. IEnumerator Reconnect(uint serial) {
  63. Debug.Log("looking for usb device " + serial.ToString());
  64. // polling to try and find the USB device
  65. while (true) {
  66. if (ANT_Common.getNumDetectedUSBDevices() > nDeviceConnected) {
  67. ANT_Device device = new ANT_Device();
  68. if (device.getSerialNumber() == serial) {
  69. Debug.Log("usb found!");
  70. AntManager.Instance.Reconnect(device);
  71. foreach (AntChannel channel in AntManager.Instance.channelList)
  72. channel.ReOpen(device);
  73. yield break;
  74. } else
  75. device.Dispose();
  76. }
  77. yield return new WaitForSeconds(0.1f);
  78. }
  79. }
  80. void OnChannelResponse(ANT_Response response) {
  81. }
  82. }