Esempio n. 1
0
 public static void ListPorts()
 {
     foreach (var p in SerialPortWrapper.ListPorts())
     {
         Console.WriteLine(p);
     }
 }
Esempio n. 2
0
        public static void Scan(string portName)
        {
            var port = new SerialPortWrapper(portName);

            if (!port.Open())
            {
                Console.WriteLine("Failed to open specified port. Exiting.");
                return;
            }

            port.WriteByte(I2CCommands.SCAN);
            var devicesCount = port.ReadByte();

            if (devicesCount == 0)
            {
                Console.WriteLine("No devices found.");
                return;
            }

            var devices = port.ReadBytes(devicesCount);

            Console.WriteLine($"Found {devicesCount} devices:");
            foreach (byte d in devices)
            {
                if (d > 127)
                {
                    Console.WriteLine($"{d - 128:X2} [ERROR]");
                }
                else
                {
                    Console.WriteLine($"{d:X2}");
                }
            }
        }
Esempio n. 3
0
 public void ConnectAgain()
 {
     try
     {
         //连接到某串口 初始化连接句柄、、
         //连接串口相当于是连接reader设备、、配对后虚拟出来串口 真正连接需要连接串口、、
         if (!this.IE.MoveNext())
         {
             //Service.Instance.log("ConnectAgain" + "Noport");
             this.portNames = System.IO.Ports.SerialPort.GetPortNames();
             this.list      = new ArrayList(this.portNames);
             this.IE        = list.GetEnumerator();
             this.IE.Reset();
         }
         else
         {
             String port = this.IE.Current.ToString();
             //Service.Instance.log("ConnectAgain" + "port:" + port);
             IAsciiSerialPort serialPort = new SerialPortWrapper(port);
             //连接到串口、、之后 commander 通过方法发送的命令就是 往串口里面写入数据、、
             this.commander.Connect(serialPort);
         }
     }
     catch (Exception ex)
     {
         String message = ex.Message;
         //Service.Instance.log("AgainConnection yichang" + message);
     }
 }
Esempio n. 4
0
        public RouterUI()
        {
            InitializeComponent();
            serial = new SerialPortWrapper();
            robot  = new Robot.Robot(serial);
            hw     = robot as IHardware;
            router = new Router.Router(hw);
            hw.onPositionUpdate += new EventHandler(PosUpdate);

            // Setup for doing our own form painting
            //this.SetStyle(
            //  ControlStyles.AllPaintingInWmPaint |
            //  ControlStyles.UserPaint |
            //  ControlStyles.DoubleBuffer, true);
            t          = new Timer();
            t.Interval = 25;
            t.Tick    += new EventHandler(t_Tick);
            t.Enabled  = false;
            //t.Start();

            this.propertyGrid.SelectedGridItemChanged += new SelectedGridItemChangedEventHandler(propertyGrid_SelectedGridItemChanged);

            userControl11.SelectedItemChanged += new EventHandler(userControl11_SelectedItemChanged);
            userControl11.AddObject(router);
            //g1 = new InvoluteGear();
            //g2 = new InvoluteGear();
            //userControl11.AddObject(g1);
            //g2.X = 122.5493f * 2;
            //g2.Rotation = 180.0f / 11.0f + 0.425f;
            //userControl11.AddObject(g2);
        }
Esempio n. 5
0
        public COMPortForm(SerialPortWrapper p)
        {
            if (p == null)
            {
                throw new NullReferenceException("Serial Port Wrapper object cannot be null");
            }

            InitializeComponent();
            port = p;

            t_Tick(null, EventArgs.Empty);
            baudBox.Text = port.BaudRate.ToString();
            SelectPortName(port.PortName);
            if (port.IsOpen)
            {
                connect.Text = "Disconnect";
            }
            else
            {
                if (comboBox1.SelectedItem == null || comboBox1.SelectedItem == "")
                {
                    connect.Enabled = false;
                }
                //if (listBox1.SelectedItem == null)
                //{
                //    connect.Enabled = false;
                //}
            }

            t          = new Timer();
            t.Interval = 100;
            t.Tick    += new EventHandler(t_Tick);
            t.Start();
        }
Esempio n. 6
0
 void OnDisable()
 {
     if (_serialPortWrapper != null)
     {
         _serialPortWrapper.KillThread();
         _serialPortWrapper = null;
     }
 }
