Subversion Repositories group.electronics

Rev

Rev 139 | Rev 155 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NITNavComm
{
    public partial class MainForm : Form
    {

        private NITPanels panels = new NITPanels();

        public MainForm() {
            InitializeComponent();

            Devices_Rescan();

        }

        private void Devices_Rescan() {
            Log("Scanning for devices...");

            panels.UsbScan();

            this.deviceGrid.DataSource = panels.devices;
            this.txtDevices.Text = panels.devices.Count.ToString();

            if (panels.devices.Count > 0) {
                this.cmDevTest.Enabled = true;
                Log(panels.devices.Count.ToString() + " devices found during scan.");
            } else {
                Log("No devices found, check connections and rescan.");
            }
        }

        private void cmdRescan_Click(object sender, EventArgs e) {
            this.Devices_Rescan();
        }

        private void cmdDevRescan_Click(object sender, EventArgs e) {
            this.Devices_Rescan();
        }

        private void cmDevTest_Click(object sender, EventArgs e) {
            NITDevice device = (NITDevice) this.deviceGrid.CurrentRow.DataBoundItem;
            NITNavCommDevice navcomm = new NITNavCommDevice(device);

            if (!navcomm.Open()) {
                Log("Could not open device " + device.type + "(" + device.serial + ").");
                return;
            }
            navcomm.Close();

            NITCommNavForm form = new NITCommNavForm();
            form.setDevice(navcomm);
            form.Show(this);

        }

        public void Log(string msg) {
            txtLog.AppendText(msg + "\r\n");
        }

        public void Log(byte[] buffer) {
            StringBuilder sb = new StringBuilder();
            foreach (byte data in buffer) {
                sb.Append(data.ToString("X") + " ");
            }
            Log(sb.ToString());
        } 

        private void quitToolStripMenuItem_Click(object sender, EventArgs e) {
            if (QuestionBox("Exit NIT Panels?", "Exiting will disable NIT Panels. Continue?")) {
                Application.Exit();
            }
        }

        public bool QuestionBox(string caption, string message) {
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            result = MessageBox.Show(message, caption, buttons);

            if (result == System.Windows.Forms.DialogResult.Yes) {
                return true;
            }
            return false;
        }

        private void txtLog_VisibleChanged(object sender, EventArgs e) {
            if (txtLog.Visible) {
                txtLog.SelectionStart = txtLog.TextLength;
                txtLog.ScrollToCaret();
            }
        }

    }
}