Transferring the Compass Reading via Bluetooth

I’ve a modified code to transfer Bluetooth Data to PC. Using Tera Term I’ve figured out that this is working, but It’s having a latency and all the reading are comes after 5-6 seconds.

If there anyone who’s occurred such a problem , please help me to solve this. If you have any alternative way please post it as well. I’ve increased the baud rate but It’s the same output

@ andre.m - If you can give me your email I’ll send it to you

Here’s the Code

Declarations …

  // Buffer for fast access of incoming data
        static byte[] InputBuffer16 = new byte[UInt16.MaxValue + 1];
        static UInt16 InputBuffer16WritePtr = 0;
        static UInt16 InputBuffer16ReadPtr = 0;

        // ArrayList to take data from the buffer, the contents of the ArrayList is then sent over the bluetooth module
        ArrayList ALReadBytes16 = new ArrayList();

        // Gives the timeinterval to take data from fast buffer to ArrayList
        GT.Timer timerTakeToArrayList;
        // const int StdTakeTime = 2000; // (in ms) if no new bytes are read from MyI2C_Ctrl work can begin
        const int StdTakeTime = 200; // (in ms) if no new bytes are read from MyI2C_Ctrl work can begin

        // Gives the timeinterval to transmit data over bluetooth
        GT.Timer StoreTimer;
        // const int StdStoreTime = 4000; // (in ms) 60.000 = 1 min
        const int StdStoreTime = 400; // (in ms) 60.000 = 1 min

        // Gives the timeinterval to write test data in buffer
        GT.Timer FeedTimer;
        const int StdFeedTime = 50;
        //const int StdFeedTime = 5000;
        //const int StdFeedTime = 3000;
        //const int StdFeedTime = 300;

        static int FeedPauseCounter = 0;
        // Your choice: when this value is reached, i make a pause
        static int FeedPauseCount = 10;
        // Your choice: i make a pause of i.e. 2000 ms.
        static int FeedPauseLength = 1000;


        //Trigger Time
        GT.Timer TriggerTimer;
        const int TriggerTime = 3000;
 void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
 //set up a timer, when it ticks Data from the fastbuffer are read to ArrayList and are sent to bluetooth module
            timerTakeToArrayList = new GT.Timer(StdTakeTime);
            timerTakeToArrayList.Tick += new GT.Timer.TickEventHandler(timerTakeToArrayList_Tick);
            timerTakeToArrayList.Stop();

            //set up a timer, when it ticks data from the ArraList are sent over bluetooth
            StoreTimer = new GT.Timer(StdStoreTime);
            StoreTimer.Tick += new GT.Timer.TickEventHandler(StoreTimer_Tick);

            FeedTimer = new GT.Timer(StdFeedTime);
            FeedTimer.Tick += new GT.Timer.TickEventHandler(FeedTimer_Tick);


            //set up a timer to initialize Bluetooth some time after power up
            timerBluetoothInit = new GT.Timer(WaitToInitTime, GT.Timer.BehaviorType.RunOnce);
            timerBluetoothInit.Tick += new GT.Timer.TickEventHandler(timerBluetoothInit_Tick);
            timerBluetoothInit.Start();

            //set up at timer to reconnect if there is no connection
            timerTestConnection = new GT.Timer(CheckConnectionTime);
            timerTestConnection.Tick += new GT.Timer.TickEventHandler(timerTestConnection_Tick);
            timerTestConnection.Stop();   //the timer is started in the intitialize timer event



            // define a bluetooth module running on socket 9
            Debug.Print("Program Message: Make Bluetooth module on Port 8");
            bluetooth = new Bluetooth(9);

            Debug.Print("Program Message: Make client to use Client Mode");
            client = bluetooth.ClientMode;

            bluetooth.PinRequested += new Bluetooth.PinRequestedHandler(bluetooth_PinRequested);

            // need a handler for state changes and data recieved.//
            bluetooth.BluetoothStateChanged += new Bluetooth.BluetoothStateChangedHandler(bluetooth_BluetoothStateChanged);

            bluetooth.DataReceived += new Bluetooth.DataReceivedHandler(bluetooth_DataReceived);

            // not needed in this application
            //bluetooth.DeviceInquired += new Bluetooth.DeviceInquiredHandler(bluetooth_DeviceInquired);

            // Handler for button pressed. Button is used to enter pairing mode.
            //button.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(button_ButtonPressed);

            TriggerTimer = new GT.Timer(TriggerTime);
            TriggerTimer.Tick += new GT.Timer.TickEventHandler(Triggertime_Tick);
 timerTakeToArrayList.Start();
            StoreTimer.Start();
            if (DataInput == Input.Test)
            {
                FeedTimer.Start();
            }
            Debug.Print("Program Started finished");
        }

 #region TriggerTime
        void Triggertime_Tick(GT.Timer timer)
        {

            if (bluetooth.IsConnected)
            {
                Debug.Print("trigger has Activated");
            }
            // otherwise, just go into pairing mode.
            else
            {
                
                timerTestConnection.Stop(); // No events from timerTestConnection
                bluetooth.SetDeviceName(MyDeviceName);
                Thread.Sleep(100);
                bluetooth.SetPinCode(MyPinCode);
                Thread.Sleep(100);
                client.EnterPairingMode();
                timerTestConnection.Restart();
                CheckConnectionTimeDownCounter = 4; // Now wait 4 times the CheckConnectionTime 4 x 5 sec. until resetting the BT-module
            }
        }
        #endregion

 #region FeedTimer_Tick
        void FeedTimer_Tick(GT.Timer timer)
        {
            if (bluetooth.IsConnected)   // Write teststring in buffer only when bluetooth is connected
            {
                Debug.Print("________________________Feed timer ticked and bluetooth is connected");


                compass.RequestMeasurement();
                compass.MeasurementComplete += new Compass.MeasurementCompleteEventHandler(compass_MeasurementComplete);

 FeedPauseCounter++;
                if (FeedPauseCounter == FeedPauseCount)
                {
                    FeedTimer.Stop();
                    FeedPauseCounter = 0;
                    Thread.Sleep(FeedPauseLength);
                    FeedTimer.Start();

                }
            }
else
            {
                Debug.Print("________________________Feed timer ticked but bluetooth is not connected, no feed to buffer");
            }

        }

