Ejemplo n.º 1
0
        void SerialPortSend(string text, RC5code rc5code)
        {
            try
            {
                if (serialPort == null && !serialPort.IsOpen)
                {
                    return;
                }
                text = text.ToLower();

                var bytesText   = Encoding.ASCII.GetBytes(text);
                var bytesResult = new byte[bytesText.Length + rc5code.Bytes.Length + 1];

                for (int i = 0; i < bytesText.Length; ++i)
                {
                    bytesResult[i] = bytesText[i];
                }

                for (int i = bytesText.Length; i < bytesResult.Length - 1; ++i)
                {
                    bytesResult[i] = rc5code.Bytes[i - bytesText.Length];
                }

                bytesResult[bytesResult.Length - 1] = Convert.ToByte('\n');

                serialPort.Write(bytesResult, 0, bytesResult.Length);
                richTextBoxConsole.AppendLine(text + rc5code, Color.Orange);
            }
            catch (Exception ex) { MessageBox.Show("Error " + GetType().FullName + ": " + MethodBase.GetCurrentMethod().Name + "()\n" + ex.ToString(), "Error " + Name); }
        }
Ejemplo n.º 2
0
        void Dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            try
            {
                var cells = dgv.Rows[dgv.Rows.Count - 2].Cells;
                var value = cells[ColumnIRCode.Index].Value;

                RC5code code;
                if (value != null)
                {
                    code = (RC5code)value;
                }
                else
                {
                    code = new RC5code(new byte[] { 0 });
                }

                var cmd = new IrCommand(false, "Name " + (dgv.RowCount - 2), code, TypeCommands.SendKeys, "", 1000);
                listCommands.Add(cmd);

                lockCellChanged = true;
                cells[ColumnEnable.Index].Value      = cmd.Enable;
                cells[ColumnName.Index].Value        = cmd.Name;
                cells[ColumnIRCode.Index].Value      = cmd.IrCode;
                cells[ColumnTypeCommand.Index].Value = cmd.TypeCommand;
                cells[ColumnCommand.Index].Value     = cmd.Command;
                cells[ColumnTimeReplay.Index].Value  = cmd.Timeout;
                cells[ColumnId.Index].Value          = cmd.Id;
                lockCellChanged = false;
            }
            catch (Exception ex) { MessageBox.Show("Error " + GetType().FullName + ": " + MethodBase.GetCurrentMethod().Name + "()\n" + ex.ToString()); }
        }
Ejemplo n.º 3
0
        public IrCommand(bool enable, string name, RC5code code, TypeCommands typeCommand, string command, int timeout)
        {
            Enable      = enable;
            _name       = name;
            _irCode     = code;
            Timeout     = timeout;
            TypeCommand = typeCommand;
            Command     = command;

            SetId();
        }
Ejemplo n.º 4
0
        public static IrCommand Parse(string[] cmdStroke)
        {
            var cmd = new IrCommand(
                Convert.ToBoolean(cmdStroke[0]),
                cmdStroke[1],
                RC5code.FromStringHex(cmdStroke[2]),
                (TypeCommands)Enum.Parse(typeof(TypeCommands), cmdStroke[3], true),
                cmdStroke[4],
                Convert.ToInt32(cmdStroke[5])
                );

            return(cmd);
        }
Ejemplo n.º 5
0
        void CodeReaded(object state)
        {
            GotActivity();
            richTextBoxConsole.AppendLine(state);
            richTextBoxConsole.ScrollToCaret();
            labelLastCode.Text = state.ToStringNull();

            SetIconByStateIr(StateIr.Receive);

            if (!(state is RC5code))
            {
                return;
            }
            var code = (RC5code)state;

            lastCode = code;

            if (dgv.CurrentCell.ColumnIndex == ColumnIRCode.Index)
            {
                dgv.CurrentCell.Value = code;
            }
            else
            {
                dgv[ColumnIRCode.Index, dgv.LastRow.Index].Value = code;
            }

            for (int i = 0; i < dgv.Rows.Count; ++i)
            {
                var cell = dgv[ColumnIRCode.Index, i];

                if (cell.Value != null && (RC5code)cell.Value == code)
                {
                    cell.Style.BackColor = Color.LightBlue;
                    cell.Style.ForeColor = Color.White;
                    if (lastCell != null && lastCell != cell)
                    {
                        lastCell.Style.BackColor = ColumnIRCode.DefaultCellStyle.BackColor;
                        lastCell.Style.ForeColor = ColumnIRCode.DefaultCellStyle.ForeColor;
                    }
                    lastCell = cell;
                    break;
                }
            }
        }
Ejemplo n.º 6
0
        void SerialPortWorker()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(NamePort))
                {
                    asyncOperation.Post(() => SetIconByStateIr(StateIr.Error));
                    return;
                }
                using (serialPort = new SafeSerialPort(NamePort, BaudRate, DataBits, Parity, StopBits))
                {
                    serialPort.NewLine = "\r\n";
                    var portNames = SerialPort.GetPortNames();
                    if (SerialConfigValidated && portNames.Contains(serialPort.PortName, StringComparer.OrdinalIgnoreCase))
                    {
                        serialPort.Open();
                        asyncOperation.Post(() =>
                        {
                            lblState.Text = "State: " + serialPort.PortName + " opened";
                            SetIconByStateIr(StateIr.Normal);
                        });
                    }
                    else
                    {
                        asyncOperation.Post(() => SetIconByStateIr(StateIr.Error));
                    }

                    while (true)
                    {
                        System.Threading.Thread.Sleep(1);
                        if (!serialPort.IsOpen)
                        {
                            break;
                        }

                        if (serialPort.BytesToRead > 0)
                        {
                            var readStr = serialPort.TryReadLine();
                            if (readStr == null)
                            {
                                continue;
                            }
                            if (readStr[0] != '>' || readStr.Length < 2 || readStr[readStr.Length - 2] != 0x0D || readStr.Last() != 0x0A)
                            {
                                continue;
                            }
                            object readPost;
                            if (readStr[1] == '>')
                            {
                                readPost = readStr.Substring(2, readStr.Length - 3);
                            }
                            else
                            {
                                var resultRead = readStr.Substring(1, readStr.Length - 3);
                                var code       = new RC5code(resultRead);
                                if (WindowState == FormWindowState.Minimized || !chb_DisableCmd.Checked)
                                {
                                    foreach (var cmdIR in listCommands)
                                    {
                                        if (cmdIR.Enable && cmdIR.IrCode == code)
                                        {
                                            cmdIR.RunCommand();
                                        }
                                    }
                                }
                                readPost = code;
                            }
                            asyncOperation.Post(CodeReaded, readPost);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                asyncOperation.Post(() => SetIconByStateIr(StateIr.Error));
                MessageBox.Show("Error " + GetType().FullName + ": " + MethodBase.GetCurrentMethod().Name + "()\n" + ex.ToString());
            }
            try
            {
                asyncOperation.Post(() => { lblState.Text = "State: ..."; });
            }
            catch (Exception ex) { MessageBox.Show("Error " + GetType().FullName + ": " + MethodBase.GetCurrentMethod().Name + "()\n" + ex.ToString(), "Error " + Name); }
        }