コード例 #1
0
        public static VSCAN InitCAN()
        {
            var canAdapter = new VSCAN();

            try
            {
                // set debugging options

                canAdapter.SetDebug(VSCAN.VSCAN_DEBUG_NONE);
                canAdapter.SetDebugMode(VSCAN.VSCAN_DEBUG_MODE_FILE);

                // open CAN channel: please specify the name of your device according to User Manual

                canAdapter.Open(VSCAN.VSCAN_FIRST_FOUND,
                                VSCAN.VSCAN_MODE_NORMAL);

                // set some options

                canAdapter.SetSpeed(VSCAN.VSCAN_SPEED_125K);
                canAdapter.SetTimestamp(VSCAN.VSCAN_TIMESTAMP_ON);
                canAdapter.SetBlockingRead(VSCAN.VSCAN_IOCTL_ON);

                /*canAdapter.SetFilterMode(VSCAN.VSCAN_FILTER_MODE_DUAL);
                 * // refer to http://www.si-kwadraat.nl/sja1000/codemask.php to establish acceptance code and mask
                 * // for the range of Rx IDs in USB_CAN_Plus_Ctrl.MessagesIDs
                 * // (http://www.si-kwadraat.nl/sja1000/codemask.php?en=dcia&mo=0&cd=00000000&ma=FFFFFFFF&fm=2&id=0.1&rn=0.5)
                 * mask.Code = 0x582F5007;
                 * mask.Mask = 0x000000E8;
                 * canAdapter.SetACCCodeMask(mask);*/
            }

            catch (Exception e)
            {
                Console.WriteLine($"CAN not opened {e.Message}");
            }

            return(canAdapter);
        }
コード例 #2
0
        public static void Main(string[] args)

        {
            VSCAN_MSG[] msgs = new VSCAN_MSG[2];

            VSCAN CanDevice = new VSCAN();

            UInt32 Written = 0;

            UInt32 Read = 0;

            VSCAN_HWPARAM hw = new VSCAN_HWPARAM();

            VSCAN_API_VERSION api_ver = new VSCAN_API_VERSION();

            byte Flags = 0x0;



            try

            {
                // set debugging options

                CanDevice.SetDebug(VSCAN.VSCAN_DEBUG_NONE);

                CanDevice.SetDebugMode(VSCAN.VSCAN_DEBUG_MODE_FILE);



                // open CAN channel: please specify the name of your device according to User Manual

                CanDevice.Open(VSCAN.VSCAN_FIRST_FOUND, VSCAN.VSCAN_MODE_SELF_RECEPTION);



                // set some options

                CanDevice.SetSpeed(VSCAN.VSCAN_SPEED_1M);

                CanDevice.SetTimestamp(VSCAN.VSCAN_TIMESTAMP_ON);

                CanDevice.SetBlockingRead(VSCAN.VSCAN_IOCTL_ON);



                // get HW Params

                CanDevice.GetHwParams(ref hw);

                Console.WriteLine("Get hardware paramter:");

                Console.WriteLine("HwVersion:" + hw.HwVersion + " SwVersion:" + (hw.SwVersion >> 4) + "." + (hw.SwVersion & 0x0f));

                Console.WriteLine("SerNr:" + hw.SerialNr + " HwType:" + hw.HwType);



                // get API version

                CanDevice.GetApiVersion(ref api_ver);

                Console.WriteLine("");

                Console.WriteLine("API version: " + api_ver.Major + "." + api_ver.Minor + "." + api_ver.SubMinor);



                msgs[0].Id = 0x100;

                msgs[0].Size = 2;

                msgs[0].Data = new byte[8];

                msgs[0].Data[0] = 0xde;

                msgs[0].Data[1] = 0xad;

                msgs[0].Flags = VSCAN.VSCAN_FLAGS_EXTENDED;



                msgs[1].Id = 0x101;

                msgs[1].Size = 2;

                msgs[1].Data = new byte[8];

                msgs[1].Data[0] = 0xbe;

                msgs[1].Data[1] = 0xef;

                msgs[1].Flags = VSCAN.VSCAN_FLAGS_EXTENDED;



                // send CAN frames

                CanDevice.Write(msgs, 2, ref Written);

                // send immediately

                CanDevice.Flush();

                Console.WriteLine("");

                Console.WriteLine("Send CAN frames: " + Written);



                // read CAN frames

                CanDevice.Read(ref msgs, 2, ref Read);

                Console.WriteLine("");

                Console.WriteLine("Read CAN frames: " + Read);

                for (int i = 0; i < Read; i++)

                {
                    Console.WriteLine("");

                    Console.WriteLine("CAN frame " + i);

                    Console.WriteLine("ID: " + msgs[i].Id);

                    Console.WriteLine("Size: " + msgs[i].Size);

                    Console.Write("Data: ");

                    for (int j = 0; j < msgs[i].Size; j++)

                    {
                        Console.Write(msgs[i].Data[j] + " ");
                    }

                    Console.WriteLine("");

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_STANDARD) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_STANDARD");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_EXTENDED) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_EXTENDED");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_REMOTE) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_REMOTE");
                    }

                    if ((msgs[i].Flags & VSCAN.VSCAN_FLAGS_TIMESTAMP) != 0)
                    {
                        Console.WriteLine("VSCAN_FLAGS_TIMESTAMP");
                    }

                    Console.WriteLine("TS: " + msgs[i].TimeStamp);
                }



                // get extended status and error flags

                CanDevice.GetFlags(ref Flags);

                Console.WriteLine("");

                Console.WriteLine("Extended status and error flags: " + Flags);

                DecodeFlags(Flags);



                // close CAN channel

                CanDevice.Close();
            }

            catch (Exception e)

            {
                Console.WriteLine("CAN opened " + e.Message);
            }
        }
