Sending Multiple Sensor Data From FEZ Spider via Bluetooth to PC

@ apprentice - glad to hear that it works for you. As you see in the program in the “FeedTimer_Tick” routine you must write your data in the specified buffer. From there it is automatically fetched and transferred via the bluetooth connection. A special (negative) characteristic of the program is, that it is necessary to make a pause in transferring data to the buffer from time to time. Only if there is a pause of more than “StdFeedTime” the data are fetched from the buffer and transferred via bluetooth. Be aware that the buffer of 64 k has no overflow protection (was neclected to achieve fast interrupt response). The program is not optimized for high speed data transfer.
The PC application is not yet ready, still struggling with som bugs.

PC app side is not overly hard. What is failing for you?

In my app, based largely on what Mike Dodaro’s example app does, I simply start a thread to read the BT stream.

            readThread = new Thread(new ThreadStart(btReadStream));
            readThread.Start();

The code for my stream reader is long and involved, I think, plus it has lots of unoptimised code and debug output that are probably not helping readability here, but here it is… The last portion of the code is where the UI thread is invoked using BeginInvoke.

        private void btReadStream()
        {
            int len;
            int maxlen = 2064;
            byte[] input = new byte[maxlen];
            bool foundEnd = false;
            int msgstart = 0, msgend = 0, msgscan=0;
            string sStatus = "";
            string sTC1 = tc1.ToString("99.99");
            string sTC2 = tc2.ToString("99.99");
            string sTime = "12:12:12." + loop.ToString("999");
            int curInput=0;
            byte[] subValue = new byte[8];

            while (true)
            {
                if (bluetoothStream != null)
                {
                    len = bluetoothStream.Read(input, curInput, maxlen-curInput);
                    if (len > 0)
                    {
                        curInput += len;
                        while (msgscan != curInput)
                        {
                            Console.WriteLine("Len=" + len + " new curInput = " + curInput);
                            for (int i = msgscan ; i < curInput && (!foundEnd); i++)
                            {
                                Console.WriteLine("ch= " + (char)input[i]);
                                if (!foundEnd)
                                {
                                    if (input[i] == delimCharEnd)
                                    {
                                        foundEnd = true;
                                        msgend = i;
                                    }
                                    else
                                        msgscan ++;
                                }

                            }
                            if (foundEnd)
                            {
 #if DEBUG
                                Console.WriteLine("1 msSt=" + msgstart + " msEnd=" + msgend + " msscan=" + msgscan);
 #endif
                                // extract from buffer
                                int whichTC = 1;
                                int subindex = 0;
                                float thisVal = 0.0f;
                                bool divisor = false;
                                int tenshundreds = 10;
                                for (int i = msgstart + 1; i <= msgend; i++)
                                {
 #if DEBUG
                                    Console.WriteLine("Inp=" + (char)input[i]);
 #endif
                                    if (input[i] != delimCharField && input[i] != delimCharEnd && input[i] != delimCharTime)
                                    {
                                        subValue[subindex++] = input[i];
                                        if (input[i] == '.')
                                            divisor = true;
                                        else
                                        {
                                            if (divisor)
                                            {
                                                int tempval = (input[i] - (int)'0');
                                                thisVal += (float)((float)tempval / (float)tenshundreds);
                                                tenshundreds *= 10;
                                            }
                                            else
                                                thisVal = thisVal * 10 + input[i] - (int)'0';
                                        }
                                    }
                                    else
                                    {
 #if DEBUG
                                        Console.WriteLine("Delim " + (char)input[i] + " found at " + i);
 #endif
                                        //                                    myValue = float(subValue.ToString());
                                        //                                    myValue = BitConverter.ToSingle(subValue, 0);
                                        Console.WriteLine("Val=" + thisVal.ToString("F4") + " " + thisVal);
                                        switch (whichTC)
                                        {
                                            case 1:
                                                tc1 = thisVal;
                                                sTC1 = tc1.ToString("F4");
                                                break;
                                            case 2:
                                                tc2 = thisVal;
                                                sTC2 = tc2.ToString("F4");
                                                break;
                                            case 3:
                                                sTime = thisVal.ToString("F0");
                                                break;
                                            case 4:
                                                if (thisVal.ToString("F0").Length ==1)
                                                    sTime += ":0" + thisVal.ToString("F0");
                                                else
                                                    sTime += ":" + thisVal.ToString("F0");
                                                break;
                                            case 5:
                                                if (thisVal.ToString("F0").Length ==1)
                                                    sTime += ":0" + thisVal.ToString("F0");
                                                else
                                                    sTime += ":" + thisVal.ToString("F0");
                                                break;
                                            case 6:
                                                if (thisVal.ToString("F0").Length ==1)
                                                    sTime += ":00" + thisVal.ToString("F0");
                                                else if (thisVal.ToString("F0").Length == 2)
                                                    sTime += ":0" + thisVal.ToString("F0");
                                                else
                                                    sTime += ":" + thisVal.ToString("F0");
                                                break;
                                        }

                                        whichTC++;
                                        subindex = 0;
                                        thisVal = 0.0f;
                                        divisor = false;
                                        tenshundreds = 10;
                                    }
                                }

 #if DEBUG
                                Console.WriteLine("3 msSt=" + msgstart + " msEnd=" + msgend + " msscan=" + msgscan + " curInp=" + curInput);
 #endif

                                // .... then eventually 
                                //rebuffer down to 0
                                int rebuf = curInput;
                                for (int i = 0; i < rebuf && i < maxlen; i++)
                                {
                                    input[i] = input[i + msgend + 1];
                                }
 #if DEBUG
                                Console.WriteLine("4 msSt=" + msgstart + " msEnd=" + msgend + " msscan=" + msgscan + " curInp=" + curInput);
 #endif
                                curInput = curInput - msgend - 1;
                                msgstart = 0;
                                msgend = 0;
                                msgscan = 0;
                                foundEnd = false;
 #if DEBUG
                                Console.WriteLine("5 msSt=" + msgstart + " msEnd=" + msgend + " msscan=" + msgscan + " curInp=" + curInput);
 #endif
                                // time to log the data
                                if (statusStartRecorderBtn == 1)
                                    log.LogTempAndTime(tc1, tc2, sTime);

                                // instance the event args and pass it each value
                                DisplayUpdateEventArgs args = new DisplayUpdateEventArgs(sStatus, sTC1, sTC2, sTime);
                                // Invoke the display handler with the updated arguments
                                Dispatcher.BeginInvoke(new DisplayUpdateHandler(MainWindow_DisplayUpdated), new object[] { args });
                                Thread.Sleep(1);

                            }
                        }
                    }
                }
            }

Hope that’s of some value to you.

@ Brett - Hi Brett, thank you for your code. It’s rather late now here in germany, so I cannot actually look on it. But I would really appreciate it, if you would have a look on my application when it is completed before I post it in the forum. Is this ok?

sure, happy to help, although I am far from an expert, as my code above should tell you :stuck_out_tongue:

@ RoSchmi - I’ve sent you a code that’ll be able to transfer the compass readings as you said , Please check your mail and could you tell me what’s wrong with that?