Bluetooth Default Setting -

@ Duke Nukem -
Did some tests with the Cerbuino Bee.
This constructor with some Thread.Sleep seems to work (at least sometimes)


       //Constructors changed by RoSchmi

            // Note: A constructor summary is auto-generated by the doc builder.
            /// <summary></summary>
            /// <param name="socketNumber">The socket that this module is plugged in to.</param>
            public Bluetooth(int socketNumber)
            {
                Complete_Common_Constructor(socketNumber, 38400);
            }

            // Note: A constructor summary is auto-generated by the doc builder.
            /// <summary></summary>
            /// <param name="socketNumber">The socket that this module is plugged in to.</param>
            /// <param name="baud">The baud rate to communicate with the module with.</param>
            public Bluetooth(int socketNumber, long baud)
            {
                
                // Throw an exception if a not allowed baudrate is passed as argument
                int pos = Array.IndexOf(possibleBaudrates, baud);
                if (!(pos >- 1)|(baud == 0))
                {
                    throw new System.ArgumentException("Argument of parameter baud (baudrate) not allowed. "
                                              + "Use 9600, 19200, 38400, 57600, 115200, 230400 or 460800");
                }
                Complete_Common_Constructor(socketNumber, baud);
            }

            private void Complete_Common_Constructor(int socketNumber, long baud)    // Common part of constructor
            {
                Thread.Sleep(1000);

                // This finds the Socket instance from the user-specified socket number.  
                // This will generate user-friendly error messages if the socket is invalid.
                // If there is more than one socket on this module, then instead of "null" for the last parameter, 
                // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
                Socket socket = Socket.GetSocket(socketNumber, true, this, null);

                this.reset = new GTI.DigitalOutput(socket, Socket.Pin.Six, false, this);
                this.statusInt = new GTI.InterruptInput(socket, Socket.Pin.Three, GTI.GlitchFilterMode.Off, GTI.ResistorMode.Disabled, GTI.InterruptMode.RisingAndFallingEdge, this);
                
                this.serialPort = new GTI.Serial(socket, (int)baud, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.One, 8, GTI.Serial.HardwareFlowControl.NotRequired, this);
                //this.statusInt.Interrupt += new GTI.InterruptInput.InterruptEventHandler(statusInt_Interrupt);
                this.serialPort.ReadTimeout = Timeout.Infinite;
                this.serialPort.Open();

                Debug.Print("Message from Class Bluetooth: Set up serialPort with baudrate: " + baud.ToString());
                
                Thread.Sleep(5);

                this.reset.Write(true);

               // Tests revealed that at least 470 msec are needed for Spider
               // Thread.Sleep(600);    // 600 for Spider and Cobra 2 ?
                  Thread.Sleep(1000);    // 1000 for Cerbuino Bee

                possibleBaudrates[0] = baud;   // try first to connect with baudrate passed to the constructor as argument
                                               // if not successful with this baudrate, we try all possible baudrates two times
                possibleBaudrates[8] = baud;   // preset for the second run

                Wait_For_Baudrates_Match = new AutoResetEvent(false);

                for (int i = 0; i < possibleBaudrates.Length; i++)
                {
                    if (possibleBaudrates[i] != baud)  // if baudrate of serialPort is not already
                    {                                  // set to the new value, set baudrate of serialPort
                        this.serialPort.Flush();       // to new value
                        Thread.Sleep(50);
                        this.serialPort.Close();
                        Thread.Sleep(50);
                        Debug.Print("Message from Class Bluetooth: Set serialPort baudrate to: " + possibleBaudrates[i].ToString());
                        this.serialPort.BaudRate = (int)possibleBaudrates[i];
                        Thread.Sleep(50);
                        this.serialPort.Open();
                        Thread.Sleep(50);

                    }
                    Debug.Print("Message from Class Bluetooth: Try to set BT-Module baudrate to: " + possibleBaudrates[i].ToString());
                    this.SetDeviceBaud(possibleBaudrates[i]);  // try to set baudrate of BT-Module on this baudrate
                                                               // if we have the baudrate to which the
                                                               // BT-Module is actually set we will get
                                                               // "OK" or "ERROR" and the bautrate is set to the new
                                                               // value, otherwise we get not readable signs and the
                                                               // new bautrate will not be set
                    
                    if (!readerThread_isRunning)  // if readerThread is not running, start new readerThread

                    {
                        readerThread = new Thread(new ThreadStart(runReaderThread));
                        readerThread.Start();
                        readerThread_isRunning = true;
                        Thread.Sleep(500);
                    }
                    
                   // if (Wait_For_Baudrates_Match.WaitOne(50,false)) // Wait for matching bautrates
                    if (Wait_For_Baudrates_Match.WaitOne(100, false)) // Wait for matching bautrates
                    {
                         Debug.Print("Message from Class Bluetooth: Could connect to BT-Module with baudrate " + possibleBaudrates[i].ToString());
                         Thread.Sleep(5);
                         if (possibleBaudrates[i] != baud)
                         {
                             Debug.Print("Message from Class Bluetooth: Set BT-Device_Baudrate to baudrate passed as argument: " + baud.ToString());
                             this.SetDeviceBaud(baud);   // Set BT-Module to baudrate passed as argument

                             this.serialPort.Flush();
                             Thread.Sleep(50);
                             this.serialPort.Close();
                             Thread.Sleep(50);
                             Debug.Print("Message from Class Bluetooth: Set serialPort to baudrate passed as argument: " + baud.ToString());
                             this.serialPort.BaudRate = (int)baud;
                             Thread.Sleep(50);
                             this.serialPort.Open();
                             Thread.Sleep(50);
                         }
                        break;
                    }
                    else
                    {
                        Debug.Print("Message from Class Bluetooth: Could not connect to BT-Module with baudrate " + possibleBaudrates[i].ToString());
                    }
                }
               
            }

An unhandled exception of type ‘System.OutOfMemoryException’ occurred, the NeuroSky MindWave Mobile pukes a lot of data, but I was thinking with the faster Cerberus I could process it faster, thus far maybe not. I tried using a Hydra but it blows up on setting up the SDCard as right now all I want to do is write the data received to an SDCard so I can check it for correctness.

@ Duke Nukem - did you work on the bluetooth driver in the meantime? I now have same time to work on it but maybe you already have a working solution so that there is no need to do it.
Regards
Roland

Last couple of days have been spent working on a WPF desktop app (got to make some money as much as I love playing with Gadgeteer, it doesn’t pay the bills) and watching VS2012 choke and die on WPF format typos, its enough to make you wonder sometimes.

@ Duke Nukem - OK, I will inform you, if I have something ready.

Sorry guys, don’t have a Gadgeteer BT module to play on at the moment to test it. But I honestly can’t recall an issue where it persists with the speed you set - not saying it’s not happening, but I can’t recall ever having to deal with that issue (then again, I may not have been concerned with it either!)

Oh and most of the work I was doing was back in 4.1 on a Hydra, so with the other recent discussions about 4.2 changes to serial port behaviour, I wouldn’t be surprised if special handling is needed now.

@ Brett -
Hi Brett, nice to see you again. Hope you have been on vacation. Hope you’ll soon have access to your BT-module.
Kind Regards
Roland

@ Duke Nukem -
any progress with your Bluetooth out of memory issue?

@ Duke Nukem -
Hi, Duke Nukem. Did you try my driver for the BT-module
https://www.ghielectronics.com/community/codeshare/entry/763
for your
NeuroSky MindWave Mobile
Regards Roland