Esempio n. 7
0
 public Robot(SerialPortWrapper serial)
 {
     commandGenerator         = null;
     this.serial              = serial;
     serial.newDataAvailable += new SerialPortWrapper.newDataAvailableDelegate(NewDataAvailable);
     t          = new Timer();
     t.Interval = 50;
     t.Start();
     t.Elapsed += new ElapsedEventHandler(t_Elapsed);
 }
    void OnEnable()
    {
        if (_serialPortWrapper != null)
        {
            _serialPortWrapper.KillThread();
        }

        _serialPortWrapper = new SerialPortWrapper(PortName, BaudRate);
        // _serialPortWrapper.onMessageCallback = OnMessage;
    }
Esempio n. 9
0
 public Robot(SerialPortWrapper serial)
 {
     this.serial = serial;
     serial.newDataAvailable += new SerialPortWrapper.newDataAvailableDelegate(NewDataAvailable);
     serial.receiveDataError += new SerialPortWrapper.receiveDataErrorDelegate(ReceiveDataError);
     t = new Timer();
     t.Interval = 50;
     t.Start();
     t.Elapsed += new ElapsedEventHandler(t_Elapsed);
 }
Esempio n. 10
0
        private void UpdateButtonEnabled()
        {
            if (__port.SelectedIndex == -1)
            {
                __ok.Enabled = false;
                return;
            }

            SerialPortWrapper wrap = __port.SelectedItem as SerialPortWrapper;

            __ok.Enabled = wrap.FirmwareInfo != null;
        }
Esempio n. 11
0
 private SerialPortDefinition GetDefinition(SerialPortWrapper port)
 {
     return(new SerialPortDefinition()
     {
         Id = port.Id,
         PortName = port.PortName,
         IsConnected = port.IsConnected,
         IsAborted = port.IsAborted,
         IsSingleStep = port.IsSingleStep,
         CommandsInQueue = port.CommandsInQueue
     });
 }
