@ 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());
}
}
}