Exemplo n.º 1
0
 public Form1()
 {
     InitializeComponent();
     textBox_code.Select(0, 0);
     defaultCSVToolStripTextBox.Text = EscPosParser.Properties.Settings.Default.database;
     ReadCsv(defaultCSVToolStripTextBox.Text);
     dataGridView_result.DataSource = ResultDatabase;
     dataGridView_commands.ReadOnly = true;
     ResultDatabase.Columns.Add("Name");
     ResultDatabase.Columns.Add("Value");
     ResultDatabase.Columns.Add("Type");
     ResultDatabase.Columns.Add("Desc");
     ResultDatabase.Columns.Add("Raw");
     ParseEscPos.Init(Accessory.ConvertHexToByteArray(textBox_code.Text), CommandDatabase);
     comboBox_printerType.SelectedIndex = 0;
     for (var i = 0; i < dataGridView_commands.Columns.Count; i++)
     {
         dataGridView_commands.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
     }
     for (var i = 0; i < dataGridView_result.Columns.Count; i++)
     {
         dataGridView_result.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
     }
 }
Exemplo n.º 2
0
        private void Button_find_Click(object sender, EventArgs e)
        {
            if (sender != listBox_commands)
            {
                listBox_commands.Items.Clear();
            }
            textBox_commandDesc.Clear();
            ResultDatabase.Clear();

            //check if cursor position in not last
            if (textBox_code.SelectionStart < textBox_code.Text.Length)
            {
                if (textBox_code.Text.Substring(textBox_code.SelectionStart, 1) == " ")
                {
                    textBox_code.SelectionStart++;
                }
            }
            //check if cursor position in not first
            if (textBox_code.SelectionStart != 0)
            {
                if (textBox_code.Text.Substring(textBox_code.SelectionStart - 1, 1) != " " &&
                    textBox_code.Text.Substring(textBox_code.SelectionStart, 1) != " ")
                {
                    textBox_code.SelectionStart--;
                }
            }
            label_currentPosition.Text = textBox_code.SelectionStart + "/" + textBox_code.TextLength;
            if (ParseEscPos.FindCommand(textBox_code.SelectionStart / 3, comboBox_printerType.SelectedItem.ToString()))
            {
                //int currentCommand = 0;  //temp const to select 1st command found
                var currentCommand = 0;
                if (sender == listBox_commands)
                {
                    currentCommand = listBox_commands.SelectedIndex;
                }
                else if (ParseEscPos.commandName.Count > 1)
                {
                    var _saved_pos = textBox_code.SelectionStart;
                    var err        = new bool[ParseEscPos.commandName.Count, 2];
                    for (var i = 0; i < ParseEscPos.commandName.Count; i++)
                    {
                        //есть ли ошибка в поиске параметров
                        err[i, 0] = ParseEscPos.FindParameter(i);
                        //если мы еще в пределах поля данных
                        if ((ParseEscPos.commandPosition[currentCommand] + ParseEscPos.commandBlockLength) * 3 <
                            textBox_code.Text.Length)
                        {
                            //ищем след. команду и, возможно, параметры
                            //есть ли ошибка в поиске след. команды
                            err[i, 1] = ParseEscPos.FindCommand(_saved_pos / 3 + ParseEscPos.commandBlockLength,
                                                                comboBox_printerType.SelectedItem.ToString());
                            //возможно, стоит поискать параметры след. команды и проверить их на ошибки тоже

                            //возвращаем поиск команды в исходное состояние для след. итерации
                            textBox_code.SelectionStart = _saved_pos;
                            ParseEscPos.FindCommand(textBox_code.SelectionStart / 3,
                                                    comboBox_printerType.SelectedItem.ToString());
                        }
                        else
                        {
                            err[i, 1] = err[i, 0];
                        }

                        //обрабатываем результаты проверок
                        //если параметры текущей и след. команда нашлись, то выбираем текущую
                        if (err[i, 1] == err[i, 0] && err[i, 0])
                        {
                            currentCommand = i;
                        }
                    }
                }

                ParseEscPos.FindParameter(currentCommand);
                if (sender != button_auto) //only update interface if it's no auto-parsing mode
                {
                    dataGridView_commands.CurrentCell = dataGridView_commands
                                                        .Rows[ParseEscPos.commandDbLineNum[currentCommand]].Cells[ParseEscPos.CSVColumns.CommandName];
                    if (sender != listBox_commands)
                    {
                        for (var i = 0; i < ParseEscPos.commandName.Count; i++)
                        {
                            listBox_commands.Items.Add(ParseEscPos.commandName[i] + "[" +
                                                       ParseEscPos.commandPrinterModel[i] + "]");
                        }
                    }
                    listBox_commands.SelectedIndexChanged -= ListBox_commands_SelectedIndexChanged;
                    listBox_commands.SelectedIndex         = currentCommand;
                    listBox_commands.SelectedIndexChanged += ListBox_commands_SelectedIndexChanged;
                    textBox_commandDesc.Text = ParseEscPos.commandDesc[currentCommand];
                    for (var i = 0; i < ParseEscPos.paramName.Count; i++)
                    {
                        var row = ResultDatabase.NewRow();
                        row[ResultColumns.Name]  = ParseEscPos.paramName[i];
                        row[ResultColumns.Value] = ParseEscPos.paramValue[i];
                        row[ResultColumns.Type]  = ParseEscPos.paramType[i];
                        row[ResultColumns.Raw]   =
                            Accessory.ConvertByteArrayToHex(ParseEscPos.paramRAWValue[i].ToArray());
                        //if (ParseEscPos.paramType[i].ToLower() != ParseEscPos.DataTypes.Bitfield) row[ResultColumns.Desc] = ParseEscPos.paramDesc[i];
                        row[ResultColumns.Desc] = ParseEscPos.paramDesc[i];
                        ResultDatabase.Rows.Add(row);

                        if (ParseEscPos.paramType[i].ToLower() == ParseEscPos.DataTypes.Bitfield) //add bitfield display
                        {
                            for (var i1 = 0; i1 < 8; i1++)
                            {
                                row = ResultDatabase.NewRow();
                                //row[ResultColumns.Name] = ParseEscPos.paramName[i] + "[" + i1.ToString() + "]";
                                row[ResultColumns.Value] = ParseEscPos.bitValue[i][i1];
                                row[ResultColumns.Type]  = ParseEscPos.bitName[i][i1];
                                row[ResultColumns.Desc]  = ParseEscPos.bitDescription[i][i1];
                                ResultDatabase.Rows.Add(row);
                            }
                        }
                    }
                }

                //textBox_code.Select(textBox_code.SelectionStart, ParseEscPos.commandBlockLength * 3);
                textBox_code.Select(ParseEscPos.commandPosition[currentCommand] * 3,
                                    ParseEscPos.commandBlockLength * 3);
            }
            //look for the end of unrecognizable data (consider it text string)
            else
            {
                var i = 3;
                while (!ParseEscPos.FindCommand((textBox_code.SelectionStart + i) / 3,
                                                comboBox_printerType.SelectedItem.ToString()) &&
                       textBox_code.SelectionStart + i < textBox_code.TextLength) //looking for a non-parseable part end
                {
                    i += 3;
                }
                if (textBox_code.SelectionStart + i >= textBox_code.TextLength)
                {
                    i = textBox_code.TextLength - textBox_code.SelectionStart;
                }
                textBox_code.Select(textBox_code.SelectionStart, i);
                ParseEscPos.commandName.Clear();
                if (textBox_code.SelectedText.Length > 0)
                {
                    if (sender != button_auto)
                    {
                        listBox_commands.Items.Add("\"" + 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();
        }