private void ButtonSendReadingCount_Click(object sender, RoutedEventArgs e) { if (TextBoxReadingCount.Text.Length > 4) { MessageBox.Show("Reading value too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] data = new byte[4]; byte[] value = System.Text.Encoding.UTF8.GetBytes(TextBoxReadingCount.Text); for (int i = 0; i < 4; i++) { if (i < value.Length) { data[i] = value[i]; } else { data[i] = 0; } } if (!CommandHost.SendReadingCount(_uart, data)) { MessageBox.Show("Transmission error sending reading count value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendDisplay_Click(object sender, RoutedEventArgs e) { if (TextBoxDisplay.Text.Length > 40) { MessageBox.Show("Display value too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] data = new byte[20]; byte[] value = Utilities.StringToByteArray(TextBoxDisplay.Text); //System.Text.Encoding.UTF8.GetBytes(TextBoxDisplay.Text); for (int i = 0; i < 20; i++) { if (i < value.Length) { data[i] = value[i]; } else { data[i] = 0; } } if (!CommandHost.SendDisplay(_uart, data)) { MessageBox.Show("Transmission error sending display value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendChannelOff_Click(object sender, RoutedEventArgs e) { byte[] data = new byte[8]; byte[] floatData; float value1, value2; if (!Single.TryParse(TextBoxCH1.Text, out value1)) { MessageBox.Show("Could not parse Channel 1 offset value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Single.TryParse(TextBoxCH2.Text, out value2)) { MessageBox.Show("Could not parse Channel 2 offset value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // set floats floatData = BitConverter.GetBytes(value1); data[0] = floatData[0]; data[1] = floatData[1]; data[2] = floatData[2]; data[3] = floatData[3]; floatData = BitConverter.GetBytes(value2); data[4] = floatData[0]; data[5] = floatData[1]; data[6] = floatData[2]; data[7] = floatData[3]; if (!CommandHost.SendChannelOff(_uart, data)) { MessageBox.Show("Transmission error sending channel offset readings.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendDeviceTime_Click(object sender, RoutedEventArgs e) { if (TextBoxDeviceTime.Text.Length > 8) { MessageBox.Show("Time number too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] data = new byte[4]; byte[] value = Utilities.StringToByteArray(TextBoxDeviceTime.Text); for (int i = 0; i < 4; i++) { if (i < value.Length) { data[i] = value[i]; } else { data[i] = 0; } } if (!CommandHost.SendDeviceTime(_uart, data)) { MessageBox.Show("Transmission error sending time value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendSerialNumber_Click(object sender, RoutedEventArgs e) { if (TextBoxSerialNumber.Text.Length > 10) { MessageBox.Show("Serial number too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] data = new byte[10]; byte[] value = System.Text.Encoding.UTF8.GetBytes(TextBoxSerialNumber.Text); for (int i = 0; i < 10; i++) { if (i < value.Length) { data[i] = value[i]; } else { data[i] = 0; } } if (!CommandHost.SendSerialNumber(_uart, data)) { MessageBox.Show("Transmission error sending serial number value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSetBluetoothState_Click(object sender, RoutedEventArgs e) { if (TextBoxBluetoothState.Text.Length > 2) { MessageBox.Show("Bluetooth state value too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] value = Utilities.StringToByteArray(TextBoxBluetoothState.Text); if (!CommandHost.SendBluetoothState(_uart, value)) { MessageBox.Show("Transmission error sending Bluetooth state.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendDataPacket_Click(object sender, RoutedEventArgs e) { byte[] data = new byte[12]; byte[] floatData; int hours, minutes, seconds; float value1, value2; if (!Single.TryParse(TextBoxTemp1.Text, out value1)) { MessageBox.Show("Could not parse temperature 1 value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Single.TryParse(TextBoxTemp2.Text, out value2)) { MessageBox.Show("Could not parse temperature 2 value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // upper byte not used data[0] = 0; hours = DateTime.Now.Hour; minutes = DateTime.Now.Minute; seconds = DateTime.Now.Second; // set time data[1] = (byte)hours; data[2] = (byte)minutes; data[3] = (byte)seconds; // set floats floatData = BitConverter.GetBytes(value1); data[4] = floatData[0]; data[5] = floatData[1]; data[6] = floatData[2]; data[7] = floatData[3]; floatData = BitConverter.GetBytes(value2); data[8] = floatData[0]; data[9] = floatData[1]; data[10] = floatData[2]; data[11] = floatData[3]; if (!CommandHost.SendDataPacket(_uart, data)) { MessageBox.Show("Transmission error sending data packet.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendLogMode_Click(object sender, RoutedEventArgs e) { if (TextBoxLoggingMode.Text.Length > 1) { MessageBox.Show("Logging mode value too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] data = new byte[1]; byte[] value = System.Text.Encoding.UTF8.GetBytes(TextBoxLoggingMode.Text); data[0] = value[0]; if (!CommandHost.SendLogMode(_uart, data)) { MessageBox.Show("Transmission error sending logging mode value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendSecondDisplay_Click(object sender, RoutedEventArgs e) { byte[] data = new byte[1]; if (TextBoxSecondDisplay.Text.Length > 1) { MessageBox.Show("Second display number too lengthy.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } byte[] value = System.Text.Encoding.UTF8.GetBytes(TextBoxSecondDisplay.Text); data[0] = value[0]; if (!CommandHost.SendSecondDisplayReadings(_uart, data)) { MessageBox.Show("Transmission error sending second display readings.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void Each_Tick(object o, EventArgs sender) { int temp1 = rnd1.Next(Min, Max); int temp2 = rnd2.Next(Min, Max); byte[] data = new byte[12]; byte[] floatData; int hours, minutes, seconds; // upper byte not used data[0] = 0; hours = DateTime.Now.Hour; minutes = DateTime.Now.Minute; seconds = DateTime.Now.Second; // set time data[1] = (byte)hours; data[2] = (byte)minutes; data[3] = (byte)seconds; // set floats floatData = BitConverter.GetBytes(temp1); data[4] = floatData[0]; data[5] = floatData[1]; data[6] = floatData[2]; data[7] = floatData[3]; floatData = BitConverter.GetBytes(temp2); data[8] = floatData[0]; data[9] = floatData[1]; data[10] = floatData[2]; data[11] = floatData[3]; if (!CommandHost.SendDataPacket(_uart, data)) { MessageBox.Show("Transmission error sending data packet.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonSendBattVolt_Click(object sender, RoutedEventArgs e) { byte[] data = new byte[4]; byte[] floatData; float value1; if (!Single.TryParse(TextBoxBatteryVoltage.Text, out value1)) { MessageBox.Show("Could not parse battery value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // set floats floatData = BitConverter.GetBytes(value1); data[0] = floatData[0]; data[1] = floatData[1]; data[2] = floatData[2]; data[3] = floatData[3]; if (!CommandHost.SendBatteryVolt(_uart, data)) { MessageBox.Show("Transmission error sending battery readings.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void ButtonGetBluetoothState_Click(object sender, RoutedEventArgs e) { CommandHost.GetBluetoothState(_uart); }
private void ButtonSendStat_Click(object sender, RoutedEventArgs e) { byte[] data = new byte[20]; byte[] floatData; float value1, value2, value3, value4, value5; if (!Single.TryParse(TextBoxMin.Text, out value1)) { MessageBox.Show("Could not parse minimum value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Single.TryParse(TextBoxMax.Text, out value2)) { MessageBox.Show("Could not parse maximum value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Single.TryParse(TextBoxAvg.Text, out value3)) { MessageBox.Show("Could not parse average value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Single.TryParse(TextBoxRange.Text, out value4)) { MessageBox.Show("Could not parse range value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (!Single.TryParse(TextBoxSTD.Text, out value5)) { MessageBox.Show("Could not parse standard deviation value.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // set floats floatData = BitConverter.GetBytes(value1); data[0] = floatData[0]; data[1] = floatData[1]; data[2] = floatData[2]; data[3] = floatData[3]; floatData = BitConverter.GetBytes(value2); data[4] = floatData[0]; data[5] = floatData[1]; data[6] = floatData[2]; data[7] = floatData[3]; floatData = BitConverter.GetBytes(value3); data[8] = floatData[0]; data[9] = floatData[1]; data[10] = floatData[2]; data[11] = floatData[3]; floatData = BitConverter.GetBytes(value4); data[12] = floatData[0]; data[13] = floatData[1]; data[14] = floatData[2]; data[15] = floatData[3]; floatData = BitConverter.GetBytes(value5); data[16] = floatData[0]; data[17] = floatData[1]; data[18] = floatData[2]; data[19] = floatData[3]; if (!CommandHost.SendStat(_uart, data)) { MessageBox.Show("Transmission error sending data packet.", "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void Read() { while (_continue) { try { int bytesToRead = _serialPort.BytesToRead; if (bytesToRead > 0) { bytesToRead = bytesToRead + _readBytes < UART_READ_BUFFER_SIZE ? bytesToRead : UART_READ_BUFFER_SIZE - _readBytes; _serialPort.Read(_readBuffer, _readBytes, bytesToRead); _readBytes += bytesToRead; if (_readBuffer[_readBytes - 1] == '\r') { CommandHost.TegamServiceCode_t serviceCode; byte[] data; byte[] decodedBuf = new byte[_readBytes + 1]; // decoded what was read from UART COBS.Decode(Encoding.Default.GetBytes(_readBuffer), (ushort)(_readBytes + 1), ref decodedBuf); if (CommandHost.Parse(decodedBuf, out serviceCode, out data)) { switch (serviceCode) { case CommandHost.TegamServiceCode_t.TEGAM_ERROR: MessageBox.Show("Received an error from Tegam 919!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); break; case CommandHost.TegamServiceCode_t.TEGAM_BLUETOOTH_GET_ACK: MessageBox.Show("Current Bluetooth state: " + data[0]); break; case CommandHost.TegamServiceCode_t.TEGAM_MODEL_NUMBER_SET_ACK: //MessageBox.Show("Received ACK for model number set."); break; case CommandHost.TegamServiceCode_t.TEGAM_DATA_PACKET_UPD_ACK: //MessageBox.Show("Received ACK for data packet update."); break; case CommandHost.TegamServiceCode_t.TEGAM_SEND_NEXT_READING_SET_REQ: MessageBox.Show("Received request to send next reading. Value: " + data[0]); break; case CommandHost.TegamServiceCode_t.TEGAM_CHANNEL_READINGS_SET_ACK: //MessageBox.Show("Received ACK for channel readings set."); break; } } ResetReadBuffer(); } } } catch (TimeoutException) { } } }