Ejemplo n.º 1
0
        static void StartListener()
        {
            var port = ConfigurationManager.AppSettings["XBeePort"];

            //LinuxSerialPort
            xBee = new LinuxSerialPort(port, 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
            try
            {
                if (xBee.IsOpen)
                {
                    xBee.Close();
                }
                xBee.Open();
                Console.WriteLine("XBEE is ready...");
                xBee.DataReceived += (object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
                                     =>
                {
                    try
                    {
                        var jsonStr = xBee.ReadLine();//TempData;
                        Console.WriteLine(jsonStr);
                        var node = JsonConvert.DeserializeObject <SensorData>(jsonStr);

                        if (node != null)
                        {
                            //SendDeviceToCloudMessagesAsync(node);
                            PublishMessage(jsonStr);
                            SendToPowerBI(node);
                        }
                        else
                        {
                            Console.WriteLine("serialize to json failed");
                        }
                    }catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Ejemplo n.º 2
0
        public void LoopReadPosition(Action <PositionRecord> onPosition)
        {
            // float latAvg = 0f;
            // float lngAvg = 0f;
            // float weight = 0f;

            using (var Serial_tty = new LinuxSerialPort(portName)
            {
                BaudRate = 9600
            })
            {
                Serial_tty.Open();

                var stream = new StreamReader(Serial_tty.BaseStream);

                for (int i = 0; i < 1000; i++)
                {
                    var lin = stream.ReadLine();

                    var parts = lin.Split(',');
                    if (!lin.StartsWith("$GPRMC") ||
                        parts[2] != "A")
                    {
                        continue;
                    }

                    onPosition(new PositionRecord()
                    {
                        position = new Vector(ParseCoordinate(parts[5], parts[6][0]), ParseCoordinate(parts[3], parts[4][0])),
                        speed    = float.Parse(parts[7]),
                        course   = float.Parse(parts[8]),
                        mode     = (GPSStatus)Enum.Parse(typeof(GPSStatus), parts[10])
                    });
                }
            }
        }
Ejemplo n.º 3
0
        public void LoopReadPosition(Action <AttitudeInfo> onAttitude)
        {
            const string portName = "/dev/ttyUSB0";

            using (var Serial_tty = new LinuxSerialPort(portName)
            {
                BaudRate = 57600
            })
            {
                Serial_tty.Open();

                Stream stream = Serial_tty.BaseStream;

                var start    = DateTime.Now;
                int count    = 0;
                var attitude = new AttitudeInfo();

                for (int i = 0; i < 100000; i++)
                {
                    if (0x55 != stream.ReadByte())
                    {
                        continue;
                    }

                    var cmd = stream.ReadByte();

                    int checksum = 0x55 + cmd;
                    ibuff = 0; // reset
                    for (int j = 0; j < 8; j++)
                    {
                        checksum += (int)(buffer[j] = (byte)stream.ReadByte());
                    }

                    var recvCS = stream.ReadByte();
                    if ((byte)(checksum & 0xFF) != recvCS)
                    {
                        continue;
                    }

                    switch (cmd)
                    {
                    case 0x51:
                        var x = GetAcc(stream);
                        var y = GetAcc(stream);
                        var z = GetAcc(stream);

                        attitude.AccX = x;
                        attitude.AccY = y;
                        attitude.AccZ = z;
                        //                        Console.WriteLine($"X:{x}\tY:{y}\tZ:{z}\tG:{Math.Sqrt(x*x+y*y+z*z)}");
                        count++;
                        break;

                    case 0x52:
                        GetAngle(stream);
                        GetAngle(stream);
                        var hdg = GetAngle(stream);

                        attitude.Heading = hdg;
                        //                        Console.WriteLine($"X:{x}\tY:{y}\tZ:{z}\tG:{Math.Sqrt(x*x+y*y+z*z)}");
                        count++;
                        break;

                    case 0x53:
                        var pitch = -1.0f * (GetAngle(stream) - 90f);
                        var roll  = GetAngle(stream);
                        var yaw   = (-1.0F * GetAngle(stream) + 360 + 140) % 360;
                        var temp  = GetInt(stream) / 100.0;

                        attitude.Pitch = pitch;
                        attitude.Roll  = roll;
                        attitude.Yaw   = yaw;
                        attitude.Temp  = (float)temp;

                        count++;
                        break;
                    }

                    if (count >= 3)
                    {
                        onAttitude(attitude);
                        count = 0;
                    }
                }
            }
        }