Пример #1
0
        private void SetupSLCanPort(ICommsSerial port)
        {
            //check if we started from within mavlink - if not get settings from menu and create port
            if (port == null || !port.IsOpen)
            {
                switch (MainV2._connectionControl.CMB_serialport.Text)
                {
                case "TCP":
                    port = new TcpSerial();
                    break;

                case "UDP":
                    port = new UdpSerial();
                    break;

                case "WS":
                    port = new WebSocket();
                    break;

                case "UDPCl":
                    port = new UdpSerialConnect();
                    break;

                default:
                    port = new SerialPort()
                    {
                        PortName = MainV2._connectionControl.CMB_serialport.Text,
                        BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text)
                    };
                    break;
                }
            }

            if (can == null)
            {
                can = new DroneCAN.DroneCAN();
            }

            can.SourceNode = 127;

            can.NodeAdded += (id, msg) =>
            {
                this.BeginInvoke((Action) delegate
                {
                    allnodes.Add(new DroneCANModel()
                    {
                        ID     = id,
                        Name   = "?",
                        Health = msg.health.ToString(),
                        Mode   = msg.mode.ToString(),
                        Uptime = TimeSpan.FromSeconds(msg.uptime_sec),
                        VSC    = msg.vendor_specific_status_code
                    });

                    uAVCANModelBindingSource.ResetBindings(false);
                });
            };

            if (!port.IsOpen)
            {
                try
                {
                    port.Open();
                }
                catch (Exception)
                {
                    CustomMessageBox.Show(Strings.CheckPortSettingsOr);
                    return;
                }
            }

            if (chk_log.Checked)
            {
                can.LogFile = Settings.Instance.LogDir + Path.DirectorySeparatorChar +
                              DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".can";
            }

            var prd = new ProgressReporterDialogue();

            prd.UpdateProgressAndStatus(-1, "Trying to connect");
            prd.DoWork          += sender => can.StartSLCAN(port.BaseStream);
            prd.btnCancel.Click += (sender, args) =>
            {
                prd.doWorkArgs.CancelAcknowledged = true;
                port.Close();
            };
            prd.RunBackgroundOperationAsync();

            if (prd.doWorkArgs.CancelRequested || prd.doWorkArgs.ErrorMessage != null)
            {
                return;
            }

            can.SetupFileServer();

            can.SetupDynamicNodeAllocator();

            can.MessageReceived += (frame, msg, transferID) =>
            {
                if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_NodeStatus))
                {
                    var ns = msg as DroneCAN.DroneCAN.uavcan_protocol_NodeStatus;

                    var nodes = allnodes.Where((a) => a.ID == frame.SourceNode);

                    if (nodes.Count() > 0 && nodes.First().Name == "?")
                    {
                        var statetracking = new DroneCAN.DroneCAN.statetracking();
                        // get node info
                        DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_req gnireq =
                            new DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_req()
                        {
                        };
                        gnireq.encode(DroneCAN.DroneCAN.dronecan_transmit_chunk_handler, statetracking);

                        var slcan = can.PackageMessageSLCAN(frame.SourceNode, 30, 0, gnireq);
                        can.WriteToStreamSLCAN(slcan);
                    }

                    foreach (var item in nodes)
                    {
                        switch (ns.health)
                        {
                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK:
                            item.Health = "OK";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_WARNING:
                            item.Health = "WARNING";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_ERROR:
                            item.Health = "ERROR";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_CRITICAL:
                            item.Health = "CRITICAL";
                            break;
                        }

                        switch (ns.mode)
                        {
                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL:
                            item.Mode = "OPERATIONAL";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_MODE_INITIALIZATION:
                            item.Mode = "INITIALIZATION";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_MODE_MAINTENANCE:
                            item.Mode = "MAINTENANCE";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_MODE_SOFTWARE_UPDATE:
                            item.Mode = "SOFTWARE_UPDATE";
                            break;

                        case (byte)DroneCAN.DroneCAN.uavcan_protocol_NodeStatus.UAVCAN_PROTOCOL_NODESTATUS_MODE_OFFLINE:
                            item.Mode = "OFFLINE";
                            break;
                        }

                        item.Uptime = TimeSpan.FromSeconds(ns.uptime_sec);
                    }

                    _updatePending = true;
                }
                else if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_res))
                {
                    var gnires = msg as DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_res;

                    var nodes = allnodes.Where((a) => a.ID == frame.SourceNode);

                    foreach (var item in nodes)
                    {
                        item.Name            = ASCIIEncoding.ASCII.GetString(gnires.name, 0, gnires.name_len);
                        item.HardwareVersion = gnires.hardware_version.major + "." + gnires.hardware_version.minor;
                        item.SoftwareVersion = gnires.software_version.major + "." + gnires.software_version.minor + "." +
                                               gnires.software_version.vcs_commit.ToString("X");
                        item.SoftwareCRC = gnires.software_version.image_crc;
                        item.HardwareUID = gnires.hardware_version.unique_id.Select(a => a.ToString("X2")).Aggregate((a, b) =>
                        {
                            return(a + " " + b);
                        });
                        item.RawMsg = gnires;
                        item.VSC    = gnires.status.vendor_specific_status_code;
                    }

                    _updatePending = true;
                }
                else if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_debug_LogMessage))
                {
                    var debug = msg as DroneCAN.DroneCAN.uavcan_protocol_debug_LogMessage;

                    this.BeginInvoke((Action) delegate()
                    {
                        DGDebug.Rows.Insert(0, new object[]
                        {
                            frame.SourceNode, debug.level.value,
                            ASCIIEncoding.ASCII.GetString(debug.source, 0, debug.source_len),
                            ASCIIEncoding.ASCII.GetString(debug.text, 0, debug.text_len)
                        });
                        if (DGDebug.Rows.Count > 100)
                        {
                            DGDebug.Rows.RemoveAt(DGDebug.Rows.Count - 1);
                        }
                    });
                }
            };
        }
        public void startslcan(byte canport)
        {
            but_slcanmode1.Enabled = false;
            but_slcanmode2.Enabled = false;

            try
            {
                if (!MainV2.comPort.BaseStream.IsOpen)
                {
                    if (CustomMessageBox.Show(
                            "You are not currently connected via mavlink. Please make sure the device is already in slcan mode or this is the slcan serialport.",
                            "SLCAN", CustomMessageBox.MessageBoxButtons.OKCancel) != CustomMessageBox.DialogResult.OK)
                    {
                        return;
                    }
                }

                if (MainV2.comPort.BaseStream.IsOpen)
                {
                    var cport = MainV2.comPort.MAV.param["CAN_SLCAN_CPORT"].Value;
                    MainV2.comPort.setParam((byte)MainV2.comPort.sysidcurrent, (byte)MainV2.comPort.compidcurrent,
                                            "CAN_SLCAN_CPORT", canport, true);
                    if (cport == 0)
                    {
                        CustomMessageBox.Show("Reboot required" + " after setting CPORT. Please reboot!",
                                              Strings.ERROR);
                        return;
                    }

                    MainV2.comPort.setParam((byte)MainV2.comPort.sysidcurrent, (byte)MainV2.comPort.compidcurrent,
                                            "CAN_SLCAN_TIMOUT", 2, true);
                    MainV2.comPort.setParam((byte)MainV2.comPort.sysidcurrent, (byte)MainV2.comPort.compidcurrent,
                                            "CAN_P" + canport + "_DRIVER", 1);
                    //MainV2.comPort.setParam((byte)MainV2.comPort.sysidcurrent, (byte)MainV2.comPort.compidcurrent, "CAN_SLCAN_SERNUM", 0, true); // usb
                    // blind send
                    var paramname = "CAN_SLCAN_SERNUM";
                    var req       = new MAVLink.mavlink_param_set_t
                    {
                        target_system    = (byte)MainV2.comPort.sysidcurrent,
                        target_component = (byte)MainV2.comPort.compidcurrent,
                        param_type       = (byte)MainV2.comPort
                                           .MAVlist[(byte)MainV2.comPort.sysidcurrent, (byte)MainV2.comPort.compidcurrent]
                                           .param_types[paramname],
                        param_id = paramname.MakeBytesSize(16)
                    };
                    MainV2.comPort.sendPacket(req, (byte)MainV2.comPort.sysidcurrent,
                                              (byte)MainV2.comPort.compidcurrent);
                    MainV2.comPort.sendPacket(req, (byte)MainV2.comPort.sysidcurrent,
                                              (byte)MainV2.comPort.compidcurrent);
                }
            }
            catch
            {
            }

            {
                // grab the connected port
                var port = MainV2.comPort.BaseStream;

                // place an invalid port in its place
                if (port != null)
                {
                    MainV2.comPort.BaseStream = new Comms.SerialPort()
                    {
                        PortName = port.PortName, BaudRate = port.BaudRate
                    }
                }
                ;

                //check if we started from within mavlink - if not get settings from menu and create port
                if (port == null || !port.IsOpen)
                {
                    switch (MainV2._connectionControl.CMB_serialport.Text)
                    {
                    case "TCP":
                        port = new TcpSerial();
                        break;

                    case "UDP":
                        port = new UdpSerial();
                        break;

                    case "WS":
                        port = new WebSocket();
                        break;

                    case "UDPCl":
                        port = new UdpSerialConnect();
                        break;

                    default:
                        port = new SerialPort()
                        {
                            PortName = MainV2._connectionControl.CMB_serialport.Text,
                            BaudRate = int.Parse(MainV2._connectionControl.CMB_baudrate.Text)
                        };
                        break;
                    }
                }

                if (can == null)
                {
                    can = new DroneCAN.DroneCAN();
                }

                can.SourceNode = 127;

                can.NodeAdded += (id, msg) =>
                {
                    this.BeginInvoke((Action) delegate
                    {
                        allnodes.Add(new DroneCANModel()
                        {
                            ID     = id,
                            Name   = "?",
                            Health = msg.health.ToString(),
                            Mode   = msg.mode.ToString(),
                            Uptime = TimeSpan.FromSeconds(msg.uptime_sec),
                            VSC    = msg.vendor_specific_status_code
                        });

                        uAVCANModelBindingSource.ResetBindings(false);
                    });
                };

                if (!port.IsOpen)
                {
                    try
                    {
                        port.Open();
                    }
                    catch (Exception)
                    {
                        CustomMessageBox.Show(Strings.CheckPortSettingsOr);
                        return;
                    }
                }

                if (chk_log.Checked)
                {
                    can.LogFile = Settings.Instance.LogDir + Path.DirectorySeparatorChar +
                                  DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".can";
                }

                var prd = new ProgressReporterDialogue();
                prd.UpdateProgressAndStatus(-1, "Trying to connect");
                prd.DoWork          += sender => can.StartSLCAN(port.BaseStream);
                prd.btnCancel.Click += (sender, args) =>
                {
                    prd.doWorkArgs.CancelAcknowledged = true;
                    port.Close();
                };
                prd.RunBackgroundOperationAsync();

                if (prd.doWorkArgs.CancelRequested || prd.doWorkArgs.ErrorMessage != null)
                {
                    return;
                }

                can.SetupFileServer();

                can.SetupDynamicNodeAllocator();

                can.MessageReceived += (frame, msg, transferID) =>
                {
                    if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_NodeStatus))
                    {
                        var ns = msg as DroneCAN.DroneCAN.uavcan_protocol_NodeStatus;

                        var nodes = allnodes.Where((a) => a.ID == frame.SourceNode);

                        if (nodes.Count() > 0 && nodes.First().Name == "?")
                        {
                            var statetracking = new DroneCAN.DroneCAN.statetracking();
                            // get node info
                            DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_req gnireq = new DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_req()
                            {
                            };
                            gnireq.encode(DroneCAN.DroneCAN.dronecan_transmit_chunk_handler, statetracking);

                            var slcan = can.PackageMessage(frame.SourceNode, 30, 0, gnireq);
                            can.WriteToStream(slcan);
                        }

                        foreach (var item in nodes)
                        {
                            switch (ns.health)
                            {
                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_OK:
                                item.Health = "OK";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_WARNING:
                                item.Health = "WARNING";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_ERROR:
                                item.Health = "ERROR";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_HEALTH_CRITICAL:
                                item.Health = "CRITICAL";
                                break;
                            }
                            switch (ns.mode)
                            {
                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_MODE_OPERATIONAL:
                                item.Mode = "OPERATIONAL";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_MODE_INITIALIZATION:
                                item.Mode = "INITIALIZATION";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_MODE_MAINTENANCE:
                                item.Mode = "MAINTENANCE";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_MODE_SOFTWARE_UPDATE:
                                item.Mode = "SOFTWARE_UPDATE";
                                break;

                            case (byte)DroneCAN.DroneCAN.UAVCAN_PROTOCOL_NODESTATUS_MODE_OFFLINE:
                                item.Mode = "OFFLINE";
                                break;
                            }
                            item.Uptime = TimeSpan.FromSeconds(ns.uptime_sec);
                        }

                        _updatePending = true;
                    }
                    else if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_res))
                    {
                        var gnires = msg as DroneCAN.DroneCAN.uavcan_protocol_GetNodeInfo_res;

                        var nodes = allnodes.Where((a) => a.ID == frame.SourceNode);

                        foreach (var item in nodes)
                        {
                            item.Name            = ASCIIEncoding.ASCII.GetString(gnires.name, 0, gnires.name_len);
                            item.HardwareVersion = gnires.hardware_version.major + "." + gnires.hardware_version.minor;
                            item.SoftwareVersion = gnires.software_version.major + "." + gnires.software_version.minor + "." + gnires.software_version.vcs_commit.ToString("X");
                            item.SoftwareCRC     = gnires.software_version.image_crc;
                            item.HardwareUID     = gnires.hardware_version.unique_id.Select(a => a.ToString("X2")).Aggregate((a, b) =>
                            {
                                return(a + " " + b);
                            });
                            item.RawMsg = gnires;
                            item.VSC    = gnires.status.vendor_specific_status_code;
                        }

                        _updatePending = true;
                    }
                    else if (msg.GetType() == typeof(DroneCAN.DroneCAN.uavcan_protocol_debug_LogMessage))
                    {
                        var debug = msg as DroneCAN.DroneCAN.uavcan_protocol_debug_LogMessage;

                        this.BeginInvoke((Action) delegate()
                        {
                            DGDebug.Rows.Insert(0, new object[]
                            {
                                frame.SourceNode, debug.level.value,
                                ASCIIEncoding.ASCII.GetString(debug.source, 0, debug.source_len),
                                ASCIIEncoding.ASCII.GetString(debug.text, 0, debug.text_len)
                            });
                            if (DGDebug.Rows.Count > 100)
                            {
                                DGDebug.Rows.RemoveAt(DGDebug.Rows.Count - 1);
                            }
                        });
                    }
                };
            }
        }

        DroneCAN.DroneCAN can = new DroneCAN.DroneCAN();