コード例 #1
0
ファイル: EV3Robot.cs プロジェクト: rbakx/UnityRobot_FHICT
        public EV3Robot(Communicator communicator, string name, Ev3Connection ev3Connection) : base(communicator, name, TypeRobot.Mindstorm)
        {
            if (ev3Connection == null)
            {
                throw new ArgumentNullException("ev3Connection");
            }

            if (!ev3Connection.Connected())
            {
                throw new InvalidOperationException("ev3 Connection is closed");
            }

            _ev3Connection = ev3Connection;
        }
コード例 #2
0
        private async void _OnReceive()
        {
            while (_udpClient != null)
            {
                UdpReceiveResult results;
                try
                {
                    results = await _udpClient.ReceiveAsync();
                }
                catch (ObjectDisposedException)
                {
                    continue;
                }

                string msgStr      = Encoding.ASCII.GetString(results.Buffer);
                Regex  serialRegex = new Regex("Serial-Number: (.*)");
                Regex  portRegex   = new Regex("Port: (.*)");

                string serialNumber = null;
                int    port         = -1;

                Match match = serialRegex.Match(msgStr);
                if (match.Success)
                {
                    serialNumber = match.Groups[1].Value;
                }
                /* serialNumber != null && */
                if (!_connectedSerials.Contains(serialNumber))
                {
                    match = portRegex.Match(msgStr);
                    if (match.Success)
                    {
                        string tcpPortStr = match.Groups[1].Value.TrimEnd();
                        bool   success    = int.TryParse(tcpPortStr, out port);

                        if (!success)
                        {
                            port = -1;
                        }
                    }

                    _connectedSerials.Add(serialNumber);
                    SendUdpResponse(_udpClient, results.RemoteEndPoint);
                    TCPDataLink dl = EstablishTcpConnection(serialNumber,
                                                            results.RemoteEndPoint.Address, port);

                    Ev3Connection con = new Ev3Connection("EV3Wifi", dl);

                    Communicator communicator = null;

                    if (Tools.ConnectToUnity(out communicator, _unityIP.ToString(), _unityPort,
                                             5000))
                    {
                        Console.WriteLine("Unity connected");

                        EV3Robot newRobot = new EV3Robot(communicator, "My ev3", con);
                        Tools.AssignRobot(communicator, newRobot);

                        _eventReceiver.OnRobotConnect(newRobot);
                    }
                    else
                    {
                        throw new Exception("Can't connect to unity...");
                    }
                }
            }
        }