Exemplo n.º 1
0
        /*
         * Once a database is loaded, this method will be called
         * to add all the messages to the ComboBox.
         */
        public List <Message> LoadMessages()
        {
            Kvadblib.MessageHnd mh;
            Kvadblib.Status     status;
            Kvadblib.NodeHnd    nh;

            List <Message> messages = new List <Message>();

            status = Kvadblib.GetFirstMsg(dh, out mh);
            while (status == Kvadblib.Status.OK)
            {
                string           name;
                int              id;
                int              dlc;
                string           nodeName;
                Kvadblib.MESSAGE flags;
                status = Kvadblib.GetMsgName(mh, out name);
                status = Kvadblib.GetMsgId(mh, out id, out flags);
                status = Kvadblib.GetMsgDlc(mh, out dlc);
                status = Kvadblib.GetMsgSendNode(mh, out nh);
                status = Kvadblib.GetNodeName(nh, out nodeName);

                Kvadblib.AttributeHnd ah;
                int attEunmVal = 8;
                status = Kvadblib.GetAttributeByName(dh, "GenMsgSendType", out ah);    //获取ah用于存储对应message的ah
                status = Kvadblib.GetMsgAttributeByName(mh, "GenMsgSendType", ref ah); //获取对应message的attribute信息
                Kvadblib.GetAttributeValueEnumeration(ah, out attEunmVal);

                int GenMsgCycleTime = 0;
                status = Kvadblib.GetAttributeByName(dh, "GenMsgCycleTime", out ah);    //获取ah用于存储对应message的ah
                status = Kvadblib.GetMsgAttributeByName(mh, "GenMsgCycleTime", ref ah); //获取对应message的attribute信息
                Kvadblib.GetAttributeValueInt(ah, out GenMsgCycleTime);


                Message message = new Message();
                message.id              = id;
                message.name            = name;
                message.dlc             = dlc;
                message.tx_node         = nodeName;
                message.GenMsgSendType  = this.GenMsgSendType[attEunmVal];
                message.GenMsgCycleTime = GenMsgCycleTime;
                message.CycleCount      = 0;

                messages.Add(message);


                status = Kvadblib.GetNextMsg(dh, out mh);
            }
            return(messages);
        }
Exemplo n.º 2
0
        private void DumpMessage(int id, string idStr, byte[] data, int dlc, int flags, long time)
        {
            Kvadblib.Status     status;
            Kvadblib.MessageHnd mh = new Kvadblib.MessageHnd();
            Kvadblib.SignalHnd  sh = new Kvadblib.SignalHnd();

            //Flips the EXT bit if the EXT flag is set
            if ((flags & Canlib.canMSG_EXT) != 0)
            {
                id ^= -2147483648;
            }
            //Find the database message whose id matches the one
            //from the incoming message
            status = Kvadblib.GetMsgById(dbhandle, id, out mh);
            outputBox.AppendText(string.Format("Reading message with id {0}", idStr) + Environment.NewLine);

            //Print the message info
            if (status == Kvadblib.Status.OK)
            {
                string msgName;
                status = Kvadblib.GetMsgName(mh, out msgName);

                outputBox.AppendText(string.Format("Message received: {0}", msgName) + Environment.NewLine);

                int msgId;
                Kvadblib.MESSAGE msgFlags;
                status = Kvadblib.GetMsgId(mh, out msgId, out msgFlags);

                outputBox.AppendText(string.Format("Id: {0}, flags: {1}", msgId, msgFlags) + Environment.NewLine);

                //Iterate through all the signals and print their name, value and unit
                status = Kvadblib.GetFirstSignal(mh, out sh);
                while (status == Kvadblib.Status.OK)
                {
                    string signalname;
                    status = Kvadblib.GetSignalName(sh, out signalname);

                    string unit;
                    status = Kvadblib.GetSignalUnit(sh, out unit);

                    double value;
                    status = Kvadblib.GetSignalValueFloat(sh, out value, data, dlc);

                    outputBox.AppendText(string.Format("Signal - {0}: {1} {2}", signalname, value, unit) + Environment.NewLine);

                    status = Kvadblib.GetNextSignal(mh, out sh);
                }
            }
        }
