public static string NumberToRaw(string s, int n) { double d = 0; if (s != "") { double.TryParse(s, out d); } if (d < 0) { d = Math.Pow(2, n * 8) - Math.Abs(d); } var b = new byte[n]; for (var i = n - 1; i >= 0; i--) { b[i] += (byte)(d / Math.Pow(256, i)); d -= b[i] * Math.Pow(256, i); } var str = ""; for (var i = 0; i < n; i++) { str += Accessory.ConvertByteToHex(b[i]); } return(str); }
public static string BitfieldToRaw(string s) { byte l = 0; if (s != "") { byte.TryParse(s, out l); } var str = ""; str += Accessory.ConvertByteToHex(l); return(str); }
public static string ErrorToRaw(string s, int n) { long l = 0; if (s != "") { long.TryParse(s, out l); } var str = ""; for (var i = 0; i < n; i++) { str += Accessory.ConvertByteToHex((byte)(l / Math.Pow(256, i))); } return(str); }
public static string PasswordToRaw(string s, int n) { s = s.Replace(" ", "").Replace("/", ""); long l = 0; if (s != "") { long.TryParse(s, out l); } var str = ""; for (var i = 0; i < n; i++) { str += Accessory.ConvertByteToHex((byte)(l / Math.Pow(256, i))); } return(str); }
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(); } }
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(); }