void compass_MeasurementComplete(Compass sender, Compass.SensorData sensorData)
        {
            string[] DP = new string[3];
            string thePackage;
            DP[0] = "X Axis" +sensorData.X.ToString() + "\n";
            DP[1] = "Y Axis"+sensorData.Y.ToString() + "\n";
            DP[2] = "Z Axis"+sensorData.Z.ToString() +"\n";
            thePackage = DP[0] + DP[1] + DP[2];
            byte[] DataMessage = Encoding.UTF8.GetBytes(thePackage);

            for (int i = 0; i < DataMessage.Length; i++)
            {
                InputBuffer16[InputBuffer16WritePtr] = DataMessage[i];
                InputBuffer16WritePtr++;
            }
        }
        #endregion

        #region StoreTimer_Tick
        void StoreTimer_Tick(GT.Timer timer)
        {
            timerTakeToArrayList.Stop();

            if (ALReadBytes16.Count > 0)
            {
                //Debug.Print("_______________________StoreTimer tick, Array contains Data");
                if (bluetooth.IsConnected)
                {
                    
                    byte[] SendArray = (byte[])ALReadBytes16.ToArray(typeof(byte));
                   
                    client.Send(SendArray);

                    ALReadBytes16.Clear();
                }
                else
                {
                    //Debug.Print("________________________StoreTimer tick, could not send, not connected");
                }
            }
            else
            {
                //Debug.Print("_______________________StoreTimer tick, Array is empty");
            }

            timerTakeToArrayList.Start();
        }
        #endregion

        #region timerTakeToArrayList_Tick
        void timerTakeToArrayList_Tick(GT.Timer timer)
        {
            //Debug.Print("timerTakeToArrayList_Tick - Take data from buffer to ArrayList");
            int Overflow16 = 0;
            if ((int)InputBuffer16ReadPtr > (int)InputBuffer16WritePtr)
            {
                Overflow16 = 65536;
            }
            //Debug.Print(InputBuffer16ReadPtr.ToString() + "  " + InputBuffer16WritePtr.ToString());
            while ((int)InputBuffer16ReadPtr < (int)InputBuffer16WritePtr + Overflow16)
            {

                //Debug.Print("InputBuffer16[" + InputBuffer16ReadPtr + "] " + InputBuffer16[InputBuffer16ReadPtr].ToString());
                ALReadBytes16.Add(InputBuffer16[InputBuffer16ReadPtr]);
                //InputBuffer16[InputBuffer16ReadPtr] = 0xFF;
                InputBuffer16ReadPtr++;
                if (InputBuffer16ReadPtr == 0)
                {
                    Overflow16 = 0;
                }
            }
        }
        #endregion