Exemplo n.º 1
0
        // This helper function retrieve active Dll Major version number
        //
        private CANResult DllMajorVersion(HardwareType hType, out int majorVersion)
        {
            CANResult Res;
            String    dllVersionStr = "";

            // We execute the "DllVersionInfo" function of the PCANLight
            // using as parameter the Hardware type and a string
            // variable to get the info like "x.xx"
            //
            Res = PCANLight.DllVersionInfo(hType, out dllVersionStr);

            // We retrieve major version number
            // spliting versionNumberStr based on "." decimal symbol
            //
            String[] versionTabInfo = dllVersionStr.Split('.');
            if (versionTabInfo.Length > 0)
            {
                Int32.TryParse(versionTabInfo[0].ToString(), out majorVersion);
            }
            else
            {
                majorVersion = 0;
            }
            return(Res);
        }
Exemplo n.º 2
0
        private void btnInfo_Click(object sender, System.EventArgs e)
        {
            string    strInfo;
            CANResult Res;

            // We execute the "VersionInfo" function of the PCANLight
            // using as parameter the Hardware type and a string
            // variable to get the info.
            //
            Res     = PCANLight.VersionInfo((HardwareType)cbbHws.SelectedIndex, out strInfo);
            strInfo = strInfo.Replace("\n", "\r\n");

            // The function was successfully executed
            //
            if (Res == CANResult.ERR_OK)
            {
                // We show the Version Information
                //
                txtInfo.Text = strInfo;
            }
            // An error occurred.  We show the error.
            //
            else
            {
                txtInfo.Text = "Error: " + Res.ToString();
            }
        }
