예제 #1
0
        public TrinketFakeUsbSerialHostSW()
        {
            notifyIcon = new NotifyIcon();
            notificationMenu = new ContextMenu(InitializeMenu());

            // notifyIcon.DoubleClick += IconDoubleClick;
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(TrinketFakeUsbSerialHostSW));
            notifyIcon.Icon = (Icon)resources.GetObject("$this.Icon");
            notifyIcon.ContextMenu = notificationMenu;

            trinket = new VUsbDevice(0x1781, 0x1111); // VID and PID set inside usbconfig.h, which is the Trinket's VID and PID while using the TrinketFakeUsbSerial library
            if (trinket.TryConnect())
            {
                try
                {
                    if (trinket.MyUsbDevice.IsOpen == false)
                    {
                        if (trinket.MyUsbDevice.Open() == false)
                        {
                            throw new Exception("Can't Open USB Device");
                        }
                    }

                    trinketReader = trinket.MyUsbDevice.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep01);
                    trinketReader.DataReceived += new EventHandler<LibUsbDotNet.Main.EndpointDataEventArgs>(trinket_DataReceived);
                    trinketReader.DataReceivedEnabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Trinket Connecting Error: " + ex.Message);
                }
            }
            //trinket.OnOtherDeviceNotifyEvent += new EventHandler<DeviceNotifyEventArgs>(trinket_OnOtherDeviceNotifyEvent);
            trinket.OnConnect += new EventHandler(trinket_OnConnect);
            trinket.OnDisconnect += new EventHandler(trinket_OnDisconnect);

            // the last used serial port is remembered inside an INI file
            ini = new IniFile(Application.ExecutablePath + ".settings.ini");
            lastPortName = ini.Read("SerialPort", "LastPort");
            if (lastPortName != null)
            {
                lastPortName = lastPortName.Trim();
                if (lastPortName.Length == 0)
                {
                    lastPortName = null;
                    // just in case the ini file is malformed
                }
            }

            if (lastPortName != null)
            {
                try
                {
                    // attempt to open the serial port immediately if the previous port exists
                    port = new SerialPort(lastPortName);
                    port.Open();
                    port.ReadTimeout = 200;
                    port.ReceivedBytesThreshold = 1;
                    port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("Serial Port \"" + lastPortName + "\" Open Error: " + ex.Message);
                }
            }

            if (lastPortName == null || port == null || port.IsOpen == false)
            {
                MessageBox.Show("Please Select a COM Port!");
            }

            Thread t = new Thread(new ThreadStart(SerPortCheckThread));
            t.Start();
        }