コード例 #1
0
ファイル: SerialPortWindow.cs プロジェクト: dozack/rps
        /// <summary>
        /// Handler for application layer notifications
        /// </summary>
        /// <param name="sender"><c>ApplicationLayer</c> instance</param>
        /// <param name="pdu">Application protocol data unit</param>
        private void ProtocolHandler_OnApplicationUpdate(ApplicationLayer sender, ApplicationPdu pdu)
        {
            switch (pdu.Address)
            {
            case 'A':
                // Update trackbar value
                if (pdu.Value <= trckAnalog.Maximum)
                {
                    Invoke(new Action(() =>
                    {
                        trckAnalog.Value = pdu.Value;
                    }));
                }
                break;

            case 'D':
                // Update button color
                bool value = Convert.ToBoolean(pdu.Value);
                if (value)
                {
                    Invoke(new Action(() =>
                    {
                        bttnDigitial.BackColor = Color.Green;
                    }));
                }
                else
                {
                    Invoke(new Action(() =>
                    {
                        bttnDigitial.BackColor = Color.Gray;
                    }));
                }
                break;

            default:
                break;
            }
        }
コード例 #2
0
ファイル: SerialPortWindow.cs プロジェクト: dozack/rps
        /// <summary>
        /// Handler for Click event of bttnConnection
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bttnConnection_Click(object sender, EventArgs e)
        {
            if (ProtocolHandler == null || !ProtocolHandler.Connected)
            {
                // ProtocolHandler not initalized or disconnected
                Configuration config = new Configuration()
                {
                    PortName = cmbPorts.SelectedItem.ToString(),
                    BaudRate = baudrates[cmbBaud.SelectedItem.ToString()],
                    IsServer = chkMaster.Checked,
                };
                // Init new instance of application layer
                ProtocolHandler = new ApplicationLayer(config);

                // Try to connect to bus
                if (!ProtocolHandler.Connect())
                {
                    MessageBox.Show("Error during connection, try again.");
                    return;
                }

                // If app is client, register handler for notifications
                if (!ProtocolHandler.IsServer)
                {
                    ProtocolHandler.OnApplicationUpdate += ProtocolHandler_OnApplicationUpdate;
                }

                // Register trackbar address
                ProtocolHandler.RegisterAddress('A');
                // Register button address
                ProtocolHandler.RegisterAddress('D');

                if (ProtocolHandler.IsServer)
                {
                    // Start service if instance is server
                    ProtocolHandler.ServerStartService();
                }

                // Update controls
                bttnConnection.Text = "Disconnect";
                grpData.Enabled     = true;
                cmbPorts.Enabled    = false;
                cmbBaud.Enabled     = false;
                chkMaster.Enabled   = false;
            }
            else
            {
                if (ProtocolHandler.IsServer)
                {
                    // Start service if instance is server
                    ProtocolHandler.ServerStopService();
                }
                // Disconnect from bus
                ProtocolHandler.Disconnect();

                // Update controls
                bttnConnection.Text = "Connect";
                grpData.Enabled     = false;
                cmbPorts.Enabled    = true;
                cmbBaud.Enabled     = true;
                chkMaster.Enabled   = true;
            }
        }