BT <--> Gadgeteer baud rate increase

May 31, 2012 at 11:39 AM

I thought my app might need a faster baud rate to get more data between the Gadgeteer board and the paired device, so I added a new constructor and a baud rate switch method.  They may be useful to someone else so I thought I'd contribute them here.  Let me know if they're worth committing back into source?

Bye

Brett

--------------------------------

           // 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)

           {

                // 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, 38400, 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();

 

               Thread.Sleep(5);

               this.reset.Write(true);

 

               Thread.Sleep(5);

               this.SetDeviceBaud(baud);

               this.serialPort.Flush();

               this.serialPort.Close();

               this.serialPort.BaudRate = baud;

               this.serialPort.Open();

 

               readerThread = new Thread(new ThreadStart(runReaderThread));

               readerThread.Start();

               Thread.Sleep(500);

           }                           

                              --------------------------------

           /// <summary>

           /// Switch the device to the directed speed

           /// </summary>

           /// <param baud="number">Name of the device</param>

           public void SetDeviceBaud(long baud)

           {

               string cmd = "";

               switch (baud)

              {

                   case 9600:

                       cmd = "9600";

                       break;

                   case 19200:

                       cmd = "19200";

                       break;

                   case 38400:

                        cmd = "38400";

                       break;

                   case 57600:

                       cmd = "57600";

                       break;

                   case 115200:

                       cmd = "115200";

                       break;

                   case 230400:

                       cmd="230400";

                       break;

                   case 460800:

                       cmd="460800";

                       break;

                   default:

                       cmd="";

                       break;

               }

 

               if (cmd != "")

                   this.serialPort.Write("\r\n+STBD=" + cmd + "\r\n");

               //todo: check it is working?! Probably should check the return code and do something about it. in the meantime,

               Thread.Sleep(500);

           }