private static string ReadData_Test(BluetoothCmd bcmd) { StringBuilder sb = new StringBuilder(); if (oRandom == null) { oRandom = new Random(); } sb.Append(bcmd.Cmd); if (!string.IsNullOrEmpty(bcmd.sExpression)) { for (int i = 0; i < bcmd.Bytes; i++) { sb.Append(oRandom.Next(0, 255).ToString("X2")); } } else { sb.Append("FF"); } if (sb.Length == 0 || sb.Length > MAX_LENGTH) { return(string.Empty); } return(sb.ToString()); }
public async Task <bool> SendCommandAsync(BluetoothCmd bcmd) { BluetoothConnection bc = oBluetoothConnection; if (bc == null || !bc.oBthSocket.IsConnected) { return(false); } if (isDebug) { Debug.WriteLine("Tx: " + bcmd.Cmd); } bcmd.sbResponse.Clear(); try { await bc.oBthSocket.OutputStream.WriteAsync(bcmd.CmdBytes, 0, bcmd.CmdBytes.Length); await bc.oBthSocket.OutputStream.FlushAsync(); bcmd.sbResponse.Append(ReadData(bc.oBthSocket)); bcmd.tx_good++; return(ValidateResponse(bcmd)); } catch (Exception e) { status_message = string.Format("{0}: {1}", "Read Error", e.Message); bcmd.tx_fail++; return(false); } }
private static bool ProcessResponse(BluetoothCmd bcmd) { // Trim the returned value portion bcmd.Response = bcmd.sbResponse.ToString().Substring(bcmd.Cmd.Length).Trim(); // Check if valid if (!string.IsNullOrEmpty(bcmd.Response)) { // If a string message: update counter, return as success if (!bcmd.isRxBytes) { bcmd.rx_good++; return(true); } // If we are expecting bytes returned and response is valid if (bcmd.Bytes != 0 && !bcmd.Response.StartsWith(RX_MESSAGE, StringComparison.OrdinalIgnoreCase)) { // Attempt to parse the hex value if (int.TryParse(bcmd.Response.Substring(bcmd.Response.Length - (bcmd.Bytes * 2)), NumberStyles.HexNumber, null, out int result)) { // Store result and call math expression parser bcmd.rxvalue = result; if (bcmd.Calculate()) { bcmd.rx_good++; return(true); } } } } // Response was invalid: update counter and return as failed bcmd.rx_fail++; return(false); }
public bool SendCommandAsync_Test(BluetoothCmd bcmd) { if (isDebug) { Debug.WriteLine("Tx: " + bcmd.Cmd); } bcmd.sbResponse.Clear(); try { bcmd.sbResponse.Append(ReadData_Test(bcmd)); bcmd.tx_good++; bool result = ValidateResponse(bcmd); //if (isDebug) // Debug.WriteLine("Rx: {0} Valid:{1}", response, result); return(result); } catch (Exception e) { status_message = string.Format("{0}: {1}", "Read Error", e.Message); bcmd.tx_fail++; if (isDebug) { Debug.WriteLine(status_message); } return(false); } }
/// <summary> /// Initializes a given commands math expression based on supplied unit type /// </summary> /// <param name="bthcmd"></param> /// <param name="Unit_Type"></param> public void InitExpression(BluetoothCmd bthcmd, UserSetting.UNIT_TYPE Unit_Type) { if (bthcmd.Command_Types == null || bthcmd.Command_Types.Length == 0) { return; } InitUnitType(bthcmd, Unit_Type); if (string.IsNullOrEmpty(sExpression)) { return; } string varvalue; e = null; arguments = null; e = new Expression(sExpression.Trim()); arguments = new List <Argument>(); for (int idx = 0; idx < bthcmd.Command_Types.Length; idx++) { varvalue = ProcessValues.GetVariable(idx); arguments.Add(new Argument(varvalue)); e.addArguments(arguments[idx]); //arguments[idx].setArgumentValue(0); // ToDo: initial value? } min = bthcmd.Value_Min; max = bthcmd.Value_Max; decimals = bthcmd.Decimals; }
/// <summary> /// Initializes a given commands math expression based on supplied unit type /// </summary> /// <param name="bthcmd"></param> /// <param name="Unit_Type"></param> /// <param name="sb"></param> public void InitExpression(BluetoothCmd bthcmd, UserSettings.UNIT_TYPE Unit_Type, StringBuilder sb) { InitUnitType(bthcmd, Unit_Type); if (string.IsNullOrEmpty(sExpression)) { return; } string varvalue; e = null; arguments = null; e = new Expression(sExpression.Trim()); arguments = new List <Argument>(); for (int idx = 0; idx < bthcmd.Command_Types.Length; idx++) { varvalue = ProcessValues.GetVariable(idx); arguments.Add(new Argument(varvalue)); e.addArguments(arguments[idx]); //arguments[idx].setArgumentValue(0); // ToDo: initial value? } min = bthcmd.Value_Min; max = bthcmd.Value_Max; decimals = bthcmd.Decimals; if (decimals < 0) { decimals = 0; } else if (decimals > MAX_DECIMALS) { decimals = MAX_DECIMALS; } if (decimals == 0) { formatter = VALUE_FORMATTER; } else { sb.Clear(); sb.Append(VALUE_FORMATTER); sb.Append("."); for (int i = 0; i < decimals; i++) { sb.Append(VALUE_FORMATTER); } formatter = sb.ToString(); } }
private static bool ValidateResponse(BluetoothCmd bcmd) { if (bcmd.sbResponse.Length == 0 || bcmd.sbResponse.Length <= bcmd.Cmd.Length) // If response is empty or length is invalid { return(false); } string msg = bcmd.sbResponse.ToString().Substring(0, bcmd.Cmd.Length); // Isolate the expected echoed value return(msg.Equals(bcmd.Cmd, StringComparison.OrdinalIgnoreCase) && ProcessResponse(bcmd)); // Return if echoed portion found and the processing result }
/// <summary> /// Initializes unique DTC commands /// </summary> public Dtc() { lst0103 = new List <string>(); VehicleStatus = new EngineStatus(); DTC_READ = new BluetoothCmd() { Cmd = "01051", Rate = 10000, isRxBytes = true, Bytes = 1 }; DTC_CLEAR = new BluetoothCmd() { Cmd = "04", Rate = 5000, isRxBytes = true, Bytes = 1, Command_Types = new[] { BlueToothCmds.COMMAND_TYPE.DTC_CLEAR } }; }
public static List <BluetoothCmd> CreateList() { BluetoothCmd coolant_temp = new BluetoothCmd() { Id = 1, Name = "coolant temp", Units_Imperial = "F", Units_Metric = "C", Cmd = "01051", Rate = 3000, Decimals = 0, Expression_Imperial = "((a-40)*9/5+32)", Expression_Metric = "(a - 40)", Bytes = 4, isRxBytes = true, isSelected = false, Value_Min = -40, Value_Max = 215, sCommand_Types = COMMAND_DEFAULT }; BluetoothCmd vss = new BluetoothCmd() { Id = 2, Name = "VSS", Units_Imperial = "MPH", Units_Metric = "KPH", Cmd = "010D1", Rate = 1000, Decimals = 1, Expression_Imperial = "(a / 1.609)", Expression_Metric = "", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = true, Value_Min = 0, Value_Max = 255 }; BluetoothCmd iat = new BluetoothCmd() { Id = 3, Name = "IAT", Units_Imperial = "F", Units_Metric = "C", Cmd = "010F1", Rate = 1000, Decimals = 1, Expression_Imperial = "((a-40)*9/5+32)", Expression_Metric = "(a - 40)", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = -40, Value_Max = 215 }; BluetoothCmd map = new BluetoothCmd() { Id = 4, Name = "MAP", Units_Imperial = "PSI", Units_Metric = "KPA", Cmd = "010B1", Rate = 1000, Decimals = 1, Expression_Imperial = "(a*0.145)", Expression_Metric = "(a)", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 255 }; BluetoothCmd tps = new BluetoothCmd() { Id = 5, Name = "TPS", Units_Imperial = "%", Units_Metric = "%", Cmd = "01111", Rate = 500, Decimals = 0, Expression_Imperial = "(a * 100 / 255)", Expression_Metric = "(a * 100 / 255)", Bytes = 1, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 100 }; BluetoothCmd maf = new BluetoothCmd() { Id = 6, Name = "MAF", Units_Imperial = "G/S", Units_Metric = "G/S", Cmd = "010D1", Rate = 1000, Decimals = 1, Expression_Imperial = "(a * 1)", Expression_Metric = "(a * 1)", Bytes = 1, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 10000 }; BluetoothCmd rpm = new BluetoothCmd() { Id = 7, Name = "RPM", Units_Imperial = "x1000", Units_Metric = "x1000", Cmd = "010C1", Rate = 500, Decimals = 0, Expression_Imperial = "(a * 1)", Expression_Metric = "(a * 1)", Bytes = 1, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 10000 }; BluetoothCmd mpg = new BluetoothCmd() { Id = 8, Name = "miles per gallon", Units_Imperial = "MPG", Units_Metric = "KPG", Cmd = "", Rate = 2000, Decimals = 1, Expression_Imperial = "(14.7*b*1740.572)/(3600*c/100)", Expression_Metric = "(14.7*b*1740.572)/(3600*c/100)", Bytes = 0, isRxBytes = false, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 100 }; BluetoothCmd fuel_system_status = new BluetoothCmd() { Id = 9, Name = "fuel system status", Units_Imperial = "", Units_Metric = "", Cmd = "01031", Rate = 5000, Decimals = 0, Expression_Imperial = "", Expression_Metric = "", Bytes = 4, isRxBytes = false, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Max = 0, Value_Min = 0 }; BluetoothCmd fuel_pressure = new BluetoothCmd() { Id = 10, Name = "fuel pressure", Units_Imperial = "", Units_Metric = "", Cmd = "010A1", Rate = 500, Decimals = 0, Expression_Imperial = "(100/128 * a - 100)", Expression_Metric = "", Bytes = 4, isRxBytes = true, sCommand_Types = COMMAND_DEFAULT, isSelected = false, Value_Min = 0, Value_Max = 765 }; List <BluetoothCmd> commands_list = new List <BluetoothCmd>(); commands_list.Add(coolant_temp); commands_list.Add(vss); commands_list.Add(iat); commands_list.Add(map); commands_list.Add(tps); commands_list.Add(maf); commands_list.Add(rpm); commands_list.Add(mpg); commands_list.Add(fuel_system_status); commands_list.Add(fuel_pressure); //commands_list.Add(ign); // Command type insert for any missing values foreach (BluetoothCmd bthCmd in commands_list.Where(bthCmd => string.IsNullOrEmpty(bthCmd.sCommand_Types))) { bthCmd.sCommand_Types = BlueToothCmds.CommandTypesToJson(new[] { BlueToothCmds.COMMAND_TYPE.DEFAULT }); } return(commands_list); }
public override void InitType(BluetoothCmd bthcmd) { bthcmd.SetUnits(bthcmd.Units_Imperial); bthcmd.SetExpression(bthcmd.Expression_Imperial); }
public abstract void InitType(BluetoothCmd bthcmd);
public override void InitType(BluetoothCmd bthcmd) { bthcmd.SetUnits(CheckString(bthcmd.Units_Metric)); bthcmd.SetExpression(CheckString(bthcmd.Expression_Metric)); }