Exemplo n.º 3
0
        private void btnSetFilter_Click(object sender, EventArgs e)
        {
            uint      FromID, ToID;
            CANResult Res;

            // The range IDs is read
            //
            FromID = Convert.ToUInt32(txtIdFrom.Text, 16);
            ToID   = Convert.ToUInt32(txtIdTo.Text, 16);

            // The desired Filter is set on the configured Hardware
            //
            Res = PCANLight.MsgFilter(ActiveHardware, FromID, ToID, (rdbStandard.Checked) ? MsgTypes.MSGTYPE_STANDARD : MsgTypes.MSGTYPE_EXTENDED);

            // The Filter was successfully set
            //
            if (Res == CANResult.ERR_OK)
            {
                txtInfo.Text = "Filter was successfully SET.\r\n";
            }
            // An error occurred.  We show the error.
            //
            else
            {
                txtInfo.Text = "Error: " + Res.ToString();
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Writes a Generic Message, check ActiveHardware
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="PID"></param>
        /// <param name="DID"></param>
        /// <param name="SID"></param>
        /// <param name="Data"></param>
        public void WriteMessage(uint ID, uint PID, uint DID, uint SID, Byte[] Data)
        {
            #region ValCheck
            if ((int)console.GetActiveHardware() == -1)
            {
                return;
            }
            #endregion

            TCLightMsg       newMsg  = new TCLightMsg();
            TCLightTimestamp newTime = new TCLightTimestamp();

            newMsg.ID = ID | PID | DID | SID;

            newMsg.MsgType = MsgTypes.MSGTYPE_EXTENDED;

            newMsg.Len = 8;

            newMsg.Data = Data;

            newTime.millis = 1002;

            console.AddOutsideMessage(newMsg, newTime);
            PCANLight.Write(console.GetActiveHardware(), newMsg);
        }
Exemplo n.º 5
0
        private void btnWrite_Click(object sender, System.EventArgs e)
        {
            TCLightMsg MsgToSend;
            TextBox    CurrentTextBox;
            CANResult  Res;

            // We create a TCLightMsg message structure
            //
            MsgToSend = new TCLightMsg();

            // We configurate the Message.  The ID (max 0x1FF),
            // Length of the Data, Message Type (Standard in
            // this example) and die data
            //
            MsgToSend.ID      = Convert.ToUInt32(txtID.Text, 16);
            MsgToSend.Len     = Convert.ToByte(nudLength.Value);
            MsgToSend.MsgType = (chbExtended.Checked) ? MsgTypes.MSGTYPE_EXTENDED : MsgTypes.MSGTYPE_STANDARD;
            // If a remote frame will be sent, the data bytes are not important.
            //
            if (chbRemote.Checked)
            {
                MsgToSend.MsgType |= MsgTypes.MSGTYPE_RTR;
            }
            else
            {
                // We get so much data as the Len of the message
                //
                CurrentTextBox = txtData0;
                for (int i = 0; i < MsgToSend.Len; i++)
                {
                    MsgToSend.Data[i] = Convert.ToByte(CurrentTextBox.Text, 16);
                    if (i < 7)
                    {
                        CurrentTextBox = (TextBox)this.GetNextControl(CurrentTextBox, true);
                    }
                }
            }

            // The message is sent to the configured hardware
            //
            Res = PCANLight.Write(ActiveHardware, MsgToSend);

            // The Hardware was successfully sent
            //
            if (Res == CANResult.ERR_OK)
            {
                txtInfo.Text = "Message was successfully SENT.\r\n";
            }
            // An error occurred.  We show the error.
            //
            else
            {
                txtInfo.Text = "Error: " + Res.ToString();
            }
        }
Exemplo n.º 6
0
        public void WriteMessage(TCLightMsg msg)
        {
            #region ValCheck
            if ((int)console.GetActiveHardware() == -1)
            {
                return;
            }
            #endregion

            console.AddOutsideMessage(msg, BuildMessageTime());
            PCANLight.Write(console.GetActiveHardware(), msg);
        }
Exemplo n.º 7
0
        private void AlignMotorBtn_Click(object sender, EventArgs e)
        {
            TCLightMsg MsgToSend = new TCLightMsg();

            MsgToSend.ID      = 0x013E20E0 | 0x00000001;
            MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED;
            TCLightTimestamp q = new TCLightTimestamp();

            q.millis = 1000;
            console.AddOutsideMessage(MsgToSend, q);
            PCANLight.Write(ActiveHardware, MsgToSend);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Sends the Execute Command over the CAN Bus
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExecuteProfileBtn_Click(object sender, EventArgs e)
        {
            #region execute
            TCLightMsg MsgToSend = new TCLightMsg();
            MsgToSend.ID      = 0x013EF0E0 | 0x00000001;
            MsgToSend.Len     = 0;
            MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED;

            PCANLight.Write(ActiveHardware, MsgToSend);
            #endregion

            TCLightTimestamp q = new TCLightTimestamp();
            q.millis = 1000;
            console.AddOutsideMessage(MsgToSend, q);

            SendProfileBtn.Enabled    = false;
            ExecuteProfileBtn.Enabled = false;
        }
Exemplo n.º 9
0
        private void CANReadThreadFunc()
        {
            // Sets the handle of the Receive-Event.
            //
            PCANLight.SetRcvEvent(ActiveHardware, RcvEvent);

            // While this mode is selected
            while (rdbEvent.Checked)
            {
                // Waiting for Receive-Event
                //
                RcvEvent.WaitOne();

                // Process Receive-Event using .NET Invoke function
                // in order to interact with Winforms UI
                //
                this.Invoke(ReadDelegate);
            }
        }
Exemplo n.º 10
0
        private void button1_Click(object sender, EventArgs e)
        {
            #region setup

            TCLightMsg MsgToSend = new TCLightMsg();
            MsgToSend.ID      = 0x013EE8E0 | 0x00000001;
            MsgToSend.Len     = 1;
            MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED;
            MsgToSend.Data[0] = numPackets;

            TCLightTimestamp q = new TCLightTimestamp();
            q.millis = 1000;
            console.AddOutsideMessage(MsgToSend, q);

            PCANLight.Write(ActiveHardware, MsgToSend);


            #endregion
        }
Exemplo n.º 11
0
        private void btnRelease_Click(object sender, System.EventArgs e)
        {
            CANResult Res;

            // We choose Timer method by default
            //
            rdbTimer.Checked = true;

            // We stopt to read from tehe CAN Queue
            //
            tmrRead.Enabled = false;

            // We close the active hardware using the
            // "Close" function of the PCANLight using
            // as parameter the Hardware type.
            //
            Res = PCANLight.Close(ActiveHardware);

            // The Hardware was successfully closed
            //
            if (Res == CANResult.ERR_OK)
            {
                txtInfo.Text = "Hardware was successfully Released.\r\n";
            }
            // An error occurred.  We show the error.
            //
            else
            {
                txtInfo.Text = "Error: " + Res.ToString();
            }

            // We set the varibale of active hardware to None
            // and activate/deactivate the corresponding buttons
            //
            ActiveHardware       = (HardwareType)(-1);
            btnInit.Enabled      = !(btnWrite.Enabled = btnSetFilter.Enabled = btnResetFilter.Enabled = btnRelease.Enabled = btnInfo.Enabled = btnDllInfo.Enabled = false);
            rdbTimer.Enabled     = false;
            rdbEvent.Enabled     = false;
            chbTimeStamp.Enabled = false;
            cbbHws_SelectedIndexChanged(this, new EventArgs());
        }
Exemplo n.º 12
0
        private void btnResetFilter_Click(object sender, EventArgs e)
        {
            CANResult Res;

            // The current Filter on the configured Hardware is reset
            //
            Res = PCANLight.ResetFilter(ActiveHardware);

            // The Filter was successfully reset
            //
            if (Res == CANResult.ERR_OK)
            {
                txtInfo.Text = "Filter was successfully RESET.\r\n";
            }
            // An error occurred.  We show the error.
            //
            else
            {
                txtInfo.Text = "Error: " + Res.ToString();
            }
        }
Exemplo n.º 13
0
        private void btnSetUsbDevNumber_Click(object sender, EventArgs e)
        {
            CANResult Res;

            // The USB Device Number will set
            //
            Res = PCANLight.SetUSBDeviceNr((HardwareType)cbbHws.SelectedIndex, Convert.ToUInt32(txtDevNumber.Text));

            // The Device number was set successfully
            //
            if (Res == CANResult.ERR_OK)
            {
                MessageBox.Show("USB Device Number was set", "SetUSBDevNr");
            }
            // An error occurred.  We show the error.
            //
            else
            {
                MessageBox.Show("Set USB Device Number failed: " + Res.ToString(), "SetUSBDevNr");
            }
        }
Exemplo n.º 14
0
        private void NPTModeBtn_Click(object sender, EventArgs e)
        {
            TCLightMsg MsgToSend = new TCLightMsg();

            MsgToSend.ID      = 0x01745400 | 0x00000001 | 0x000000E0;
            MsgToSend.Len     = 8;
            MsgToSend.Data[0] = (byte)0x01;
            MsgToSend.Data[1] = (byte)0x00;
            MsgToSend.Data[2] = (byte)0x00;
            MsgToSend.Data[3] = (byte)0x00;
            MsgToSend.Data[4] = (byte)0x00;
            MsgToSend.Data[5] = (byte)0x00;
            MsgToSend.Data[6] = (byte)0x00;
            MsgToSend.Data[7] = (byte)0x00;
            MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED;
            TCLightTimestamp q = new TCLightTimestamp();

            q.millis = 1000;
            console.AddOutsideMessage(MsgToSend, q);
            PCANLight.Write(ActiveHardware, MsgToSend);
        }
Exemplo n.º 15
0
        private void ReadMessage()
        {
            TCLightMsg       MyMsg;
            TCLightTimestamp MyTimeStamp = null;
            CANResult        Res;

            // We read at least one time the queue looking for messages.
            // If a message is found, we look again trying to find more.
            // If the queue is empty or an error occurr, we get out from
            // the dowhile statement.
            //
            do
            {
                // We read the queue looking for messages.
                //
                if (chbTimeStamp.Checked)
                {
                    // We execute the "ReadEx" function of the PCANLight
                    // if "Show Time Stamp" checkbox is selected
                    //
                    Res = PCANLight.ReadEx(ActiveHardware, out MyMsg, out MyTimeStamp);
                }
                else
                {
                    // We execute the "Read" function of the PCANLight
                    // if "Show Time Stamp" checkbox isn't selected
                    //
                    Res = PCANLight.Read(ActiveHardware, out MyMsg);
                }

                // A message was received
                // We process the message(s)
                //
                if (Res == CANResult.ERR_OK)
                {
                    ProcessMessage(MyMsg, MyTimeStamp);
                }
            } while (((int)ActiveHardware != -1) && (!Convert.ToBoolean(Res & CANResult.ERR_QRCVEMPTY)));
        }
Exemplo n.º 16
0
        private void btnGetUsbDevNumber_Click(object sender, EventArgs e)
        {
            uint      iDevNum;
            CANResult Res;

            // The USB Device Number will asked
            //
            Res = PCANLight.GetUSBDeviceNr((HardwareType)cbbHws.SelectedIndex, out iDevNum);

            // The Device number was got successfully
            //
            if (Res == CANResult.ERR_OK)
            {
                MessageBox.Show("USB Device Number is: " + iDevNum, "GetUSBDevNr");
            }
            // An error occurred.  We show the error.
            //
            else
            {
                MessageBox.Show("Get USB Device Number failed: " + Res.ToString(), "GetUSBDevNr");
            }
        }
Exemplo n.º 17
0
        private void btnInit_Click(object sender, System.EventArgs e)
        {
            CANResult Res;
            int       majorVersion = 0;

            // Check version 2.x Dll is available
            //
            Res = DllMajorVersion((HardwareType)cbbHws.SelectedIndex, out majorVersion);
            if (Res == CANResult.ERR_OK)
            {
                // Sample must ONLY work if a 2.x or later version of the
                // PCAN-Light is installed
                //
                if (majorVersion < 2)
                {
                    MessageBox.Show("DLL 2.x or later are required to run this program" +
                                    "\r\nPlease, download lastest DLL version on http://www.peak-system.com or refer to the documentation for more information.",
                                    "DLL Version", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // According with the active parameters/hardware, we
                    // use one of the two possible "Init" PCANLight functions.
                    // One is for Plug And Play hardware, and the other for
                    // Not P&P.
                    //
                    if (cbbIO.Enabled)
                    {
                        // Not P&P Hardware
                        //
                        Res = PCANLight.Init((HardwareType)cbbHws.SelectedIndex,
                                             (Baudrates)cbbBaudrates.Tag,
                                             (FramesType)cbbMsgType.SelectedIndex,
                                             Convert.ToUInt32(cbbIO.Text, 16),
                                             Convert.ToByte(cbbInterrupt.Text));
                    }
                    else
                    {
                        // P&P Hardware
                        //
                        Res = PCANLight.Init((HardwareType)cbbHws.SelectedIndex,
                                             (Baudrates)cbbBaudrates.Tag,
                                             (FramesType)cbbMsgType.SelectedIndex);
                    }

                    // The Hardware was successfully initiated
                    //
                    if (Res == CANResult.ERR_OK)
                    {
                        // We save the hardware type which is currently
                        // initiated
                        //
                        ActiveHardware = (HardwareType)cbbHws.SelectedIndex;

                        // We start to read from the CAN Queue
                        //
                        tmrRead.Enabled = true;

                        // Set UI enable
                        btnInit.Enabled      = !(btnWrite.Enabled = btnSetFilter.Enabled = btnResetFilter.Enabled = btnRelease.Enabled = btnInfo.Enabled = btnDllInfo.Enabled = true);
                        rdbTimer.Enabled     = true;
                        rdbEvent.Enabled     = true;
                        chbTimeStamp.Enabled = true;
                        cbbHws_SelectedIndexChanged(this, new EventArgs());

                        // We show the information of the configured
                        // and initiated hardware
                        //
                        txtInfo.Text  = "Active Hardware: " + cbbHws.Text;
                        txtInfo.Text += "\r\nBaud Rate: " + cbbBaudrates.Text;
                        txtInfo.Text += "\r\nFrame Type: " + cbbMsgType.Text;
                        // If was a no P&P Hardware, we show additional information
                        //
                        if (cbbIO.Enabled)
                        {
                            txtInfo.Text += "\r\nI/O Addr.: " + cbbIO.Text + "h";
                            txtInfo.Text += "\r\nInterrupt: " + cbbInterrupt.Text;
                        }
                    }
                    // An error occurred.  We show the error.
                    //
                    else
                    {
                        txtInfo.Text = "Error: " + Res.ToString();
                    }
                }
            }
            else
            {
                txtInfo.Text = "Error: " + Res.ToString();
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Sends the Command List over the CAN Bus
        /// </summary>
        /// <param name="cmds"></param>
        /// <param name="numPackets"></param>
        private void SendCommandList(List <String> cmds, byte numPackets)
        {
            #region ValCheck
            if (cmds == null || numPackets == 0)
            {
                return;
            }
            #endregion

            #region setup

            TCLightMsg MsgToSend = new TCLightMsg();
            MsgToSend.ID      = 0x013EE8E0 | 0x00000001;
            MsgToSend.Len     = 1;
            MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED;
            MsgToSend.Data[0] = numPackets;

            TCLightTimestamp q = new TCLightTimestamp();
            q.millis = 1000;
            console.AddOutsideMessage(MsgToSend, q);

            PCANLight.Write(ActiveHardware, MsgToSend);


            #endregion

            #region move

            MsgToSend.ID      = 0x013EE0E0 | 0x00000001;
            MsgToSend.Len     = 8;
            MsgToSend.MsgType = MsgTypes.MSGTYPE_EXTENDED;
            byte count = 0;
            foreach (String cmd in cmds)
            {
                String[] cmd2 = cmd.Split('~');

                for (int i = 0; i < MsgToSend.Len; i++)
                {
                    #region Data
                    switch (i)
                    {
                    case 0:
                        ProfileParamsTb.AppendText(cmd + "\r\n");
                        MsgToSend.Data[0] = count;
                        count++;
                        break;

                    case 1:
                        MsgToSend.Data[1] = numPackets;
                        break;

                    case 2:
                        MsgToSend.Data[2] = BitConverter.GetBytes(Int16.Parse(cmd2[0]))[1];
                        break;

                    case 3:
                        MsgToSend.Data[3] = BitConverter.GetBytes(Int16.Parse(cmd2[0]))[0];
                        break;

                    case 4:
                        MsgToSend.Data[4] = BitConverter.GetBytes(UInt16.Parse(cmd2[1]))[1];
                        break;

                    case 5:
                        MsgToSend.Data[5] = BitConverter.GetBytes(UInt16.Parse(cmd2[1]))[0];
                        break;

                    case 6:

                        MsgToSend.Data[6] = BitConverter.GetBytes(UInt16.Parse(cmd2[2]))[1];
                        break;

                    case 7:
                        MsgToSend.Data[7] = BitConverter.GetBytes(UInt16.Parse(cmd2[2]))[0];
                        break;
                    }
                    #endregion
                }
                console.AddOutsideMessage(MsgToSend, q);
                PCANLight.Write(ActiveHardware, MsgToSend);
            }
            #endregion

            #region Delete
            //prevents sending repeats, consider disabling if you want such a feature
            cmds = null;

            numPackets = 0;
            #endregion
        }