コード例 #3
0
        bool iniCAN()
        {
            try
            {
                msgs = new VSCAN_MSG[2];

                CanDevice = new VSCAN();

                Written = 0;

                // ReadCAN = 0;

                hw = new VSCAN_HWPARAM();

                api_ver = new VSCAN_API_VERSION();

                Flags = 0x0;


                // set debugging options

                CanDevice.SetDebug(VSCAN.VSCAN_DEBUG_NONE);

                CanDevice.SetDebugMode(VSCAN.VSCAN_DEBUG_MODE_FILE);



                // open CAN channel: please specify the name of your device according to User Manual

                CanDevice.Open(VSCAN.VSCAN_FIRST_FOUND, VSCAN.VSCAN_MODE_SELF_RECEPTION);



                // set some options

                CanDevice.SetSpeed(VSCAN.VSCAN_SPEED_250K);

                // CanDevice.SetTimestamp(VSCAN.VSCAN_TIMESTAMP_ON);
                CanDevice.SetTimestamp(VSCAN.VSCAN_TIMESTAMP_OFF);

                CanDevice.SetBlockingRead(VSCAN.VSCAN_IOCTL_ON);
                //   CanDevice.SetBlockingRead(VSCAN.VSCAN_IOCTL_OFF);



                // get HW Params

                CanDevice.GetHwParams(ref hw);

                add("Get hardware paramter:");

                add("HwVersion:" + hw.HwVersion + " SwVersion:" + (hw.SwVersion >> 4) + "." + (hw.SwVersion & 0x0f));

                add("SerNr:" + hw.SerialNr + " HwType:" + hw.HwType);



                // get API version

                CanDevice.GetApiVersion(ref api_ver);


                add("API version: " + api_ver.Major + "." + api_ver.Minor + "." + api_ver.SubMinor + "\n----------------");



                data.connOK   = true;
                data.SOBITkod = 1;

                return(true);
            }
            catch (Exception ex)
            {
                data.SOBstr = "error connect " + String.Format("{0}", ex); data.SOBITkod = 2;
                // button7.Enabled = false;
                // button3.Enabled = true;



                // MessageBox.Show(String.Format("{0}", ex), "Ошибка VSСOM");
                return(false);
            }
        }