public static double RawToQuantity(byte[] b) { double l = 0; for (var n = 0; n < b.Length; n++) { if (n == b.Length - 1 && Accessory.GetBit(b[n], 7)) { l += b[n] * Math.Pow(256, n); l = l - Math.Pow(2, b.Length * 8); } else { l += b[n] * Math.Pow(256, n); } } return(l / 1000); }
private void Button_find_Click(object sender, EventArgs e) { textBox_command.Text = ""; textBox_commandDesc.Clear(); ResultDatabase.Clear(); if (textBox_code.SelectionStart != textBox_code.Text.Length) //check if cursor position in not last { if (textBox_code.Text.Substring(textBox_code.SelectionStart, 1) == " ") { textBox_code.SelectionStart++; } } if (textBox_code.SelectionStart != 0) //check if cursor position in not first { if (textBox_code.Text.Substring(textBox_code.SelectionStart - 1, 1) != " " && textBox_code.Text.Substring(textBox_code.SelectionStart, 1) != " ") { textBox_code.SelectionStart--; } } /*if (sender != button_find) * { * textBox_code.SelectionStart = textBox_code.SelectionStart + textBox_code.SelectionLength; * }*/ label_currentPosition.Text = textBox_code.SelectionStart + "/" + textBox_code.TextLength; if (ParseEscPos.FindCommand(textBox_code.SelectionStart / 3)) { ParseEscPos.FindCommandParameter(); if (sender != button_auto) //update interface only if it's no auto-parsing mode { dataGridView_commands.CurrentCell = dataGridView_commands.Rows[ParseEscPos.commandDbLineNum] .Cells[ParseEscPos.CSVColumns.CommandName]; if (ParseEscPos.itIsReply) { textBox_command.Text = "[REPLY] " + ParseEscPos.commandName; } else { textBox_command.Text = "[COMMAND] " + ParseEscPos.commandName; } if (ParseEscPos.crcFailed) { textBox_commandDesc.Text += "!!!CRC FAILED!!! "; } if (ParseEscPos.lengthIncorrect) { textBox_commandDesc.Text += "!!!FRAME LENGTH INCORRECT!!! "; } textBox_commandDesc.Text = ParseEscPos.commandDesc; for (var i = 0; i < ParseEscPos.commandParamSize.Count; i++) { var row = ResultDatabase.NewRow(); row[ResultColumns.Value] = ParseEscPos.commandParamValue[i]; row[ResultColumns.Type] = ParseEscPos.commandParamType[i]; row[ResultColumns.Raw] = Accessory.ConvertByteArrayToHex(ParseEscPos.commandParamRAWValue[i].ToArray()); row[ResultColumns.Description] = ParseEscPos.commandParamDesc[i]; if (ParseEscPos.commandParamType[i].ToLower() == ParseEscPos.DataTypes.Error) { row[ResultColumns.Description] += ": " + GetErrorDesc(int.Parse(ParseEscPos.commandParamValue[i])); } ResultDatabase.Rows.Add(row); if (ParseEscPos.commandParamType[i].ToLower() == ParseEscPos.DataTypes.Bitfield ) //add bitfield display { var b = byte.Parse(ParseEscPos.commandParamValue[i]); for (var i1 = 0; i1 < 8; i1++) { row = ResultDatabase.NewRow(); row[ResultColumns.Value] = (Accessory.GetBit(b, (byte)i1) ? (byte)1 : (byte)0).ToString(); row[ResultColumns.Type] = "bit" + i1; row[ResultColumns.Description] = dataGridView_commands .Rows[ParseEscPos.commandParamDbLineNum[i] + i1 + 1] .Cells[ParseEscPos.CSVColumns.CommandDescription].Value; ResultDatabase.Rows.Add(row); } } } } if (ParseEscPos.itIsReply && textBox_code.Text.Substring(textBox_code.SelectionStart + (ParseEscPos.commandBlockLength + 1) * 3, 3) == Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { textBox_code.Select(textBox_code.SelectionStart, (ParseEscPos.commandBlockLength + 2) * 3); } else { textBox_code.Select(textBox_code.SelectionStart, (ParseEscPos.commandBlockLength + 1) * 3); } } else //no command found. consider it's a string { var i = 3; while (!ParseEscPos.FindCommand((textBox_code.SelectionStart + i) / 3) && textBox_code.SelectionStart + i < textBox_code.TextLength) //looking for a non-parseable part end { i += 3; } ParseEscPos.commandName = ""; textBox_code.Select(textBox_code.SelectionStart, i); if (sender != button_auto) { //textBox_command.Text += ""; //textBox_commandDesc.Text = "\"" + (String)textBox_code.SelectedText + "\""; if (textBox_code.SelectedText == Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { textBox_command.Text = "ACK"; } else if (textBox_code.SelectedText == Accessory.ConvertByteToHex(ParseEscPos.nakSign)) { textBox_command.Text = "NAK"; } else if (textBox_code.SelectedText == Accessory.ConvertByteToHex(ParseEscPos.enqSign) + Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { textBox_command.Text = "BUSY"; } else { textBox_command.Text = "\"" + textBox_code.SelectedText + "\""; } dataGridView_commands.CurrentCell = dataGridView_commands.Rows[0].Cells[0]; if (Accessory.PrintableHex(textBox_code.SelectedText)) { textBox_commandDesc.Text = "\"" + Encoding.GetEncoding(Settings.Default.CodePage) .GetString(Accessory.ConvertHexToByteArray(textBox_code.SelectedText)) + "\""; } } } textBox_code.ScrollToCaret(); }
private void Button_auto_Click(object sender, EventArgs e) { File.WriteAllText(SourceFile + ".escpos", ""); File.WriteAllText(SourceFile + ".list", ""); textBox_code.Select(0, 0); var asciiString = new StringBuilder(); while (textBox_code.SelectionStart < textBox_code.TextLength) { var saveStr = new StringBuilder(); //run "Find" button event as "Auto" Button_find_Click(button_auto, EventArgs.Empty); if (ParseEscPos.commandName != "") { //ParseEscPos.FindCommandParameter(); //????????????? //Save ASCII string if collected till now if (asciiString.Length != 0) { saveStr.Append("#" + ParseEscPos.commandFramePosition + " RAW data [" + asciiString + "]\r\n"); if (asciiString.ToString() == Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { saveStr.Append("ACK\r\n"); } else if (asciiString.ToString() == Accessory.ConvertByteToHex(ParseEscPos.nakSign)) { saveStr.Append("NAK\r\n"); } else if (asciiString.ToString() == Accessory.ConvertByteToHex(ParseEscPos.enqSign) + Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { saveStr.Append("BUSY\r\n"); } else if (Accessory.PrintableHex(asciiString.ToString())) { saveStr.Append("ASCII string: \"" + Encoding.GetEncoding(Settings.Default.CodePage) .GetString(Accessory.ConvertHexToByteArray(asciiString.ToString())) + "\"\r\n"); } saveStr.Append("\r\n"); File.AppendAllText(SourceFile + ".list", asciiString + "\r\n", Encoding.GetEncoding(Settings.Default.CodePage)); asciiString.Clear(); } //collect command into file /* RAW [12 34] * Command: "12 34" - "Description" * Printer model: "VKP80II-SX" * Parameter: "n" = "1234"[Word] - "Description" * Parameter: ... */ saveStr.Append("#" + ParseEscPos.commandFramePosition + " RAW data [" + textBox_code.SelectedText + "]\r\n"); if (ParseEscPos.itIsReply) { saveStr.Append("Reply: [" + ParseEscPos.commandName + "] - \"" + ParseEscPos.commandDesc + "\"\r\n"); } else { saveStr.Append("Command: [" + ParseEscPos.commandName + "] - \"" + ParseEscPos.commandDesc + "\"\r\n"); } for (var i = 0; i < ParseEscPos.commandParamSize.Count; i++) { saveStr.Append("\tParameter = "); saveStr.Append("\"" + ParseEscPos.commandParamValue[i] + "\""); saveStr.Append("[" + ParseEscPos.commandParamType[i] + "] - \"" + ParseEscPos .commandParamDesc[i].TrimStart('\r').TrimStart('\n').TrimEnd('\n').TrimEnd('\r') .Replace("\n", "\n\t\t\t\t")); if (ParseEscPos.commandParamType[i].ToLower() == ParseEscPos.DataTypes.Error) { saveStr.Append(": " + GetErrorDesc(int.Parse(ParseEscPos.commandParamValue[i]))); } saveStr.Append("\", RAW [" + Accessory.ConvertByteArrayToHex(ParseEscPos.commandParamRAWValue[i].ToArray()) + "]\r\n"); if (ParseEscPos.commandParamType[i].ToLower() == ParseEscPos.DataTypes.Bitfield) { var b = byte.Parse(ParseEscPos.commandParamValue[i]); for (var i1 = 0; i1 < 8; i1++) { saveStr.Append("\t\t[bit" + i1 + "]\" = \""); saveStr.Append((Accessory.GetBit(b, (byte)i1) ? (byte)1 : (byte)0) + "\" - \""); saveStr.Append(dataGridView_commands.Rows[ParseEscPos.commandParamDbLineNum[i] + i1 + 1] .Cells[ParseEscPos.CSVColumns.CommandDescription].Value.ToString() .Replace("\n", "\n\t\t\t\t")); saveStr.Append("\"\r\n"); } } } saveStr.Append("\r\n"); File.AppendAllText(SourceFile + ".list", textBox_code.SelectedText + "\r\n", Encoding.GetEncoding(Settings.Default.CodePage)); File.AppendAllText(SourceFile + ".escpos", saveStr.ToString(), Encoding.GetEncoding(Settings.Default.CodePage)); } else //consider this as a string and collect { asciiString.Append(textBox_code.SelectedText); } textBox_code.SelectionStart = textBox_code.SelectionStart + textBox_code.SelectionLength; } if (asciiString.Length != 0) { var saveStr = new StringBuilder(); saveStr.Append("#" + ParseEscPos.commandFramePosition + " RAW data [" + asciiString + "]\r\n"); if (asciiString.ToString() == Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { saveStr.Append("ACK"); } if (asciiString.ToString() == Accessory.ConvertByteToHex(ParseEscPos.nakSign)) { saveStr.Append("NAK"); } if (asciiString.ToString() == Accessory.ConvertByteToHex(ParseEscPos.enqSign) + Accessory.ConvertByteToHex(ParseEscPos.ackSign)) { saveStr.Append("BUSY"); } else if (Accessory.PrintableHex(asciiString.ToString())) { saveStr.Append("ASCII string: \"" + Encoding.GetEncoding(Settings.Default.CodePage) .GetString(Accessory.ConvertHexToByteArray(asciiString.ToString())) + "\"\r\n"); } saveStr.Append("\r\n"); File.AppendAllText(SourceFile + ".list", asciiString + "\r\n", Encoding.GetEncoding(Settings.Default.CodePage)); File.AppendAllText(SourceFile + ".escpos", saveStr.ToString(), Encoding.GetEncoding(Settings.Default.CodePage)); asciiString.Clear(); } }
public static bool FindParameter(int _commandNum) { ClearParameters(); if (_commandNum < 0 || _commandNum >= commandName.Count) { return(false); } //collect parameters var _stopSearch = commandDbLineNum[_commandNum] + 1; while (_stopSearch < _commandDataBase.Rows.Count && _commandDataBase.Rows[_stopSearch][CSVColumns.CommandName].ToString() == "") { _stopSearch++; } for (var i = commandDbLineNum[_commandNum] + 1; i < _stopSearch; i++) { if (_commandDataBase.Rows[i][CSVColumns.ParameterName].ToString() != "") { paramDbLineNum.Add(i); paramName.Add(_commandDataBase.Rows[i][CSVColumns.ParameterName].ToString()); paramDesc.Add(_commandDataBase.Rows[i][CSVColumns.Description].ToString()); paramType.Add(_commandDataBase.Rows[i][CSVColumns.ParameterType].ToString()); } } errors = false; //Error in parameter found //process each parameter for (var parameter = 0; parameter < paramDbLineNum.Count; parameter++) { paramPosition.Add(commandPosition[_commandNum] + ResultLength(_commandNum)); bitName.Add(new List <string>()); bitValue.Add(new List <string>()); bitDescription.Add(new List <string>()); //collect predefined RAW values var predefinedParamsRaw = new List <string>(); var j = paramDbLineNum[parameter] + 1; while (j < _commandDataBase.Rows.Count && _commandDataBase.Rows[j][CSVColumns.ParameterValue].ToString() != "") { predefinedParamsRaw.Add(_commandDataBase.Rows[j][CSVColumns.ParameterValue].ToString()); j++; } //Calculate predefined params var predefinedParamsVal = new List <int>(); foreach (var expresion in predefinedParamsRaw) { var val = 0; //select formula basing on parameter value "?k=1:n+n1 ?k-2:n*n1" if (expresion.StartsWith("?")) { var tmpstr = expresion.Trim().Replace("\r", "").Replace("\n", "").Split('?'); foreach (var str in tmpstr) { if (str != "") { var equation = str.Substring(0, str.IndexOf(':')); //calculate equation if needed for (var i2 = 0; i2 < paramName.Count - 1; i2++) //insert all parameters before current into equation { equation = equation.Replace(paramName[i2], paramValue[i2]); equation = equation.Replace('=', '-'); } if (Accessory.Evaluate(equation) == 0) { var equation2 = str.Substring(str.IndexOf(':') + 1).Trim(); for (var i3 = 0; i3 < paramName.Count - 1; i3++) { equation2 = equation2.Replace(paramName[i3], paramValue[i3]); } try { val = (int)Accessory.Evaluate(equation2); } catch { } } } } } else if (expresion.StartsWith("@")) { var equation = expresion.Substring(1); //insert all parameters before current into equation for (var i2 = 0; i2 < paramName.Count - 1; i2++) { equation = equation.Replace(paramName[i2], paramValue[i2]); } val = (int)Accessory.Evaluate(equation); // = str.Substring(str.IndexOf(':') + 1); } else { if (!int.TryParse(expresion.Trim(), out val)) { val = 0; } } predefinedParamsVal.Add(val); } //get parameter from text var tmpStrLength = 0; var predefinedFound = false; //Matching predefined parameter found and it's number is in "predefinedParameterMatched" var errMessage = ""; byte l = 0, h = 0; var predefinedParameterMatched = 0; var _prmType = _commandDataBase.Rows[paramDbLineNum[parameter]][CSVColumns.ParameterType].ToString() .ToLower(); if (_prmType == DataTypes.Byte) { if (paramPosition[parameter] + 1 <= sourceData.Count) { paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], 1)); l = paramRAWValue[parameter].ToArray()[0]; } else { errors = true; errMessage = "!!!ERR: Out of data!!!"; paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], sourceData.Count - paramPosition[parameter])); } paramValue.Add(l.ToString()); } else if (_prmType == DataTypes.Bitfield) { if (paramPosition[parameter] + 1 <= sourceData.Count) { paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], 1)); l = paramRAWValue[parameter].ToArray()[0]; for (byte i2 = 0; i2 < 8; i2++) { bitName[parameter].Add("bit" + i2); bitValue[parameter].Add(Accessory.GetBit(l, i2).ToString()); bitDescription[parameter] .Add(_commandDataBase.Rows[paramDbLineNum[parameter] + i2 + 1][CSVColumns.Description] .ToString()); } } else { errors = true; errMessage = "!!!ERR: Out of data!!!"; paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], sourceData.Count - paramPosition[parameter])); } paramValue.Add(l.ToString()); } else if (_prmType == DataTypes.Word) { if (paramPosition[parameter] + 2 <= sourceData.Count) { paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], 2)); l = paramRAWValue[parameter].GetRange(0, 1)[0]; h = paramRAWValue[parameter].GetRange(1, 1)[0]; } else { errors = true; errMessage = "!!!ERR: Out of data!!!"; paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], sourceData.Count - paramPosition[parameter])); } paramValue.Add((h * 256 + l).ToString()); } else if (_prmType == DataTypes.Rword) { if (paramPosition[parameter] + 2 <= sourceData.Count) { paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], 2)); h = paramRAWValue[parameter].GetRange(0, 1)[0]; l = paramRAWValue[parameter].GetRange(1, 1)[0]; } else { errors = true; errMessage = "!!!ERR: Out of data!!!"; paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], sourceData.Count - paramPosition[parameter])); } paramValue.Add((h * 256 + l).ToString()); } else if (_prmType == DataTypes.Textstring || _prmType == DataTypes.Decstring || _prmType == DataTypes.Hexstring || _prmType == DataTypes.Binarystring) { if (predefinedParamsVal.Count > 0) { //look for the end of the string (predefined byte) while (predefinedFound == false && paramPosition[parameter] + tmpStrLength <= sourceData.Count) { //check for each predefined value for (var i1 = 0; i1 < predefinedParamsVal.Count; i1++) { if (paramPosition[parameter] + tmpStrLength + 1 <= sourceData.Count) { if (sourceData[paramPosition[parameter] + tmpStrLength] == predefinedParamsVal[i1]) { predefinedFound = true; predefinedParameterMatched = i1; paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], tmpStrLength)); } } else { errors = true; errMessage = "!!!ERR: Out of data!!!"; } } tmpStrLength++; } if (tmpStrLength < 1) { errors = true; errMessage = "!!!ERR: Out of data!!!"; } if (errors) { paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], sourceData.Count - paramPosition[parameter])); } if (paramRAWValue[parameter].Count != 0) { if ((_prmType == DataTypes.Textstring || _prmType == DataTypes.Decstring || _prmType == DataTypes.Hexstring) && Accessory.PrintableByteArray(paramRAWValue[parameter].ToArray())) { paramValue.Add(Encoding.GetEncoding(Settings.Default.CodePage) .GetString(paramRAWValue[parameter].ToArray())); } else { paramValue.Add("[" + Accessory.ConvertByteArrayToHex(paramRAWValue[parameter].ToArray()) + "]"); } } else { paramValue.Add(""); } } else { errors = true; errMessage = "!!!ERR: There must be predefined values!!!"; paramRAWValue.Add(null); paramValue.Add(""); } } else if (_prmType == DataTypes.Textarray || _prmType == DataTypes.Decarray || _prmType == DataTypes.Hexarray || _prmType == DataTypes.Binaryarray) { if (predefinedParamsVal.Count == 1) { predefinedFound = true; predefinedParameterMatched = 0; if (paramPosition[parameter] + predefinedParamsVal[0] <= sourceData.Count) { paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], (byte)predefinedParamsVal[0])); } else { errors = true; errMessage = "!!!ERR: Out of data bound!!!"; paramRAWValue.Add(sourceData.GetRange(paramPosition[parameter], sourceData.Count - paramPosition[parameter])); } if (paramRAWValue[parameter].Count != 0) { if ((_prmType == DataTypes.Textarray || _prmType == DataTypes.Decarray || _prmType == DataTypes.Hexarray) && Accessory.PrintableByteArray(paramRAWValue[parameter].ToArray())) { paramValue.Add(Encoding.GetEncoding(Settings.Default.CodePage) .GetString(paramRAWValue[parameter].ToArray())); } else { paramValue.Add("[" + Accessory.ConvertByteArrayToHex(paramRAWValue[parameter].ToArray()) + "]"); } } else { paramValue.Add(""); } } else { errors = true; errMessage = "!!!ERR: There must be only one predefined value!!!"; paramRAWValue.Add(null); paramValue.Add(""); } } else { predefinedFound = true; errors = true; errMessage = "!!!ERR: Incorrect parameter type!!!"; paramRAWValue.Add(new List <byte>()); paramValue.Add(""); } if (errors) { paramDesc[parameter] += errMessage + "\r\n"; } //compare parameter value with predefined values to get proper description if (predefinedFound == false && !errors) { for (var i1 = 0; i1 < predefinedParamsVal.Count; i1++) { if (paramValue[parameter] == predefinedParamsVal[i1].ToString()) { predefinedFound = true; predefinedParameterMatched = i1; i1 = predefinedParamsVal.Count; } } if (predefinedParamsVal.Count > 0 && predefinedFound == false) { errors = true; //if no parameters match predefined } } //get description for predefined parameter //if description is within current command parameters group if (paramDbLineNum[parameter] + predefinedParameterMatched + 1 <= commandDbLineNum[_commandNum] + commandDbHeight[_commandNum] && predefinedFound) { paramDesc[parameter] += _commandDataBase.Rows[paramDbLineNum[parameter] + predefinedParameterMatched + 1][ CSVColumns.Description].ToString(); } } ResultLength(_commandNum); return(!errors); }