Example #1
0
        public static void StartClient(RequestType reqType, string reqText = "")
        {
            try
            {
                IPAddress ipAddress = IPAddress.Parse(Ip);
                var       remoteEP  = new IPEndPoint(ipAddress, Port);

                var client = new Socket(ipAddress.AddressFamily,
                                        SocketType.Stream, ProtocolType.Tcp);

                client.BeginConnect(remoteEP, ConnectCallback, client);
                connectDone.WaitOne();

                Send(client, "001lfglbhjgr<EOF>");
                sendDone.WaitOne();

                Receive(client);
                receiveDone.WaitOne();
                if (reqType == RequestType.TestConnection && response == "ok")
                {
                    ConnectionDone?.Invoke();
                }

                Debug.WriteLine($"Response received : {response}");

                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
    // Contructor.
    // scanResultsCallback -> Callback function that will be invoked once the Bluetooth scan for PLUX devices ends.
    // connectionDoneCallback -> Callback function that will be invoked once a connection with a PLUX device is established.
    public PluxDeviceManager(ScanResults scanResultsCallback, ConnectionDone connectionDoneCallback)
    {
        LazyObject    = new Lazy <BufferAcqSamples>(InitBufferedSamplesObject);
        PluxDevsFound = new Lazy <List <String> >(InitiListDevFound);

        // exceptionPointer -> Pointer to the callback function that will be used to send/communicate information about exceptions generated inside this .dll
        //                     The exception code and description will be sent to Unity where an appropriate action can take place.
        FPtrUnity dllCommunicationHandler = new FPtrUnity(DllCommunicationHandler);

        SetCommunicationHandler(dllCommunicationHandler);

        // Scan callback.
        this.ScanResultsCallback = new ScanResults(scanResultsCallback);

        // On connection successful callback.
        this.ConnectionDoneCallback = new ConnectionDone(connectionDoneCallback);

        // Initialise helper object that manages threads creating during the scanning and connection processes.
        var unitDispatcher = UnityThreadHelper.Dispatcher;

        // Specification of the callback function (defined on this/the user Unity script) which will receive the acquired data
        // samples as inputs.
        SetCallbackHandler(CallbackHandler);
    }