} /* InitSocket() */ #endregion #region CAN send/receive definition protected void TransmitData<T>(T obj) where T : struct { // get factory & instanciate en emplty shell message Ixxat.Vci4.IMessageFactory factory = Ixxat.Vci4.VciServer.Instance().MsgFactory; Ixxat.Vci4.Bal.Can.ICanMessage canMsg = (Ixxat.Vci4.Bal.Can.ICanMessage)factory.CreateMsg(typeof(Ixxat.Vci4.Bal.Can.ICanMessage)); // build up the data from object serializing byte[] data = AbstractCANHandle.Serialize<T>(obj); // configure the empty shell canMsg.TimeStamp = 0; canMsg.Identifier = 0x100; canMsg.FrameType = Ixxat.Vci4.Bal.Can.CanMsgFrameType.Data; canMsg.DataLength = (byte)data.Length; canMsg.SelfReceptionRequest = true; // show this message in the console window // fill up the data for (int idx = 0; idx < data.Length; ++idx) { canMsg[idx] = data[idx]; } // Write the CAN message into the transmit FIFO this.m_CANMessageWriter.SendMessage(canMsg); } /* TransmitData() */
static void Main(string[] args) { // simple test AbstractCANHandle handle = null; // init handle = new handle.ActiveSeatHandle(); handle.SelectDevice(); handle.InitSocket(0); (handle as handle.ActiveSeatHandle).BeginSession(); for (uint i = 0; i < 10; ++i) { (handle as handle.ActiveSeatHandle).BeginTrial(); (handle as handle.ActiveSeatHandle).Start( AngularAcceleration: /* rad/s^2 (SI) */ 6.8 + i, AngularSpeedSaturation: /* rad/s (SI) */ -2.5 + i, MaxStimDuration: /* ms (SI) */ 2.70 + i ); (handle as handle.ActiveSeatHandle).Stop(); (handle as handle.ActiveSeatHandle).Reset(); (handle as handle.ActiveSeatHandle).EndTrial(); } (handle as handle.ActiveSeatHandle).EndSession(); //handle.onHandleDeactivationRequested(); }
} /* Dispose() */ protected void Dispose(bool bDisposing = true) { // // Dispose all hold VCI objects. // // v1.0 // Dispose message reader //ApollonAbstractCANHandle.DisposeVciObject(this.m_CANMessageReader); // Dispose message writer AbstractCANHandle.DisposeVciObject(this.m_CANMessageWriter); // Dispose CAN channel AbstractCANHandle.DisposeVciObject(this.m_CANChannel); // Dispose CAN controller AbstractCANHandle.DisposeVciObject(this.m_CANController); // Dispose VCI device AbstractCANHandle.DisposeVciObject(this.m_VCIDevice); } /* Dispose(bool) */
// v1.0 // Event that's set if at least one message was received. //private System.Threading.AutoResetEvent m_RxEvent; #endregion #region CAN device selection // Select the first CAN adapter. public void SelectDevice() { // temporary Ixxat.Vci4.IVciDeviceManager deviceManager = null; Ixxat.Vci4.IVciDeviceList deviceList = null; System.Collections.IEnumerator deviceEnum = null; // encapsulate try { // Get device manager from VCI server deviceManager = Ixxat.Vci4.VciServer.Instance().DeviceManager; // Get the list of installed VCI devices deviceList = deviceManager.GetDeviceList(); // Get enumerator for the list of devices deviceEnum = deviceList.GetEnumerator(); // Get first CAN device // #TODO be more specific to select the apropriate device in list deviceEnum.MoveNext(); this.m_VCIDevice = deviceEnum.Current as Ixxat.Vci4.IVciDevice; // print the bus type, controller type, device name and serial number of first found controller Ixxat.Vci4.IVciCtrlInfo info = this.m_VCIDevice.Equipment[0]; object serialNumberGuid = this.m_VCIDevice.UniqueHardwareId; string serialNumberText = AbstractCANHandle.GetSerialNumberText(ref serialNumberGuid); //UnityEngine.Debug.Log( // "<color=Blue>Info: </color> ApollonAbstractCANHandle.SelectDevice() : found CAN device [{ BusType: " // + info.BusType // + " },{ ControllerType: " // + info.ControllerType // + " },{ Interface: " // + this.m_VCIDevice.Description // + " },{ Serial_Number: " // + serialNumberText // + " }]" //); } catch (System.Exception ex) { // log //UnityEngine.Debug.LogError( // "<color=Red>Error: </color> ApollonAbstractCANHandle.SelectDevice() : failed with error [" // + ex.Message // + "] => " // + ex.StackTrace //); } finally { // Dispose device manager ; it's no longer needed. AbstractCANHandle.DisposeVciObject(deviceManager); // Dispose device list ; it's no longer needed. AbstractCANHandle.DisposeVciObject(deviceList); // Dispose device list ; it's no longer needed. AbstractCANHandle.DisposeVciObject(deviceEnum); } /* try */ } /* SelectDevice() */
} /* SelectDevice() */ #endregion #region CAN socket init & setup // Opens the specified socket, creates a message channel, initializes // and starts the CAN controller. public bool InitSocket(System.Byte canNo) { // temp Ixxat.Vci4.Bal.IBalObject bal = null; bool bRslt = false; // encapsulate try { // Open bus access layer bal = this.m_VCIDevice.OpenBusAccessLayer(); // Open a message channel for the CAN controller this.m_CANChannel = bal.OpenSocket(canNo, typeof(Ixxat.Vci4.Bal.Can.ICanChannel)) as Ixxat.Vci4.Bal.Can.ICanChannel; // Open the scheduler of the CAN controller this.m_CANScheduler = bal.OpenSocket(canNo, typeof(Ixxat.Vci4.Bal.Can.ICanScheduler)) as Ixxat.Vci4.Bal.Can.ICanScheduler; // Initialize the message channel // v1.0 //this.m_CANChannel.Initialize( // receiveFifoSize: // 1024, // transmitFifoSize: // 128, // exclusive: // false //); // v2.0 // dont need buffer as there isnt on ADwin side this.m_CANChannel.Initialize( receiveFifoSize: 1, transmitFifoSize: 1, exclusive: true ); // v1.0 //// Get a message reader object //this.m_CANMessageReader = this.m_CANChannel.GetMessageReader(); // v1.0 //// Initialize message reader //this.m_CANMessageReader.Threshold = 1; // v1.0 //// Create and assign the event that's set if at least one message was received. //this.m_RxEvent = new System.Threading.AutoResetEvent(false); //this.m_CANMessageReader.AssignEvent(this.m_RxEvent); // Get a message wrtier object this.m_CANMessageWriter = this.m_CANChannel.GetMessageWriter(); // v1.0 //// Initialize message writer //this.m_CANMessageWriter.Threshold = 1; // Activate the message channel this.m_CANChannel.Activate(); // Open the CAN controller this.m_CANController = bal.OpenSocket(canNo, typeof(Ixxat.Vci4.Bal.Can.ICanControl)) as Ixxat.Vci4.Bal.Can.ICanControl; // Initialize the CAN controller // v1.0 //this.m_CANController.InitLine( // operatingMode: // Ixxat.Vci4.Bal.Can.CanOperatingModes.Standard // | Ixxat.Vci4.Bal.Can.CanOperatingModes.Extended // | Ixxat.Vci4.Bal.Can.CanOperatingModes.ErrFrame, // bitrate: // Ixxat.Vci4.Bal.Can.CanBitrate.Cia125KBit //); // v2.0 // Dont need extended mode (11 bits is enought) this.m_CANController.InitLine( operatingMode: Ixxat.Vci4.Bal.Can.CanOperatingModes.Standard, bitrate: Ixxat.Vci4.Bal.Can.CanBitrate.Cia125KBit ); // log //UnityEngine.Debug.Log( // "<color=Blue>Info: </color> ApollonAbstractCANHandle.InitSocket() : setup CAN socket [{ LineStatus: " // + this.m_CANController.LineStatus // + " }]" //); // v1.0 //// Set the acceptance filter for std identifiers //this.m_CANController.SetAccFilter( // select: // Ixxat.Vci4.Bal.Can.CanFilter.Std, // code: // (uint)Ixxat.Vci4.Bal.Can.CanAccCode.All, // mask: // (uint)Ixxat.Vci4.Bal.Can.CanAccMask.All //); // v1.0 //// Set the acceptance filter for ext identifiers //this.m_CANController.SetAccFilter( // select: // Ixxat.Vci4.Bal.Can.CanFilter.Ext, // code: // (uint)Ixxat.Vci4.Bal.Can.CanAccCode.All, // mask: // (uint)Ixxat.Vci4.Bal.Can.CanAccMask.All //); // Start the CAN controller this.m_CANController.StartLine(); // success bRslt = true; } catch (System.Exception ex) { // log //UnityEngine.Debug.LogError( // "<color=Red>Error: </color> ApollonAbstractCANHandle.InitSocket() : failed with error [" // + ex.Message // + "] => " // + ex.StackTrace //); // fail bRslt = false; } finally { // Dispose bus access layer AbstractCANHandle.DisposeVciObject(bal); } /* try */ // return return bRslt; } /* InitSocket() */