Esempio n. 12
0
 /// <summary>
 /// Connects to the reader using the current <see cref="PortName"/>
 /// </summary>
 public void Connect()
 {
     try
     {
         IAsciiSerialPort serialPort = new SerialPortWrapper(this.PortName);
         this.commander.Connect(serialPort);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
 }
Esempio n. 13
0
        public static void IdentifyInteface(string portName)
        {
            var port = new SerialPortWrapper(portName);

            if (!port.Open())
            {
                Console.WriteLine("Failed to open specified port. Exiting.");
                return;
            }

            port.WriteByte(I2CCommands.IDENT);
            var size = port.ReadByte();

            Console.WriteLine(port.ReadString(size));
        }
Esempio n. 14
0
 /// <summary>
 /// Connects to the reader using the current <see cref="PortNameAfter"/>
 /// </summary>
 public void Connect()
 {
     try
     {
         IAsciiSerialPort serialPort = new SerialPortWrapper(this.PortNameAfter);
         this.commander.Connect(serialPort);
     }
     catch (Exception ex)
     {
         String message = ex.Message;
         //Service.Instance.log("Connection yichang"+message);
         ConnectAgain();
         System.Diagnostics.Debug.WriteLine(ex.Message);
     }
 }
Esempio n. 15
0
        private void TestComPort(Object comWrapper)
        {
            SerialPortWrapper wrap = (SerialPortWrapper)comWrapper;

            wrap.IsTesting   = true;
            wrap.DisplayName = "Testing...";

            Invoke(new DoSub(delegate() {
                _ports.ResetBindings();
            }));

            try {
                PTFirmwareInfo fw = PTUnit.GetFirmwareInfo(wrap.PortName);
                if (fw == null)
                {
                    wrap.DisplayName = "Not detected";
                }
                else
                {
                    wrap.DisplayName = fw.FirmwareString;
                }

                wrap.FirmwareInfo = fw;
            } catch (SystemException sex) {
                wrap.DisplayName  = "Exception: " + sex.Message;
                wrap.FirmwareInfo = null;
            }

            Invoke(new DoSub(delegate() {
                _ports.ResetBindings();
            }));

            wrap.IsTesting = false;

            Invoke(new DoSub(delegate() {
                foreach (SerialPortWrapper wrapper in _ports)
                {
                    if (wrapper.IsTesting)
                    {
                        return;
                    }
                }

                // then none of the ports are currently being tested, so the buttons can be re-enabled
                UpdateButtonEnabled();
                this.__test.Enabled = true;
            }));
        }
Esempio n. 16
0
 void Awake()
 {
     if (_serialPort != null)
     {
         _serialPort.KillThread();
     }
     if (!IsRuning)
     {
         _serialPort = new SerialPortWrapper("/dev/tty.usbserial-A8004whG", 115200);
         _serialPort.onMessageCallback = OnMessageByte;
         IsRuning = true;
         //thread_ = new Thread(Read);
         //thread_.Start();
         Debug.Log("Awake");
     }
 }
Esempio n. 17
0
 void Awake()
 {
     _serialPort = new SerialPortWrapper("COM16", 9600);
 }
 public MainFormParamViewModel()
 {
     using (var serialPortWrapper = new SerialPortWrapper())
         SerialPorts = serialPortWrapper.GetValidPortNames().ToList();
 }
Esempio n. 19
0
 public RobotControl()
 {
     InitializeComponent();
     serial = new SerialPortWrapper();
 }
        public bool Start(string portName)
        {
            txQueue = new ConcurrentQueue <byte[]>();

            md = new MessageDispatcher();
            md.AddHandler <PingReplyMessage>(m =>
            {
                devicePresence[m.Source - 1] = true;
                Console.WriteLine("Device {0} is present [{1:0.0} ms]", m.Source, (DateTime.Now - pingTimer).TotalMilliseconds);
            });
            md.AddHandler <NothingTokenMessage>(m =>
            {
                TokenReceived(m.Destination);
            });
            md.AddHandler <ReportTokenMessage>(m =>
            {
                TokenReceived(m.Destination);

                for (int i = 0; i < tokenRingLength - 1; i++)
                {
                    qualityMatrix[m.Source - 1, i] = ((sbyte)m.Rssis[i]);
                }

                if (m.Destination == tokenRingLength)                 // last device in the ring means a full sweep has been made
                {
                    Task.Factory.StartNew(() =>
                    {
                        var now  = DateTime.Now;
                        var time = now - lastTime;
                        lastTime = now;

                        foreach (var lqi in lqis)                         // calculate lqi as the average of sensed RSSI by either end point of a link
                        {
                            int a = lqi.EndPointA;
                            int b = lqi.EndPointB;
                            if (qualityMatrix[a, b] > -5 || qualityMatrix[b, a] > -5)
                            {
                                lqi.Quality = 0;
                            }
                            else
                            {
                                lqi.Quality = (qualityMatrix[a, b] + qualityMatrix[b, a]) / 2.0;
                            }
                        }

                        if (SweepCompleted != null)
                        {
                            SweepCompleted(lqis);
                        }
                    });
                }
            });

            try
            {
                var sp = new SerialPort(portName, 500000);
                var pl = new SerialPortWrapper(sp);
                dll = new FrameTransceiver(pl);
                dll.FrameReceived += md.HandleFrame;
                dll.Start();
            }
            catch
            {
                return(false);
            }

            Thread.Sleep(200);

            DetectDevices();             // detect which devices are online and detect the length of the ring

            List <LinkQualityIndicator> l = new List <LinkQualityIndicator>();

            qualityMatrix = new double[tokenRingLength - 1, tokenRingLength - 1];

            for (int i = 0; i < tokenRingLength - 1; i++)
            {
                for (int j = 0; j < tokenRingLength - 1; j++)
                {
                    qualityMatrix[i, j] = 0.0;

                    if (i < j)
                    {
                        l.Add(new LinkQualityIndicator {
                            EndPointA = i, EndPointB = j, Quality = 0.0
                        });
                    }
                }
            }

            lqis = l.ToArray();

            Console.WriteLine("Found " + (tokenRingLength - 1) + " devices");

            dll.Send(nothingToken);             // start the token passing

            Console.WriteLine("Started");

            return(true);
        }
Esempio n. 21
0
 void OnEnable()
 {
     _serialPortWrapper?.KillThread();
     _serialPortWrapper = new SerialPortWrapper(PortName, BaudRate);
 }
Esempio n. 22
0
        static ArduinoHelper()
        {
            var com = ConfigurationManager.AppSettings["CookingSerialPort"];

            SerialPortWrapper = new SerialPortWrapper(com);
        }
Esempio n. 23
0
 public RobotGUI(SerialPortWrapper serial, Router.Router router) : base(serial)
 {
     this.router = router;
     base.onRobotStatusChange += new EventHandler(RouterPositionUpdate);
 }
        public bool Start(string portName, int baudrate)
        {
            nextFrameEntryNumber = 1;
            nextLogEntryNumber   = 1;

            try
            {
                receivedFrames = new List <FrameEntry>();
                receivedLogs   = new List <LogEntry>();

                md = new MessageDispatcher();
                md.MessagePredicate += (d, s) => { return(d == LOCAL_ID || d == BROADCAST_ID); };
                md.AddHandler <PingReplyMessage>(m => { });
                md.AddHandler <ReportFrameMessage>(m =>
                {
                    lock (receivedFrames)
                    {
                        receivedFrames.Add(new FrameEntry {
                            Number = nextFrameEntryNumber++, Destination = m.To, Source = m.From, Time = DateTime.Now, Payload = m.Payload
                        });
                    }
                });
                md.AddHandler <ReportApplicationMessageMessage>(m =>
                {
                    lock (receivedLogs)
                    {
                        receivedLogs.Add(new LogEntry {
                            Type = LogEntry.TYPE_MESSAGE, Level = LogEntry.LEVEL_APPLICATION, Number = nextLogEntryNumber++, Source = m.Source, Time = DateTime.Now, Text = ASCIIEncoding.ASCII.GetString(m.Message)
                        });
                    }
                });
                md.AddHandler <ReportApplicationWarningMessage>(m =>
                {
                    lock (receivedLogs)
                    {
                        receivedLogs.Add(new LogEntry {
                            Type = LogEntry.TYPE_WARNING, Level = LogEntry.LEVEL_APPLICATION, Number = nextLogEntryNumber++, Source = m.Source, Time = DateTime.Now, Text = ASCIIEncoding.ASCII.GetString(m.Message)
                        });
                    }
                });
                md.AddHandler <ReportApplicationErrorMessage>(m =>
                {
                    lock (receivedLogs)
                    {
                        receivedLogs.Add(new LogEntry {
                            Type = LogEntry.TYPE_ERROR, Level = LogEntry.LEVEL_APPLICATION, Number = nextLogEntryNumber++, Source = m.Source, Time = DateTime.Now, Text = ASCIIEncoding.ASCII.GetString(m.Message)
                        });
                    }
                });
                md.AddHandler <ReportFrameworkMessageMessage>(m =>
                {
                    lock (receivedLogs)
                    {
                        receivedLogs.Add(new LogEntry {
                            Type = LogEntry.TYPE_MESSAGE, Level = LogEntry.LEVEL_FRAMEWORK, Number = nextLogEntryNumber++, Source = m.Source, Time = DateTime.Now, Text = ASCIIEncoding.ASCII.GetString(m.Message)
                        });
                    }
                });
                md.AddHandler <ReportFrameworkWarningMessage>(m =>
                {
                    lock (receivedLogs)
                    {
                        receivedLogs.Add(new LogEntry {
                            Type = LogEntry.TYPE_WARNING, Level = LogEntry.LEVEL_FRAMEWORK, Number = nextLogEntryNumber++, Source = m.Source, Time = DateTime.Now, Text = ASCIIEncoding.ASCII.GetString(m.Message)
                        });
                    }
                });
                md.AddHandler <ReportFrameworkErrorMessage>(m =>
                {
                    lock (receivedLogs)
                    {
                        receivedLogs.Add(new LogEntry {
                            Type = LogEntry.TYPE_ERROR, Level = LogEntry.LEVEL_FRAMEWORK, Number = nextLogEntryNumber++, Source = m.Source, Time = DateTime.Now, Text = ASCIIEncoding.ASCII.GetString(m.Message)
                        });
                    }
                });
                md.AddHandler <ReportMessagesDroppedMessage>(m =>
                {
                    nextFrameEntryNumber += m.FramesDropped;
                    nextLogEntryNumber   += m.LogsDropped;
                });

                Task.Factory.StartNew(() =>
                {
                    while (true)
                    {
                        md.HandleFrame(ReportApplicationMessageMessage.Create(LOCAL_ID, 123, new byte[30] {
                            65, 66, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                        }));

                        md.HandleFrame(ReportFrameMessage.Create(LOCAL_ID, 123, 1, 2, new byte[32] {
                            65, 0, 66, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                        }));

                        Thread.Sleep(200);
                    }
                });

                return(true);

                var sp = new SerialPort(portName, baudrate);
                var pl = new SerialPortWrapper(sp);
                dll = new FrameTransceiver(pl);
                dll.FrameReceived += md.HandleFrame;
                dll.Start();
            }
            catch
            {
                return(false);
            }

            return(true);
        }