private void bwDevInfo_DoWork(object sender, DoWorkEventArgs e) { // Check COM port is still open. if (_serialPort.IsOpen) { // Request version information from bluetooth module. _deviceState = HC6State.HC6Version; e.Result = sendAtCommand(("AT+VERSION").ToCharArray()); } else { // Return port closed status in thread results. e.Result = HC6Error.HC6ErrorPortClosed; } }
private void bwDevUpdate_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // Check for any error result. If error detected cancel device update process and return. if (((HC6Error)(e.Result)) != HC6Error.HC6Success) { _deviceState = HC6State.HC6Idle; this.Invoke((MethodInvoker) delegate { btnDevUpdate.Enabled = true; txtStatus.Text = "Bluetooth module update failed."; MessageBox.Show("Bluetooth module configuration update failed.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); }); } }
private void bwDevInfo_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // Check for any worker related error codes. if (((HC6Error)e.Result) != HC6Error.HC6Success) { this.Invoke((MethodInvoker) delegate { txtStatus.Text = HC6ErrorToString((HC6Error)e.Result); // Switch application to idle state due to error. _deviceState = HC6State.HC6Idle; updateErrorUI(); }); return; } // Now application should received information from bluetooth module... txtStatus.Text = "Connected"; tmDevProbe.Enabled = true; }
private void bwDevUpdate_DoWork(object sender, DoWorkEventArgs e) { // Check COM port is still open. if (_serialPort.IsOpen) { // Update display name of the bluetooth module. if (_deviceState == HC6State.HC6Name) { e.Result = sendAtCommand(("AT+NAME" + _devName).ToCharArray()); } else if (_deviceState == HC6State.HC6Password) { // Update bluetooth pair password. e.Result = sendAtCommand(("AT+PIN" + _devPinCode).ToCharArray()); } else if (_deviceState == HC6State.HC6Baud) { // Update baud rate of the bluetooth module. e.Result = sendAtCommand(("AT+BAUD" + (_devBaudRate + 1).ToString("X")).ToCharArray()); } else if (_deviceState == HC6State.HC6Parity) { // Update parity check type of the module. (Available from version 1.5). string parityCode = "PN"; if (_devParity > 0) { parityCode = (_devParity == 1) ? "PO" : "PE"; } e.Result = sendAtCommand(("AT+" + parityCode).ToCharArray()); } } else { // Return port closed status in thread results. e.Result = HC6Error.HC6ErrorPortClosed; _deviceState = HC6State.HC6Idle; } }
private void btnDevUpdate_Click(object sender, EventArgs e) { int pairPassword = 0; // Validate bluettoth module's device name. if (txtDevName.Text.Trim() == string.Empty) { MessageBox.Show("Bluetooth module name is not specified.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtDevName.Focus(); return; } // Validate bluettoth pair password. if (txtDevPassword.Text.Trim() == string.Empty) { MessageBox.Show("Bluetooth pair password is not specified.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtDevPassword.Focus(); return; } // Check format of the bluetooth pair password. if ((!int.TryParse(txtDevPassword.Text.Trim(), out pairPassword)) || (txtDevPassword.Text.Trim().Length != 4)) { MessageBox.Show("Bluetooth pair password is not valid.\nPair password should be 4-bit number.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); txtDevPassword.Focus(); return; } _devName = txtDevName.Text.Trim(); _devPinCode = txtDevPassword.Text.Trim(); _devBaudRate = cmbDevBaud.SelectedIndex; _devParity = cmbDevParity.SelectedIndex; btnDevUpdate.Enabled = false; // Start module configuration update process. _deviceState = HC6State.HC6Name; bwDevUpdate.RunWorkerAsync(); }
public frmMain() { InitializeComponent(); _deviceState = HC6State.HC6Idle; }
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Wait for addtional data to get filled into COM port read buffer. System.Threading.Thread.Sleep(500); // Check for data related with HC6 version. if (_deviceState == HC6State.HC6Version) { string retMessage = _serialPort.ReadExisting(); // Stop timeout counter. tmDevProbe.Enabled = false; if (retMessage.Trim().Length > 0) { // Trim "OK" prefix from the received version string. if (retMessage.Substring(0, 2).ToUpper() == "OK") { retMessage = retMessage.Substring(2, retMessage.Length - 2); } // Update UI with received version information. txtVersion.Invoke((MethodInvoker) delegate { txtVersion.Text = retMessage; }); } // Application finished initialization. _deviceState = HC6State.HC6Idle; this.Invoke((MethodInvoker) delegate { setConnectionMode(true); }); } else if (_deviceState == HC6State.HC6Name) { // Device name update completed. Move to next update state. System.Threading.Thread.Sleep(50); _deviceState = HC6State.HC6Password; bwDevUpdate.RunWorkerAsync(); } else if (_deviceState == HC6State.HC6Password) { // Device pair password update completed. Move to next update state. System.Threading.Thread.Sleep(50); _deviceState = HC6State.HC6Baud; bwDevUpdate.RunWorkerAsync(); } else if (_deviceState == HC6State.HC6Baud) { // Device baud rate update completed. Move to next update state. System.Threading.Thread.Sleep(50); _deviceState = HC6State.HC6Parity; bwDevUpdate.RunWorkerAsync(); } else if (_deviceState == HC6State.HC6Parity) { // Device parity check type updated. Move to next update state. this.Invoke((MethodInvoker) delegate { btnDevUpdate.Enabled = true; MessageBox.Show("Bluetooth module configuration updated successfully.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }); } }