Exemplo n.º 3
0
        public static List <CanMessage> OpenCanDB(string fullFilePath)
        {
            Kvadblib.Hnd    db_handle;
            Kvadblib.Status status;


            status = Kvadblib.Open(out db_handle);
            status = Kvadblib.Create(db_handle, "MyTestDB", fullFilePath);

            if (status == Kvadblib.Status.Err_DbFileOpen)
            {
                throw new Exception("Could not open CAN database file"); // TODO: Handle more gracefully
            }
            if (status == Kvadblib.Status.Err_DbFileParse)
            {
                string errorMessage;
                status = Kvadblib.GetLastParseError(out errorMessage);
                throw new Exception("Could not parse file. Error: \n" + errorMessage);
            }


            List <CanMessage> Messages = new List <CanMessage>();

            // Get the first message in the database
            Kvadblib.MessageHnd messageHandle;
            status = Kvadblib.GetFirstMsg(db_handle, out messageHandle);
            if (status != Kvadblib.Status.OK)
            {
                throw new Exception("kvaDbGetFirstMsg failed: " + status.ToString()); // TODO: Handle more gracefully
            }
            // Go through all messages in the database
            while (status == Kvadblib.Status.OK)
            {
                string           tempMessageName;
                string           tempMessageQualifiedName;
                string           tempMessageComment;
                int              tempMessageID;
                Kvadblib.MESSAGE tempFlags;
                int              tempMessageDlc;

                // Get the properties for each message
                status = Kvadblib.GetMsgName(messageHandle, out tempMessageName);
                if (status != Kvadblib.Status.OK)
                {
                    throw new Exception("kvaDbGetMsgName failed: " + status.ToString()); // TODO: Handle more gracefully
                }
                status = Kvadblib.GetMsgQualifiedName(messageHandle, out tempMessageQualifiedName);
                if (status != Kvadblib.Status.OK)
                {
                    throw new Exception("kvaDbGetMsgQualifiedName failed: " + status.ToString());
                }

                status = Kvadblib.GetMsgComment(messageHandle, out tempMessageComment);
                if (status != Kvadblib.Status.OK)
                {
                    throw new Exception("kvaDbGetMsgComment failed: " + status.ToString()); // TODO: Handle more gracefully
                }
                status = Kvadblib.GetMsgIdEx(messageHandle, out tempMessageID);
                if (status != Kvadblib.Status.OK)
                {
                    throw new Exception("kvaDbGetMsgId failed: " + status.ToString()); // TODO: Handle more gracefully
                }
                status = Kvadblib.GetMsgFlags(messageHandle, out tempFlags);
                if (status != Kvadblib.Status.OK)
                {
                    throw new Exception("GetMsgFlags failed: " + status.ToString()); // TODO: Handle more gracefully
                }
                status = Kvadblib.GetMsgDlc(messageHandle, out tempMessageDlc);
                if (status != Kvadblib.Status.OK)
                {
                    throw new Exception("kvaDbGetMsgDlc failed: " + status.ToString()); // TODO: Handle more gracefully
                }
                CanMessage tempCanMessage = new CanMessage
                {
                    Comment       = tempMessageComment,
                    DLC           = tempMessageDlc,
                    Flags         = tempFlags,
                    ID            = tempMessageID,
                    Name          = tempMessageName,
                    QualifiedName = tempMessageQualifiedName,
                };
                Messages.Add(tempCanMessage);

                // Go through all signals for this message
                Kvadblib.SignalHnd signalHandle;
                status = Kvadblib.GetFirstSignal(messageHandle, out signalHandle);
                while (status == Kvadblib.Status.OK)
                {
                    string tempSignalName;
                    string tempSignalQualifiedName;
                    string tempSignalComment;
                    string tempSignalUnit;
                    Kvadblib.SignalEncoding tempSignalEncoding;
                    Kvadblib.SignalType     tempSignalType;
                    int    tempStartbit    = 0;
                    int    tempLength      = 0;
                    double tempMinval      = 0;
                    double tempMaxval      = 0;
                    double tempScaleFactor = 0;
                    double tempOffset      = 0;

                    // Get the properties for each signal.
                    status = Kvadblib.GetSignalName(signalHandle, out tempSignalName);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalName failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalQualifiedName(signalHandle, out tempSignalQualifiedName);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalQualifiedName failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalComment(signalHandle, out tempSignalComment);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalComment failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalUnit(signalHandle, out tempSignalUnit);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalUnit failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalEncoding(signalHandle, out tempSignalEncoding);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalEncoding failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalRepresentationType(signalHandle, out tempSignalType);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalRepresentationType failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalValueLimits(signalHandle, out tempMinval, out tempMaxval);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalValueLimits failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalValueScaling(signalHandle, out tempScaleFactor, out tempOffset);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalValueScaling failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    status = Kvadblib.GetSignalValueSize(signalHandle, out tempStartbit, out tempLength);
                    if (status != Kvadblib.Status.OK)
                    {
                        throw new Exception("kvaDbGetSignalValueSize failed: " + status.ToString()); // TODO: Handle more gracefully
                    }
                    CanSignal tempCanSignal = new CanSignal
                    {
                        Comment       = tempSignalComment,
                        Encoding      = tempSignalEncoding,
                        Length        = tempLength,
                        MaxValue      = tempMaxval,
                        MinValue      = tempMinval,
                        Name          = tempSignalName,
                        Offset        = tempOffset,
                        QualifiedName = tempSignalQualifiedName,
                        ScaleFactor   = tempScaleFactor,
                        StartBit      = tempStartbit,
                        Type          = tempSignalType,
                        Unit          = tempSignalUnit,
                    };
                    tempCanMessage.Signals.Add(tempCanSignal);

                    status = Kvadblib.GetNextSignal(messageHandle, out signalHandle);
                }
                status = Kvadblib.GetNextMsg(db_handle, out messageHandle);
            }

            // All done; close database
            Kvadblib.Close(db_handle);
            return(Messages);
        }