/* * Constructs a form for creating messages. * Consists of one TextBox for every signal in the loaded message. */ private List <Signal> LoadSignals(int dlc) { Kvadblib.SignalHnd sh; Kvadblib.Status status = Kvadblib.GetFirstSignal(msgHandle, out sh); List <Signal> signals = new List <Signal>(); while (status == Kvadblib.Status.OK) { string name; string unit; double min, max, scale, offset, val; //byte[] data = new byte[dlc]; //Construct the text for the label status = Kvadblib.GetSignalName(sh, out name); status = Kvadblib.GetSignalUnit(sh, out unit); status = Kvadblib.GetSignalValueLimits(sh, out min, out max); status = Kvadblib.GetSignalValueScaling(sh, out scale, out offset); //status = Kvadblib.GetSignalValueFloat(sh, out val, data, dlc); //Signal s = new Signal(name, max, min, min, scale); Signal s = new Signal(name, max, min, min, scale); signals.Add(s); status = Kvadblib.GetNextSignal(msgHandle, out sh); } return(signals); }
static void createDatabase(Kvadblib.Hnd dh) { DisplayError(Kvadblib.SetFlags(dh, Kvadblib.DATABASE.J1939), "SetFlags"); var nh = new List<Kvadblib.NodeHnd>(); for(int n = 0; n < 4; n++) { Kvadblib.NodeHnd nhx; DisplayError(Kvadblib.AddNode(dh, out nhx), "AddNode"); nh.Add(nhx); DisplayError(Kvadblib.SetNodeName(nhx, "node" + n.ToString()), "SetNodeName"); DisplayError(Kvadblib.SetNodeComment(nhx, "comment" + n.ToString()), "SetNodeComment"); } for (int m = 0; m < 4; m++) { Kvadblib.MessageHnd mh; DisplayError(Kvadblib.AddMsg(dh, out mh), "AddMsg"); DisplayError(Kvadblib.SetMsgName(mh, "msg" + m.ToString()), "SetMsgName"); DisplayError(Kvadblib.SetMsgComment(mh, "comment" + m.ToString()), "SetMsgComment"); DisplayError(Kvadblib.SetMsgId(mh, 0x42 + m, 0), "SetMsgId"); DisplayError(Kvadblib.SetMsgDlc(mh, 8 - m), "SetMsgDlc"); for (int n = 0; n < 4; n++) { Kvadblib.SignalHnd sh; DisplayError(Kvadblib.AddSignal(mh, out sh), "AddSignal"); DisplayError(Kvadblib.SetSignalValueLimits(sh, n, n + 10), "SetSignalValueLimits"); DisplayError(Kvadblib.SetSignalValueScaling(sh, n / 2.0, n), "SetSignalValueScaling"); DisplayError(Kvadblib.SetSignalValueSize(sh, n * 16, 16), "SetSignalValueSize"); DisplayError(Kvadblib.SetSignalName(sh, "sig" + m.ToString() + "_" + n.ToString()), "SetSignalName"); DisplayError(Kvadblib.SetSignalComment(sh, "comment" + n.ToString()), "SetSignalComment"); DisplayError(Kvadblib.SetSignalUnit(sh, "m/s"), "SetSignalUnit"); DisplayError(Kvadblib.SetSignalEncoding(sh, (Kvadblib.SignalEncoding)n), "SetSignalEncoding"); DisplayError(Kvadblib.SetSignalRepresentationType(sh, Kvadblib.SignalType.Signed + n), "SetSignalRepresentationType"); if (n >= 1 && n <= nh.Count) { DisplayError(Kvadblib.AddReceiveNodeToSignal(sh, nh[n - 1]), "AddReceiveNodeToSignal"); } } } }
/* * 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); }
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); } } }
/* * Loads the selected database * returns true if OK. */ private bool LoadDB(string filename) { Kvadblib.Hnd hnd = new Kvadblib.Hnd(); Kvadblib.Status status; Kvadblib.Open(out hnd); status = Kvadblib.ReadFile(hnd, filename); if (status == Kvadblib.Status.OK) { dh = hnd; } return(status == Kvadblib.Status.OK); }
/* * Loads the selected message's signals to construct a form */ public List <Signal> LoadSignalsById(int MsgId) { List <Signal> result = new List <Signal>(); Kvadblib.MessageHnd mh; Kvadblib.Status status = Kvadblib.GetMsgById(dh, MsgId, out mh); if (status == Kvadblib.Status.OK) { Kvadblib.MESSAGE f; int dlc; msgHandle = mh; Kvadblib.GetMsgId(mh, out msgId, out f); Kvadblib.GetMsgDlc(mh, out dlc); msgId = ((MsgId & -2147483648) == 0) ? MsgId : MsgId ^ -2147483648; msgFlags = ((MsgId & -2147483648) == 0) ? 0 : Canlib.canMSG_EXT; hasMessage = true; result = LoadSignals(dlc); } return(result); }
public Form1() { InitializeComponent(); Kvadblib.Status dbstatus; //Load database dbhandle = new Kvadblib.Hnd(); dbstatus = Kvadblib.Open(out dbhandle); dbstatus = Kvadblib.ReadFile(dbhandle, DBCPath); idBox.Text = "318"; DLCBox.Text = "8"; dataBox0.Text = "02"; flagsBox.Text = "0"; //Sets up a BackgroundWorker and adds delegates to //the DumpMessageLoop and ProcessMessage methods dumper = new BackgroundWorker(); dumper.DoWork += DumpMessageLoop; dumper.WorkerReportsProgress = true; dumper.ProgressChanged += new ProgressChangedEventHandler(ProcessMessage); }
/* * Constructs a message from the form and sends it * to the channel */ private bool SendMessage(int dlc, List <Signal> signals) { byte[] data = new byte[dlc]; Kvadblib.Status status = Kvadblib.Status.OK; Kvadblib.SignalHnd sh; bool error = false; foreach (Signal s in signals) { double min, max; status = Kvadblib.GetSignalByName(msgHandle, s.name, out sh); if (status != Kvadblib.Status.OK) { error = true; break; } Kvadblib.GetSignalValueLimits(sh, out min, out max); status = Kvadblib.StoreSignalValuePhys(sh, data, dlc, s.Value); //Check if the signal value was successfully stored and that it's in the correct interval if (status != Kvadblib.Status.OK || s.Value < min || s.Value > max) { error = true; break; } } if (!error) { Canlib.canWriteWait(chanHandle, msgId, data, dlc, msgFlags, 50); } return(error); }
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); }
static void dumpSignal(Kvadblib.SignalHnd sh) { double minval, maxval, factor, offset; DisplayError(Kvadblib.GetSignalValueLimits(sh, out minval, out maxval), "GetSignalValueLimits"); DisplayError(Kvadblib.GetSignalValueScaling(sh, out factor, out offset), "GetSignalValueScaling"); int startbit, length; DisplayError(Kvadblib.GetSignalValueSize(sh, out startbit, out length), "GetSignalValueScaling"); string name, qname; DisplayError(Kvadblib.GetSignalName(sh, out name), "GetSignalName"); DisplayError(Kvadblib.GetSignalQualifiedName(sh, out qname), "GetSignalQualifiedName"); Console.WriteLine("{0} / {1}: {2} - {3} * {4} + {5} @ {6}, {7}", name, qname, minval, maxval, factor, offset, startbit, length); string comment, unit; DisplayError(Kvadblib.GetSignalComment(sh, out comment), "GetSignalComment"); DisplayError(Kvadblib.GetSignalUnit(sh, out unit), "GetSignalUnit"); Kvadblib.SignalEncoding encoding; DisplayError(Kvadblib.GetSignalEncoding(sh, out encoding), "GetSignalEncoding"); Kvadblib.SignalType rtype, ptype; DisplayError(Kvadblib.GetSignalRepresentationType(sh, out rtype), "GetSignalRepresentationType"); DisplayError(Kvadblib.GetSignalPresentationType(sh, out ptype), "GetSignalPresentationType"); Console.WriteLine(" {0} {1} {2} {3} {4}", comment, unit, encoding, rtype, ptype); }
static void dumpNode(Kvadblib.NodeHnd nh) { Console.WriteLine("No way to get node data"); }
static void dumpMessage(Kvadblib.MessageHnd mh) { string name, qname, comment; DisplayError(Kvadblib.GetMsgName(mh, out name), "GetMsgName"); DisplayError(Kvadblib.GetMsgQualifiedName(mh, out qname), "GetMsgQualifiedName"); DisplayError(Kvadblib.GetMsgComment(mh, out comment), "GetMsgComment"); int id, dlc; Kvadblib.MESSAGE flags; DisplayError(Kvadblib.GetMsgId(mh, out id, out flags), "GetMsgId"); DisplayError(Kvadblib.GetMsgDlc(mh, out dlc), "GetMsgDcl"); Console.WriteLine("Message {0} / {1} ({2}): {3}/{4} {5}", name, qname, comment, id, flags, dlc); Kvadblib.SignalHnd sh; Kvadblib.Status status = Kvadblib.GetFirstSignal(mh, out sh); while (status == Kvadblib.Status.OK) { DisplayError(status, "GetFirst/NextSignal"); dumpSignal(sh); status = Kvadblib.GetNextSignal(mh, out sh); } }
static void dumpDatabase(Kvadblib.Hnd dh) { Kvadblib.DATABASE flags; DisplayError(Kvadblib.GetFlags(dh, out flags), "GetFlags"); Console.WriteLine("Flags: {0}", flags); Kvadblib.MessageHnd mh; Kvadblib.Status status = Kvadblib.GetFirstMsg(dh, out mh); while (status == Kvadblib.Status.OK) { DisplayError(status, "GetFirst/NextMsg"); dumpMessage(mh); status = Kvadblib.GetNextMsg(dh, out mh); } Kvadblib.NodeHnd nh; status = Kvadblib.GetFirstNode(dh, out nh); while (status == Kvadblib.Status.OK) { DisplayError(status, "GetFirst/NextNode"); dumpNode(nh); status = Kvadblib.GetNextNode(dh, out nh); } }
static void DisplayError(Kvadblib.Status status, String routineName) { switch (status) { case Kvadblib.Status.OK: break; default: Console.WriteLine("{0} failed: {1}", routineName, status); Environment.Exit(1); break; } }